Updated problem_04 in project_euler (#2427)

* Updated problem_04 in project_euler

* fixup! Format Python code with psf/black push

* That number is larger than our acceptable range.

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Du Yuanchao
2020-09-15 04:33:08 +08:00
committed by GitHub
parent 10aa214fcb
commit cbbc43ba3a
2 changed files with 20 additions and 5 deletions

View File

@ -18,25 +18,34 @@ def solution(n):
29992
>>> solution(40000)
39893
>>> solution(10000)
Traceback (most recent call last):
...
ValueError: That number is larger than our acceptable range.
"""
# fetches the next number
for number in range(n - 1, 10000, -1):
for number in range(n - 1, 9999, -1):
# converts number into string.
strNumber = str(number)
str_number = str(number)
# checks whether 'strNumber' is a palindrome.
if strNumber == strNumber[::-1]:
# checks whether 'str_number' is a palindrome.
if str_number == str_number[::-1]:
divisor = 999
# if 'number' is a product of two 3-digit numbers
# then number is the answer otherwise fetch next number.
while divisor != 99:
if (number % divisor == 0) and (len(str(int(number / divisor))) == 3):
if (number % divisor == 0) and (len(str(number // divisor)) == 3.0):
return number
divisor -= 1
raise ValueError("That number is larger than our acceptable range.")
if __name__ == "__main__":
import doctest
doctest.testmod()
print(solution(int(input().strip())))