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

Function solution

project_euler/problem_004/sol2.py:16–35  ·  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

(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 """
28
29 answer = 0
30 for i in range(999, 99, -1): # 3 digit numbers range from 999 down to 100
31 for j in range(999, 99, -1):
32 product_string = str(i * j)
33 if product_string == product_string[::-1] and i * j < n:
34 answer = max(answer, i * j)
35 return answer
36
37
38if __name__ == "__main__":

Callers 1

sol2.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected