mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 09:10:16 +00:00
756bb268eb
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/psf/black: 22.6.0 → 22.8.0](https://github.com/psf/black/compare/22.6.0...22.8.0) - [github.com/asottile/pyupgrade: v2.37.0 → v2.38.2](https://github.com/asottile/pyupgrade/compare/v2.37.0...v2.38.2) - https://gitlab.com/pycqa/flake8 → https://github.com/PyCQA/flake8 - [github.com/PyCQA/flake8: 3.9.2 → 5.0.4](https://github.com/PyCQA/flake8/compare/3.9.2...5.0.4) - [github.com/pre-commit/mirrors-mypy: v0.961 → v0.981](https://github.com/pre-commit/mirrors-mypy/compare/v0.961...v0.981) - [github.com/codespell-project/codespell: v2.1.0 → v2.2.1](https://github.com/codespell-project/codespell/compare/v2.1.0...v2.2.1) * Fix a long line * Update sol1.py * Update sol1.py * lambda_ * Update multi_level_feedback_queue.py * Update double_ended_queue.py * Update sequential_minimum_optimization.py * Update .pre-commit-config.yaml Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
76 lines
1.8 KiB
Python
76 lines
1.8 KiB
Python
"""
|
|
Project Euler Problem 113: https://projecteuler.net/problem=113
|
|
|
|
Working from left-to-right if no digit is exceeded by the digit to its left it is
|
|
called an increasing number; for example, 134468.
|
|
|
|
Similarly if no digit is exceeded by the digit to its right it is called a decreasing
|
|
number; for example, 66420.
|
|
|
|
We shall call a positive integer that is neither increasing nor decreasing a
|
|
"bouncy" number; for example, 155349.
|
|
|
|
As n increases, the proportion of bouncy numbers below n increases such that there
|
|
are only 12951 numbers below one-million that are not bouncy and only 277032
|
|
non-bouncy numbers below 10^10.
|
|
|
|
How many numbers below a googol (10^100) are not bouncy?
|
|
"""
|
|
|
|
|
|
def choose(n: int, r: int) -> int:
|
|
"""
|
|
Calculate the binomial coefficient c(n,r) using the multiplicative formula.
|
|
>>> choose(4,2)
|
|
6
|
|
>>> choose(5,3)
|
|
10
|
|
>>> choose(20,6)
|
|
38760
|
|
"""
|
|
ret = 1.0
|
|
for i in range(1, r + 1):
|
|
ret *= (n + 1 - i) / i
|
|
return round(ret)
|
|
|
|
|
|
def non_bouncy_exact(n: int) -> int:
|
|
"""
|
|
Calculate the number of non-bouncy numbers with at most n digits.
|
|
>>> non_bouncy_exact(1)
|
|
9
|
|
>>> non_bouncy_exact(6)
|
|
7998
|
|
>>> non_bouncy_exact(10)
|
|
136126
|
|
"""
|
|
return choose(8 + n, n) + choose(9 + n, n) - 10
|
|
|
|
|
|
def non_bouncy_upto(n: int) -> int:
|
|
"""
|
|
Calculate the number of non-bouncy numbers with at most n digits.
|
|
>>> non_bouncy_upto(1)
|
|
9
|
|
>>> non_bouncy_upto(6)
|
|
12951
|
|
>>> non_bouncy_upto(10)
|
|
277032
|
|
"""
|
|
return sum(non_bouncy_exact(i) for i in range(1, n + 1))
|
|
|
|
|
|
def solution(num_digits: int = 100) -> int:
|
|
"""
|
|
Calculate the number of non-bouncy numbers less than a googol.
|
|
>>> solution(6)
|
|
12951
|
|
>>> solution(10)
|
|
277032
|
|
"""
|
|
return non_bouncy_upto(num_digits)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(f"{solution() = }")
|