-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from FranzDiebold/feat/add-solution-for-proble…
…m-53 Add solution for problem 53.
- Loading branch information
Showing
7 changed files
with
145 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
Problem 53: Combinatoric selections | ||
https://projecteuler.net/problem=53 | ||
There are exactly ten ways of selecting three from five, 12345: | ||
123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 | ||
In combinatorics, we use the notation, (5 over 3) = 10. | ||
In general, (n over r) = n! / (r! * (n−r)!), where r <= n, n! = n * (n−1) * ... * 3 * 2 * 1, | ||
and 0! = 1. | ||
It is not until n = 23, that a value exceeds one-million: (23 over 10) = 1144066. | ||
How many, not necessarily distinct, values of (n over r) for 1 <= n <= 100, | ||
are greater than one-million? | ||
""" | ||
|
||
from typing import Iterable, Tuple | ||
|
||
from src.common.calculations import calculate_binomial_coefficient | ||
|
||
|
||
# pylint: disable=invalid-name | ||
def get_large_binomial_coefficients(max_n: int, threshold: int) -> Iterable[Tuple[int, int, int]]: | ||
""" | ||
Get binomial coefficients (n over r) for `1 <= n <= max_n` that are greater than `threshold`. | ||
Returns tuples `(n, r, (n over r))`. | ||
""" | ||
for n in range(1, max_n + 1): | ||
for r in range(n + 1): | ||
binomial_coefficient = calculate_binomial_coefficient(n, r) | ||
if binomial_coefficient > threshold: | ||
yield n, r, binomial_coefficient | ||
|
||
|
||
def main() -> None: | ||
"""Main function.""" | ||
max_n = 100 | ||
threshold = int(1e6) | ||
count = len(list(get_large_binomial_coefficients(max_n, threshold))) | ||
print(f'The number of values of (n over r) for 1 <= n <= {max_n} ' \ | ||
f'that are greater than {threshold:,} is {count}.') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
Problem 53: Combinatoric selections | ||
https://projecteuler.net/problem=53 | ||
There are exactly ten ways of selecting three from five, 12345: | ||
123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 | ||
In combinatorics, we use the notation, (5 over 3) = 10. | ||
In general, (n over r) = n! / (r! * (n−r)!), where r <= n, n! = n * (n−1) * ... * 3 * 2 * 1, | ||
and 0! = 1. | ||
It is not until n = 23, that a value exceeds one-million: (23 over 10) = 1144066. | ||
How many, not necessarily distinct, values of (n over r) for 1 <= n <= 100, | ||
are greater than one-million? | ||
""" | ||
|
||
|
||
def test_get_large_binomial_coefficients(): | ||
# arrange | ||
from src.p053_combinatoric_selections import get_large_binomial_coefficients | ||
|
||
# act | ||
actual_result_iter = get_large_binomial_coefficients(100, int(1e6)) | ||
|
||
# assert | ||
expected_result = (23, 10, 1144066) | ||
assert next(actual_result_iter) == expected_result |