Problem Statement :Problem12
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:
What is the value of the first triangle number to have over five hundred divisors?
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:1: 1We can see that 28 is the first triangle number to have over five divisors.
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
What is the value of the first triangle number to have over five hundred divisors?
Algorithm:
Solution:
- We solve the problem on a basic assumption that if one factor of the number is present within root of the number the we need not iterate over all the factors till n/2.
- count can be incremented twice just because of the above reason.
- The value of n = i * (i+1) / 2 is evident for i in the
public class problem12 { public static void main(String args[]) { for (int i = 10;; i++) { int n = i * (i + 1) / 2; int count = 2; for (int k = 2; k * k <= n; k++) { if (n % k == 0) count += 2; } if (count >= 500) { System.out.println(n); break; } } } }
No comments:
Post a Comment