Tuesday 23 August 2016

Project Euler Problem 12: Highly divisible triangular number

Problem

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:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?

Solution

First attempt

The trick of counting the divisors is only check up to the square root of the number, and each time add 2 to the count. It didn't cross my mind the first time, and I checked up to half the number and each time add 1 to the count. It took forever....

      private static final int DIVISOR_NUMBER = 500;  
      public static void main(String[] args) {  
           int triangleNumber = 1;  
           int natualNumber = 2;  
           int divisorNumber = 1;  
           while (divisorNumber <= DIVISOR_NUMBER){  
                triangleNumber += natualNumber;  
                divisorNumber = divisorNumber(triangleNumber);  
                natualNumber++;  
           }  
           System.out.println(triangleNumber);  
      }  
      private static int divisorNumber(int number){  
           int count = 2;  
           int sqrt = (int)Math.sqrt(number);  
           for (int i=2; i<=sqrt; i++){  
                if (number % i == 0){  
                     count+=2;  
                }  
           }  
           if (sqrt * sqrt == number){  
                count--;  
           }  
           return count;  
      }  

Second attempt

This is really brilliant.

Kudos to kpsychas who came up with it. https://projecteuler.net/thread=12;page=3#5370

It is a bit difficult to understand the original post. So I have expanded it to make it more clear.



      private static final int DIVISOR_NUMBER = 500;  
      public static void main(String[] args) {  
           int k = 2;  
           int divisorNumberOdd = 0;  
           int divisorNumberEven = 0;  
           do{  
                k++;  
                divisorNumberOdd = divisorNumber(k) * divisorNumber(2*k-1);  
                divisorNumberEven = divisorNumber(k) * divisorNumber(2*k+1);  
           }while(divisorNumberOdd <= DIVISOR_NUMBER && divisorNumberEven <= DIVISOR_NUMBER);  
           if (divisorNumberOdd > DIVISOR_NUMBER){  
                System.out.println(k*(2*k-1));  
           }else{  
                System.out.println(k*(2*k+1));  
           }  
      }  
      private static int divisorNumber(int number){  
           int count = 2;  
           int sqrt = (int)Math.sqrt(number);  
           for (int i=2; i<=sqrt; i++){  
                if (number % i == 0){  
                     count+=2;  
                }  
           }  
           if (sqrt * sqrt == number){  
                count--;  
           }  
           return count;  
      }  

No comments:

Post a Comment