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

Function solution

project_euler/problem_004/sol1.py:16–47  ·  view source on GitHub ↗

Returns the largest palindrome made from the product of two 3-digit numbers which is less than n. >>> solution(20000) 19591 >>> solution(30000) 29992 >>> solution(40000) 39893 >>> solution(10000) Traceback (most recent call last): ... ValueError:

(n: int = 998001)

Source from the content-addressed store, hash-verified

14
15
16def solution(n: int = 998001) -> int:
17 """
18 Returns the largest palindrome made from the product of two 3-digit
19 numbers which is less than n.
20
21 >>> solution(20000)
22 19591
23 >>> solution(30000)
24 29992
25 >>> solution(40000)
26 39893
27 >>> solution(10000)
28 Traceback (most recent call last):
29 ...
30 ValueError: That number is larger than our acceptable range.
31 """
32
33 # fetches the next number
34 for number in range(n - 1, 9999, -1):
35 str_number = str(number)
36
37 # checks whether 'str_number' is a palindrome.
38 if str_number == str_number[::-1]:
39 divisor = 999
40
41 # if 'number' is a product of two 3-digit numbers
42 # then number is the answer otherwise fetch next number.
43 while divisor != 99:
44 if (number % divisor == 0) and (len(str(number // divisor)) == 3.0):
45 return number
46 divisor -= 1
47 raise ValueError("That number is larger than our acceptable range.")
48
49
50if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected