Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 780f04e

Browse files
committedOct 9, 2020
Added solution for Project Euler problem 113. Fixes: #2695
1 parent c9500dc commit 780f04e

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
 

‎project_euler/problem_113/__init__.py

Whitespace-only changes.

‎project_euler/problem_113/sol1.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Working from left-to-right if no digit is exceeded by the digit to its left it is
3+
called an increasing number; for example, 134468.
4+
5+
Similarly if no digit is exceeded by the digit to its right it is called a decreasing
6+
number; for example, 66420.
7+
8+
We shall call a positive integer that is neither increasing nor decreasing a
9+
"bouncy" number; for example, 155349.
10+
11+
As n increases, the proportion of bouncy numbers below n increases such that there
12+
are only 12951 numbers below one-million that are not bouncy and only 277032
13+
non-bouncy numbers below 10^10.
14+
15+
How many numbers below a googol (10^100) are not bouncy?
16+
"""
17+
18+
19+
def choose(n: int, r: int) -> int:
20+
"""
21+
Calculate the binomial coefficient c(n,r) using the multiplicative formula.
22+
>>> choose(4,2)
23+
6
24+
>>> choose(5,3)
25+
10
26+
>>> choose(20,6)
27+
38760
28+
"""
29+
ret = 1.0
30+
for i in range(1, r + 1):
31+
ret *= (n + 1 - i) / i
32+
return round(ret)
33+
34+
35+
def non_bouncy_exact(n: int) -> int:
36+
"""
37+
Calculate the number of non-bouncy numbers with at most n digits.
38+
>>> non_bouncy_exact(1)
39+
9
40+
>>> non_bouncy_exact(6)
41+
7998
42+
>>> non_bouncy_exact(10)
43+
136126
44+
"""
45+
return choose(8 + n, n) + choose(9 + n, n) - 10
46+
47+
48+
def non_bouncy_upto(n: int) -> int:
49+
"""
50+
Calculate the number of non-bouncy numbers with at most n digits.
51+
>>> non_bouncy_upto(1)
52+
9
53+
>>> non_bouncy_upto(6)
54+
12951
55+
>>> non_bouncy_upto(10)
56+
277032
57+
"""
58+
return sum(non_bouncy_exact(i) for i in range(1, n + 1))
59+
60+
61+
def solution() -> int:
62+
"""
63+
Caclulate the number of non-bouncy numbers less than a googol.
64+
"""
65+
return non_bouncy_upto(100)
66+
67+
68+
if __name__ == "__main__":
69+
print(solution())

0 commit comments

Comments
 (0)
Please sign in to comment.