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));
     }
    }
    

1 comment:

  1. dude...i guess we have similar interests...

    check out this..and this site is definitely useful for you!!!


    http://oeis.org/A000396

    ReplyDelete