You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
8
+
9
+
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
10
+
11
+
Let us list the factors of the first seven triangle numbers:
12
+
13
+
1: 1
14
+
3: 1,3
15
+
6: 1,2,3,6
16
+
10: 1,2,5,10
17
+
15: 1,3,5,15
18
+
21: 1,3,7,21
19
+
28: 1,2,4,7,14,28
20
+
21
+
We can see that 28 is the first triangle number to have over five divisors.
22
+
23
+
What is the value of the first triangle number to have over five hundred divisors?
24
+
25
+
---------
26
+
27
+
A very straightforward problem at a high level. For each triangle number starting from the first, find the divisors.
28
+
If the count of the divisor list exceeds 500, you found the number.
29
+
30
+
Generating triangle numbers iteratively is easy as each subsequent triangle number is the previous one plus
31
+
the next number. Computing divisors can be done by finding prime divisors and multiplying the exponents.
32
+
Alternatively, you can just brute force it and wait ~20 minutes.
33
+
34
+
Answer: 76576500
35
+
36
+
---------
37
+
38
+
--License--
39
+
40
+
Copyright 2019 Scott Weeden-Moody
41
+
42
+
Licensed under the Apache License, Version 2.0 (the "License");
43
+
you may not use this project except in compliance with the License.
44
+
You may obtain a copy of the License at
45
+
46
+
http://www.apache.org/licenses/LICENSE-2.0
47
+
48
+
Unless required by applicable law or agreed to in writing, software
49
+
distributed under the License is distributed on an "AS IS" BASIS,
50
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51
+
See the License for the specific language governing permissions and
* Licensed under the Apache License, Version 2.0 (the "License");
5
+
* you may not use this project except in compliance with the License.
6
+
* You may obtain a copy of the License at
7
+
*
8
+
* http://www.apache.org/licenses/LICENSE-2.0
9
+
*
10
+
* Unless required by applicable law or agreed to in writing, software
11
+
* distributed under the License is distributed on an "AS IS" BASIS,
12
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+
* See the License for the specific language governing permissions and
14
+
* limitations under the License.
15
+
*/
16
+
17
+
packagecom.lillicoder.projecteuler
18
+
19
+
importkotlin.math.ceil
20
+
importkotlin.math.sqrt
21
+
22
+
/**
23
+
* Project Euler - Problem #12
24
+
*
25
+
* Highly divisible triangular number
26
+
*
27
+
* Problem 12
28
+
*
29
+
* 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:
30
+
*
31
+
* 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
32
+
*
33
+
* Let us list the factors of the first seven triangle numbers:
34
+
*
35
+
* 1: 1
36
+
* 3: 1,3
37
+
* 6: 1,2,3,6
38
+
* 10: 1,2,5,10
39
+
* 15: 1,3,5,15
40
+
* 21: 1,3,7,21
41
+
* 28: 1,2,4,7,14,28
42
+
*
43
+
* We can see that 28 is the first triangle number to have over five divisors.
44
+
*
45
+
* What is the value of the first triangle number to have over five hundred divisors?
46
+
*/
47
+
48
+
funmain() {
49
+
val divisorCount =500
50
+
val triangleNumber =Problem12().withDivisorCount(divisorCount)
51
+
println("The triangle number with more than $divisorCount divisors is: $triangleNumber")
0 commit comments