Advertisement
sindrijo

ProEuler_P12

Dec 16th, 2011
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package problems;
  2.  
  3.  
  4. /*
  5. Problem 12
  6. 08 March 2002
  7.  
  8. The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
  9.  
  10. 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
  11.  
  12. Let us list the factors of the first seven triangle numbers:
  13.  
  14.      1: 1
  15.      3: 1,3
  16.      6: 1,2,3,6
  17.     10: 1,2,5,10
  18.     15: 1,3,5,15
  19.     21: 1,3,7,21
  20.     28: 1,2,4,7,14,28
  21.  
  22. We can see that 28 is the first triangle number to have over five divisors.
  23.  
  24. What is the value of the first triangle number to have over five hundred divisors?
  25.  
  26. Answer:
  27.  
  28. FIVE HUNDRED DIVISORS?
  29.  
  30. This means the number has to be made up of at least 500 numbers, and probably a lot more.
  31.  
  32.  
  33. Generate numbers until you find one with 500 divisors?...
  34.  
  35. 0.5 x n(n+1)
  36.  
  37. */
  38.  
  39. public class Problem_12 {
  40.  
  41.     public static void main(String[] args) {
  42.        
  43.         int number = 0;
  44.         int i = 1;
  45.          
  46.         while(numberOfDivisors(number) < 500){
  47.             number += i;
  48.             i++;
  49.         }
  50.        
  51.         System.out.println("The answer is: " +number +" with " + numberOfDivisors(number) +" factors.");
  52.        
  53.     } //End of main.
  54.    
  55.     private static int numberOfDivisors(int inputNumber){
  56.        
  57.         int nod = 0; // Counts Number Of Divsions
  58.         int sqrt = (int) Math.sqrt(inputNumber);
  59.        
  60.         /* No need to check higher than the sqrt of the current number,
  61.         because if a value higher than the square root were a factor, it
  62.         would already have been checked. A factor of a number will always
  63.         have a "sibling" so to speak. */
  64.        
  65.         for(int i = 1; i<= sqrt; i++){
  66.            
  67.             if(inputNumber % i == 0){
  68.                 nod += 2;
  69.                
  70.                 /* You've found one factor, that means there must be
  71.                  * another number which is a factor. So you increment
  72.                  * by two.
  73.                  *
  74.                  * ALL numbers have at least two factors (except 1),
  75.                  * 1 and the number itself.
  76.                  */
  77.             }
  78.            
  79.         }
  80.        
  81.        
  82.         /* Correction if the number is a perfect square.
  83.          * */
  84.        
  85.         if (sqrt * sqrt == inputNumber) {
  86.             nod--;
  87.            
  88.             /* In other words, if the factor is the sqrt
  89.              * of the number it has no "sibling number" it
  90.              * is forever alone.
  91.              *
  92.              * Example = 9 has the factors 1, 3 and 9.
  93.              */
  94.         }
  95.      
  96.         return nod;
  97.    
  98.        
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement