1
- /*
2
- 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.
3
- The first ten terms would be:
1
+ package ProjectEuler ;
2
+ /**
3
+ * The sequence of triangle numbers is generated by adding the natural numbers.
4
+ * So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.
5
+ * The first ten terms would be:
6
+ * <p>
7
+ * 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
8
+ * <p>
9
+ * Let us list the factors of the first seven triangle numbers:
10
+ * <p>
11
+ * 1: 1
12
+ * 3: 1,3
13
+ * 6: 1,2,3,6
14
+ * 10: 1,2,5,10
15
+ * 15: 1,3,5,15
16
+ * 21: 1,3,7,21
17
+ * 28: 1,2,4,7,14,28
18
+ * We can see that 28 is the first triangle number to have over five divisors.
19
+ * <p>
20
+ * What is the value of the first triangle number to have over five hundred divisors?
21
+ * <p>
22
+ * link: https://projecteuler.net/problem=12
23
+ */
24
+ public class Problem12 {
4
25
5
- 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
6
-
7
- Let us list the factors of the first seven triangle numbers:
8
-
9
- 1: 1
10
- 3: 1,3
11
- 6: 1,2,3,6
12
- 10: 1,2,5,10
13
- 15: 1,3,5,15
14
- 21: 1,3,7,21
15
- 28: 1,2,4,7,14,28
16
- We can see that 28 is the first triangle number to have over five divisors.
17
-
18
- What is the value of the first triangle number to have over five hundred divisors?
19
- */
20
-
21
- public class Problem_12 {
26
+ /**
27
+ * Driver Code
28
+ */
29
+ public static void main (String [] args ) {
30
+ assert solution1 (500 ) == 76576500 ;
31
+ }
22
32
23
33
/* returns the nth triangle number; that is, the sum of all the natural numbers less than, or equal to, n */
24
34
public static int triangleNumber (int n ) {
@@ -28,36 +38,29 @@ public static int triangleNumber(int n) {
28
38
return sum ;
29
39
}
30
40
31
- public static void main (String [] args ) {
32
-
33
- long start = System .currentTimeMillis (); // start the stopwatch
34
-
41
+ public static int solution1 (int number ) {
35
42
int j = 0 ; // j represents the jth triangle number
36
43
int n = 0 ; // n represents the triangle number corresponding to j
37
44
int numberOfDivisors = 0 ; // number of divisors for triangle number n
38
-
39
- while (numberOfDivisors <= 500 ) {
45
+
46
+ while (numberOfDivisors <= number ) {
40
47
41
48
// resets numberOfDivisors because it's now checking a new triangle number
42
49
// and also sets n to be the next triangle number
43
50
numberOfDivisors = 0 ;
44
51
j ++;
45
52
n = triangleNumber (j );
46
-
53
+
47
54
// for every number from 1 to the square root of this triangle number,
48
55
// count the number of divisors
49
56
for (int i = 1 ; i <= Math .sqrt (n ); i ++)
50
57
if (n % i == 0 )
51
58
numberOfDivisors ++;
52
-
59
+
53
60
// 1 to the square root of the number holds exactly half of the divisors
54
61
// so multiply it by 2 to include the other corresponding half
55
62
numberOfDivisors *= 2 ;
56
63
}
57
-
58
- long finish = System .currentTimeMillis (); // stop the stopwatch
59
-
60
- System .out .println (n );
61
- System .out .println ("Time taken: " + (finish - start ) + " milliseconds" );
64
+ return n ;
62
65
}
63
66
}
0 commit comments