Wednesday, April 16, 2014

Prime numbers addition

In math, a prime number is a number only divisible by 1 and itself. Given the first few prime numbers 2 3 5 7 11 13 17 ...
What is the sum of the first 250 prime numbers


package primenumbersum;

public class Primenumbersum {

    
    public static void main(String[] args) {
        Integer val;
        Integer sum=0;
        Integer prnum=2;
        int cycle=1;
        while(cycle<251){
           if(isPrime(prnum)){
                sum+=prnum;
                   cycle++;
            }
         
           
           prnum++;
            
        }
        System.out.println("Sum: "+(int)sum);
    }
    
    //Determine whether a number is prime by doing a check

    public static boolean isPrime(int num){
       
         for(int i=2; i<num;i++){
           if (num%i==0||num==1){
               return false;
                    }
                }
      return true;
         }
}

Tuesday, April 15, 2014

Another Java Challenge

Challenge:

Given the following information:

a = 26, b = 25, c = 24, d = 23 ..... x = 3, y = 2, z = 1

What is the sum of each letter of this sentence: "The quick brown fox jumped over the cow"?

Answer:

public class Revalphabetcodes {
   
    public static void main(String[] args) {
        String element="The quick brown fox jumped over the cow";
        int cycle=element.length()-1;
        int sum=0;
        while(cycle>-1){
              if(Character.getNumericValue(element.charAt(cycle))!=-1){
         
            sum+=36-Character.getNumericValue(element.charAt(cycle));
                }
             cycle--;
            }
        System.out.println("sum is: "+sum);
        }
   
}

Friday, April 11, 2014

Java Examples executed

1. List Iterator with input from console

The following is a an example run for inputting an ArrayList from console and reads out. This example is for novices only............

package iteratortut;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.ListIterator;

public class Iteratortut {
    public static void main(String[] args) throws IOException {
        ArrayList<String> vegs=new ArrayList<String>();
        ListIterator ltr=null;
        BufferedReader bufferRead=new BufferedReader(new InputStreamReader(System.in));
        boolean inputreq=true;
        String s=null;
        String veginput=null;
           while(inputreq){
            System.out.println("Whether you want to continue : ");
            try{
            s=bufferRead.readLine();
                }catch(IOException e){
                e.printStackTrace();
                }
           
            if (s.equals("no")){
               inputreq=false;
            }
           
            System.out.println("The vegetable name is= ");
            veginput=bufferRead.readLine();
            vegs.add(veginput);
        }
       
        for(String veg: vegs){
            System.out.println("hi"+veg);
              }
       
    }
   
}

2. Challenge:  the sum of the digits of 2 to the 10th power is: 2^10 = 1024 => 1+0+2+4 => 7

What is the sum of the digits of 2^50?

 package powaddition;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

public class Powaddition {
    public static void main(String[] args) {
       int number=0;
       int power=0;
       int cycle=0;
      //Input number from console       
        System.out.println("What is the power: " );
        Scanner pscn=new Scanner(System.in);
        power=pscn.nextInt();

        System.out.println("What is the number: " );
        Scanner scn=new Scanner(System.in);
        number = (int) Math.pow(scn.nextInt(), power);

//How many cycle this will iterate through
        cycle=String.valueOf(number).length();
//Length of array to be created
         int[] intarry=new int[String.valueOf(number).length()];
     
//Call the function to perform seperation
 while(cycle>0){
            int res=addpower(number,cycle);
            System.out.println("the value returned is "+res);
            intarry[cycle-1]=res;
            number=(int) (number-res*Math.pow(10, cycle-1));
            cycle=cycle-1;
            }
       int sum=0;
       int iter=0;
        for(Integer n:intarry){
           sum=sum+intarry[iter];
           iter++;
                 }
        System.out.println("Sum is="+sum);
    }
//Function that performs separation
 
   public static int addpower(double d, int n){
               int num=(int) ((int)d/Math.pow(10, n-1));
                System.out.println("The rerune value is "+ num);
       return num;
      
           }
   
}