MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_097/sol1.py:13–39  ·  view source on GitHub ↗

Returns the last n digits of NUMBER. >>> solution() '8739992577' >>> solution(8) '39992577' >>> solution(1) '7' >>> solution(-1) Traceback (most recent call last): ... ValueError: Invalid input >>> solution(8.3) Traceback (most re

(n: int = 10)

Source from the content-addressed store, hash-verified

11
12
13def solution(n: int = 10) -> str:
14 """
15 Returns the last n digits of NUMBER.
16 >>> solution()
17 '8739992577'
18 >>> solution(8)
19 '39992577'
20 >>> solution(1)
21 '7'
22 >>> solution(-1)
23 Traceback (most recent call last):
24 ...
25 ValueError: Invalid input
26 >>> solution(8.3)
27 Traceback (most recent call last):
28 ...
29 ValueError: Invalid input
30 >>> solution("a")
31 Traceback (most recent call last):
32 ...
33 ValueError: Invalid input
34 """
35 if not isinstance(n, int) or n < 0:
36 raise ValueError("Invalid input")
37 modulus = 10**n
38 number = 28433 * (pow(2, 7830457, modulus)) + 1
39 return str(number % modulus)
40
41
42if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected