-
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 #9 from FranzDiebold/feat/add-solution-for-problem-48
Add solution for problem 48.
- Loading branch information
Showing
5 changed files
with
119 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Problem 48: Self powers | ||
https://projecteuler.net/problem=48 | ||
The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. | ||
Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. | ||
""" | ||
|
||
from typing import Iterable | ||
import itertools | ||
|
||
from src.common.calculations import calculate_large_power, calculate_large_sum | ||
|
||
|
||
# pylint: disable=invalid-name | ||
def get_self_powers(last_n_digits_only: int = 0) -> Iterable[str]: | ||
"""Get increasing self powers `n^n` (`1^1`, `2^2`, ...) as an Iterable.""" | ||
n = 1 | ||
while True: | ||
yield calculate_large_power(n, n, last_n_digits_only) | ||
n += 1 | ||
|
||
|
||
def get_self_powers_sum(max_n: int, last_n_digits_only: int = 0) -> str: | ||
"""Get the series, `1^1 + 2^2 + ... + max_n^max_n`.""" | ||
return calculate_large_sum(itertools.islice(get_self_powers(last_n_digits_only), max_n)) | ||
|
||
|
||
def main() -> None: | ||
"""Main function.""" | ||
max_n = 1000 | ||
last_n_digits_only = 10 | ||
self_powers_sum = get_self_powers_sum(max_n, last_n_digits_only) | ||
print(f'The last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000 ' \ | ||
f'are {self_powers_sum[-last_n_digits_only:]}.') | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Problem 48: Self powers | ||
https://projecteuler.net/problem=48 | ||
The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. | ||
Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. | ||
""" | ||
|
||
|
||
def test_get_self_powers(): | ||
# arrange | ||
from src.p048_self_powers import get_self_powers | ||
|
||
# act | ||
actual_result_iter = get_self_powers() | ||
|
||
# assert | ||
expected_result = ['1', '4', '27', '256', '3125', '46656', '823543', '16777216'] | ||
for expected_self_power in expected_result: | ||
assert next(actual_result_iter) == expected_self_power | ||
|
||
|
||
def test_get_self_powers_sum(): | ||
# arrange | ||
from src.p048_self_powers import get_self_powers_sum | ||
|
||
# act | ||
actual_result = get_self_powers_sum(10) | ||
|
||
# assert | ||
expected_result = '10405071317' | ||
assert actual_result == expected_result | ||
|
||
|
||
def test_get_self_powers_sum_with_last_n_digits_only(): | ||
# arrange | ||
from src.p048_self_powers import get_self_powers_sum | ||
|
||
# act | ||
actual_result = get_self_powers_sum(10, 5) | ||
|
||
# assert | ||
expected_result = '71317' | ||
assert actual_result.endswith(expected_result) |