This commit is contained in:
shellhub 2020-10-06 14:58:38 +08:00
parent 064d941722
commit 8ad87ce9d3

View File

@ -1,24 +1,34 @@
/* package ProjectEuler;
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: * 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.
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... * The first ten terms would be:
* <p>
Let us list the factors of the first seven triangle numbers: * 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
* <p>
1: 1 * Let us list the factors of the first seven triangle numbers:
3: 1,3 * <p>
6: 1,2,3,6 * 1: 1
10: 1,2,5,10 * 3: 1,3
15: 1,3,5,15 * 6: 1,2,3,6
21: 1,3,7,21 * 10: 1,2,5,10
28: 1,2,4,7,14,28 * 15: 1,3,5,15
We can see that 28 is the first triangle number to have over five divisors. * 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? * We can see that 28 is the first triangle number to have over five divisors.
* <p>
* What is the value of the first triangle number to have over five hundred divisors?
* <p>
* link: https://projecteuler.net/problem=12
*/ */
public class Problem12 {
public class Problem_12 { /**
* Driver Code
*/
public static void main(String[] args) {
assert solution1(500) == 76576500;
}
/* returns the nth triangle number; that is, the sum of all the natural numbers less than, or equal to, n */ /* returns the nth triangle number; that is, the sum of all the natural numbers less than, or equal to, n */
public static int triangleNumber(int n) { public static int triangleNumber(int n) {
@ -28,15 +38,12 @@ public class Problem_12 {
return sum; return sum;
} }
public static void main(String[] args) { public static int solution1(int number) {
long start = System.currentTimeMillis(); // start the stopwatch
int j = 0; // j represents the jth triangle number int j = 0; // j represents the jth triangle number
int n = 0; // n represents the triangle number corresponding to j int n = 0; // n represents the triangle number corresponding to j
int numberOfDivisors = 0; // number of divisors for triangle number n int numberOfDivisors = 0; // number of divisors for triangle number n
while (numberOfDivisors <= 500) { while (numberOfDivisors <= number) {
// resets numberOfDivisors because it's now checking a new triangle number // resets numberOfDivisors because it's now checking a new triangle number
// and also sets n to be the next triangle number // and also sets n to be the next triangle number
@ -54,10 +61,6 @@ public class Problem_12 {
// so multiply it by 2 to include the other corresponding half // so multiply it by 2 to include the other corresponding half
numberOfDivisors *= 2; numberOfDivisors *= 2;
} }
return n;
long finish = System.currentTimeMillis(); // stop the stopwatch
System.out.println(n);
System.out.println("Time taken: " + (finish - start) + " milliseconds");
} }
} }