Showing posts with label EULER 21-30. Show all posts
Showing posts with label EULER 21-30. Show all posts

Sunday, February 5, 2012

Problem 24

Problem Statement :Problem24

A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012   021   102   120   201   210
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
Approach:
  • Well if the actual number  is a0,a1,a2,a3,a4,a5,a6,a7,a8,a9:
    a0*9! + a1*8! + a2*7! + ..... + a9*0! = 10^6 
  • To do this start filling the number from the right 
  • In every iteration decide the digit at that position and subtract the remaining from count.
  • In each iteration we are removing multiples of k! 
  • Quit when count reaches 0.  

Solution:
    import java.util.ArrayList;
    import java.util.List;
    
    public class problem24 {
     public static List list = new ArrayList();
     
     public static long getFactorial(int limit) {
      long factorial = 1;
      for (int i = 1; i <= limit; i++)
       factorial = factorial * i;
      return factorial;
     }
    
     public static void main(String chars[]) {
      long count = 1000000;
      int num = 9;
      list.add(0);
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(6);
      list.add(7);
      list.add(8);
      list.add(9);
      
      String s = "";
      while (count != 0) {
       long fact = getFactorial(num--);
       int p1 = (int) (count / fact);
       s=s+""+list.get(p1);
       list.remove(p1);
       count = count - fact * p1;
      }
     System.out.println(s+list.get(0));
     }
    }
    

Wednesday, February 1, 2012

Problem 23


Problem Statement :Problem23
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.


Approach:
  • Find all abundant numbers and fill them in a list
  • Iterate over the list in 2 for loops and generate all possible abundant numbers
  • Use an array to mark the index (ie generated abundant numbers).
  • All indexes that are not marked will not be abundant.  

Solution:
    import java.util.ArrayList;
    import java.util.List;
    
    public class problem24 {
     public static List<Integer> list = new ArrayList<Integer>();
     
     public static long getFactorial(int limit) {
      long factorial = 1;
      for (int i = 1; i <= limit; i++)
       factorial = factorial * i;
      return factorial;
     }
    
     public static void main(String chars[]) {
      long count = 1000000;
      int num = 9;
      list.add(0);
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(6);
      list.add(7);
      list.add(8);
      list.add(9);
      
      String s = "";
      while (count != 0) {
       long fact = getFactorial(num--);
       int p1 = (int) (count / fact);
       s=s+""+list.get(p1);
       list.remove(p1);
       count = count - fact * p1;
      }
     System.out.println(s+list.get(0));
     }
    }
    

Problem 22


Problem Statement :Problem22

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?


Approach:
  • Just follow what is written in the problem
Solution:
    import java.util.Arrays;
    public class problem22 {
    	public static void main(String args[])
    	{
    		String x;//read from file
    		long t1=System.currentTimeMillis();
    		String arr[]=x.split(",");
    		Arrays.sort(arr);
    		long total=0;
    		for(int i=0;i

Problem 21

Problem Statement :Problem21
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.


Approach:
  • Function getvalue will return the value of all divisors of a number 
  • Call it twice and and just check whether they are equal or not.
Solution:
    public class problem21 {
     public static int isPrime(int n1) {
      int num = n1;
      for (int i = 2; i <= Math.sqrt(num); i++) {
       if (num % i == 0)
        return i;
      }
      return 0;
     }
    
    
     static int getvalue(int num) {
      if (isPrime(num) == 0)
       return 1;
      int value = 0;
      for (int i = 1; i <= num / 2; i++) {
       if (num % i == 0)
        value = value + i;
      }
      return value;
     }
    
     public static void main(String args[]) {
      int n = 10000, sum = 0;
      for (int i = 2; i < n; i++) {
       int val = getvalue(i);
       int revval = getvalue(val);
       if (i == revval && val != i)
        sum = sum + revval;
      }
      System.out.println(sum);
     }
    }