Showing posts with label EULER 1 -10. Show all posts
Showing posts with label EULER 1 -10. Show all posts

Saturday, January 21, 2012

Problem10


Problem Statement :Problem10


Algorithm:
Well I solved this in a straight forward manner running over all primes less than the limit.
We can also use Eratosthenes Sieve or the Segmented Sieve to speed up the algo. 
Solution:
    public class problem10 {
     public static boolean isPrime(double num) {
      for (int i = 2; i <= Math.sqrt(num); i++) {
       if (num % i == 0)
        return false;
      }
      return true;
     }
     public static void main(String args[])
     {
      long sum=2;
      for(long i=3;;i=i+2)
      {  
       if(i>2000000)
        break;
       if(isPrime(i))
        sum+=i;
      }
     System.out.println(sum);
     }
    }
    

Saturday, January 14, 2012

Problem 9


Problem Statement :Problem9


Algorithm:
We  solve the two given equations in the problem ie
  • a2 + b2 = c2
  • a + b + c = 1000
This leads to value of  b = (1000000 - 2000 * a) /  (2000 - 2 * a).
The remaining part is olved by iterating over 1000 values of a using the above value of b and checking for c.

Solution:
    public class problem9 {
     public static void main(String args[]) {
      for (int a = 999; a >= 1; a--) {
       double b = (1000000 - 2000 * a) / (double) (2000 - 2 * a);
       if ((a * a + b * b) == (1000 - a - b) * (1000 - a - b) && a != b
         && (b - (int) b) == 0 && b > 0)
        System.out.println(a * b * (1000 - a - b));
      }
     }
    }
    
    

Problem 7


Problem Statement :Problem7

Algorithm:

  • Because we can not run a direct loop we keep adding primes to a list and check whether the size of the list exceeds 10001.
  • As soon as it does we break and print the prime.

Solution:

    import java.util.ArrayList;
    import java.util.List;
    
    public class problem7 {
     public static boolean isPrime(double num) {
      for (int i = 2; i <= Math.sqrt(num); i++) {
       if (num % i == 0)
        return false;
      }
      return true;
     }
     
     public static List<Long> primes = new ArrayList<Long>();
     
     public static void main(String args[])
     {
      primes.add((long)2);
      for(long i=3;;i=i+2)
      {
       if(isPrime(i))
        primes.add(i);
       if(primes.size()==10001)
        break;
      }
     System.out.println(primes.get(10000));
     }
     
    }
    
    

Problem 6


Problem Statement :Problem6

Algorithm:
Well i will say this ones is pretty direct as Problem 1 on my blog.You dont need any code for this

  • Sum of k^2 is N*(N+1)*(2*N+1)/6 
  • Sum of k whole square is (N*(N+1)/2)^2
  • Subtract the second value from the first for N=100 for the answer 

Problem 4

Problem Statement :Problem4

Algorithm:
We brute over all possible palindromes from 999*999 to 100*100. Then we try to find all possible palindromes and find the largest among them.

Solution:
    public class problem4 {
    
     public static boolean isPalind(int n) {
      String num = n + "";
      for (int j = 0; j < num.length(); j++) {
       if (num.charAt(j) != num.charAt(num.length() - 1 - j))
        return false;
      }
      return true;
     }
    
     public static void main(String args[]) {
      int max=Integer.MIN_VALUE; 
      for (int i = 999; i >= 100; i--) {
       for (int j = i; j >= 100; j--) {
        int prod = i * j;
        if (isPalind(prod)) {
         if(max<prod)
          max=prod;
        }
       }
      }
      System.out.println(max);
     }
    }
    
    

Problem 3

Problem Statement : Problem 3

Algorithm:
We try to iterate over all the factors of the number.
The algorithm runs over all prime factors of a given number that are less than N^.5.

Solution:
public class problem3 {
 public static boolean isPrime(double num) {
  for (int i = 2; i <= Math.sqrt(num); i++) {
   if (num % i == 0)
    return false;
  }
  return true;
 }

 public static void main(String rgp[]) {
  long num = (long) 600851475143.0;
  
  for (int i = (int)Math.pow(num,.5);  i>=3; i--) {
   if (num % i == 0) {
    if (isPrime(i)) {
     System.out.println(i);
     break;
    }
   }
  }
 }
}

Problem 2


Problem Statement :Problem 2


Algorithm:
Generating the Fibonacci series can be read here
    Solution:
    A linear time algorithm can is written here:

        public class problem2 {
         public static void main(String args[]) {
          int a = 1, b = 2;
          int sum = 2;
          while (true) {
           int c = a + b;
           a = b;
           b = c;
           if (c > 4000000)
            break;
        
           if (c % 2 == 0)
            sum = sum + c;
          }
          System.out.println(sum);
         }
        }
        

      Problem 1

      Problem Statement : Problem 1


      Algorithm:
      This problem is pretty simple and requires basic set theory.
      Explanation about the principle can be read here.
      • Calculate sum of all multiples of 3 less than 1000
      • Calculate sum of all multiples of 5 less than 1000
      • Calculate sum of all multiples of 15 less than 1000
      Solution:
      A linear time running algorithm to calculate this is written below:
          public class problem1 {
           public static void main(String args[]) {
            int sum = 0;
            for (int i = 1; i < 1000; i++) {
             if (i % 3 == 0) {
              sum += i;
              continue;
             }
             if (i % 5 == 0)
              sum += i;
            }
            System.out.println(sum);
           }
          }