Skip to content

Commit 8ad87ce

Browse files
reformat
1 parent 064d941 commit 8ad87ce

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

ProjectEuler/Problem12.java

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
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 {
425

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+
}
2232

2333
/* returns the nth triangle number; that is, the sum of all the natural numbers less than, or equal to, n */
2434
public static int triangleNumber(int n) {
@@ -28,36 +38,29 @@ public static int triangleNumber(int n) {
2838
return sum;
2939
}
3040

31-
public static void main(String[] args) {
32-
33-
long start = System.currentTimeMillis(); // start the stopwatch
34-
41+
public static int solution1(int number) {
3542
int j = 0; // j represents the jth triangle number
3643
int n = 0; // n represents the triangle number corresponding to j
3744
int numberOfDivisors = 0; // number of divisors for triangle number n
38-
39-
while (numberOfDivisors <= 500) {
45+
46+
while (numberOfDivisors <= number) {
4047

4148
// resets numberOfDivisors because it's now checking a new triangle number
4249
// and also sets n to be the next triangle number
4350
numberOfDivisors = 0;
4451
j++;
4552
n = triangleNumber(j);
46-
53+
4754
// for every number from 1 to the square root of this triangle number,
4855
// count the number of divisors
4956
for (int i = 1; i <= Math.sqrt(n); i++)
5057
if (n % i == 0)
5158
numberOfDivisors++;
52-
59+
5360
// 1 to the square root of the number holds exactly half of the divisors
5461
// so multiply it by 2 to include the other corresponding half
5562
numberOfDivisors *= 2;
5663
}
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;
6265
}
6366
}

0 commit comments

Comments
 (0)