Skip to content

Commit 063a0ec

Browse files
MaximSmolskiygithub-actions
and
github-actions
authored
feat: add Project Euler problem 115 solution 1 (TheAlgorithms#6303)
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent a69d880 commit 063a0ec

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,8 @@
856856
* [Sol1](project_euler/problem_113/sol1.py)
857857
* Problem 114
858858
* [Sol1](project_euler/problem_114/sol1.py)
859+
* Problem 115
860+
* [Sol1](project_euler/problem_115/sol1.py)
859861
* Problem 119
860862
* [Sol1](project_euler/problem_119/sol1.py)
861863
* Problem 120

Diff for: project_euler/problem_115/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_115/sol1.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Project Euler Problem 115: https://projecteuler.net/problem=115
3+
4+
NOTE: This is a more difficult version of Problem 114
5+
(https://projecteuler.net/problem=114).
6+
7+
A row measuring n units in length has red blocks
8+
with a minimum length of m units placed on it, such that any two red blocks
9+
(which are allowed to be different lengths) are separated by at least one black square.
10+
11+
Let the fill-count function, F(m, n),
12+
represent the number of ways that a row can be filled.
13+
14+
For example, F(3, 29) = 673135 and F(3, 30) = 1089155.
15+
16+
That is, for m = 3, it can be seen that n = 30 is the smallest value
17+
for which the fill-count function first exceeds one million.
18+
19+
In the same way, for m = 10, it can be verified that
20+
F(10, 56) = 880711 and F(10, 57) = 1148904, so n = 57 is the least value
21+
for which the fill-count function first exceeds one million.
22+
23+
For m = 50, find the least value of n
24+
for which the fill-count function first exceeds one million.
25+
"""
26+
27+
from itertools import count
28+
29+
30+
def solution(min_block_length: int = 50) -> int:
31+
"""
32+
Returns for given minimum block length the least value of n
33+
for which the fill-count function first exceeds one million
34+
35+
>>> solution(3)
36+
30
37+
38+
>>> solution(10)
39+
57
40+
"""
41+
42+
fill_count_functions = [1] * min_block_length
43+
44+
for n in count(min_block_length):
45+
fill_count_functions.append(1)
46+
47+
for block_length in range(min_block_length, n + 1):
48+
for block_start in range(n - block_length):
49+
fill_count_functions[n] += fill_count_functions[
50+
n - block_start - block_length - 1
51+
]
52+
53+
fill_count_functions[n] += 1
54+
55+
if fill_count_functions[n] > 1_000_000:
56+
break
57+
58+
return n
59+
60+
61+
if __name__ == "__main__":
62+
print(f"{solution() = }")

0 commit comments

Comments
 (0)