Skip to content

Commit 4e181d1

Browse files
author
Scott Weeden-Moody
committed
Added solution for problem 12
1 parent f553374 commit 4e181d1

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

12/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Project Euler - Problem #11
2+
3+
## Highly divisible triangluar number
4+
5+
### Problem 12
6+
7+
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
52+
limitations under the License.

12/build.gradle

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2016 Scott Weeden-Moody
3+
*
4+
* 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+
apply plugin: 'kotlin'
18+
19+
buildscript {
20+
ext.kotlin_version = '1.3.50'
21+
repositories {
22+
mavenCentral()
23+
}
24+
dependencies {
25+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
26+
}
27+
}
28+
repositories {
29+
mavenCentral()
30+
}
31+
dependencies {
32+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
33+
}
34+
compileKotlin {
35+
kotlinOptions {
36+
jvmTarget = "1.8"
37+
}
38+
}
39+
compileTestKotlin {
40+
kotlinOptions {
41+
jvmTarget = "1.8"
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2019 Scott Weeden-Moody
3+
*
4+
* 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+
package com.lillicoder.projecteuler
18+
19+
import kotlin.math.ceil
20+
import kotlin.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+
fun main() {
49+
val divisorCount = 500
50+
val triangleNumber = Problem12().withDivisorCount(divisorCount)
51+
println("The triangle number with more than $divisorCount divisors is: $triangleNumber")
52+
}
53+
54+
class Problem12 {
55+
56+
fun withDivisorCount(count: Int): Long {
57+
var current = 0L
58+
var iteration = 1
59+
60+
var didFind = false
61+
while (!didFind) {
62+
current = nextTriangleNumber(current, iteration)
63+
val divisors = divisors(current)
64+
if (divisors.size > count) didFind = true
65+
66+
println("[debug] Triangle number: $current, divisorCount: ${divisors.size}, divisors: $divisors")
67+
iteration++
68+
}
69+
70+
return current
71+
}
72+
73+
private fun divisors(number: Long): List<Long> {
74+
val divisors = mutableListOf(number)
75+
val half = number / 2
76+
for (divisor in 1..half) {
77+
if (number % divisor == 0.toLong()) divisors.add(divisor)
78+
}
79+
80+
return divisors
81+
}
82+
83+
private fun nextTriangleNumber(lastTriangleNumber: Long, iteration: Int): Long {
84+
return lastTriangleNumber + iteration;
85+
}
86+
87+
}

settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ include ":08"
99
include ":09"
1010
include ":10"
1111
include ":11"
12+
include ":12"

0 commit comments

Comments
 (0)