Automatic commit performed through alias...
This commit is contained in:
@@ -52,7 +52,6 @@ def main():
|
|||||||
|
|
||||||
# Calculate intermediate inputs...
|
# Calculate intermediate inputs...
|
||||||
max_factor = 10**(factor_length)-1
|
max_factor = 10**(factor_length)-1
|
||||||
product_of_factors = max_factor * max_factor # Maximum candidate for palindrome test.
|
|
||||||
|
|
||||||
# Initialize loop parameters...
|
# Initialize loop parameters...
|
||||||
n = max_factor
|
n = max_factor
|
||||||
|
|||||||
100
problems/005_problem.py
Executable file
100
problems/005_problem.py
Executable file
@@ -0,0 +1,100 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Problem 4:
|
||||||
|
#
|
||||||
|
# A palindromic number reads the same both ways.
|
||||||
|
# The largest palindrome made from the product
|
||||||
|
# of two 2-digit numbers is 9009 = 91 × 99.
|
||||||
|
#
|
||||||
|
# Find the largest palindrome made from the
|
||||||
|
# product of two 3-digit numbers.
|
||||||
|
#
|
||||||
|
|
||||||
|
import decorators
|
||||||
|
import time
|
||||||
|
|
||||||
|
def is_palindrome(candidate):
|
||||||
|
"""
|
||||||
|
Returns a boolean to confirm if the passed
|
||||||
|
integer is a palindrome.
|
||||||
|
|
||||||
|
is_palindrome(candidate)
|
||||||
|
|
||||||
|
param 'candidate': Integer to test for palindrom-iness.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Convert maximum candidate to a list ...
|
||||||
|
listed_candidate = [int(i) for i in str(candidate)]
|
||||||
|
flipped_candidate = [int(i) for i in str(candidate)]
|
||||||
|
|
||||||
|
# Determine length of maximum candidate, and manipulate.
|
||||||
|
num_digits = listed_candidate.__len__()
|
||||||
|
digit_flips = int(num_digits/2)
|
||||||
|
|
||||||
|
for i in range(1,digit_flips):
|
||||||
|
flipped_candidate[-i-1] = flipped_candidate[i]
|
||||||
|
flipped_candidate[-1] = flipped_candidate[0]
|
||||||
|
|
||||||
|
# Compare if the flipped version and the original
|
||||||
|
# candidate are identical.
|
||||||
|
# The function returns this test result...
|
||||||
|
if listed_candidate == flipped_candidate:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
@decorators.function_timer
|
||||||
|
def main():
|
||||||
|
|
||||||
|
# Define the problem inputs...
|
||||||
|
factor_length = 3
|
||||||
|
|
||||||
|
# Calculate intermediate inputs...
|
||||||
|
max_factor = 10**(factor_length)-1
|
||||||
|
product_of_factors = max_factor * max_factor # Maximum candidate for palindrome test.
|
||||||
|
|
||||||
|
# Initialize loop parameters...
|
||||||
|
n = max_factor
|
||||||
|
m = max_factor
|
||||||
|
n_has_token = False
|
||||||
|
test_passed = False
|
||||||
|
palindrome_list=[]
|
||||||
|
|
||||||
|
|
||||||
|
while (n*m)>1:
|
||||||
|
|
||||||
|
# Loop over all possible pairs of
|
||||||
|
# integers, testing for "palindrom-iness" ...
|
||||||
|
print("{} * {} = {}".format(n,m,n*m))
|
||||||
|
test_passed = is_palindrome(n*m)
|
||||||
|
|
||||||
|
if test_passed:
|
||||||
|
palindrome_list.append(n*m)
|
||||||
|
if n_has_token:
|
||||||
|
n -= 1
|
||||||
|
if n ==0:
|
||||||
|
n_has_token = False
|
||||||
|
n = m-1
|
||||||
|
if not n_has_token:
|
||||||
|
m -= 1
|
||||||
|
if m ==1:
|
||||||
|
n_has_token = True
|
||||||
|
m = n-1
|
||||||
|
|
||||||
|
|
||||||
|
if not test_passed:
|
||||||
|
if n_has_token:
|
||||||
|
n -= 1
|
||||||
|
if n ==0:
|
||||||
|
n_has_token = False
|
||||||
|
n = m-1
|
||||||
|
if not n_has_token:
|
||||||
|
m -= 1
|
||||||
|
if m ==1:
|
||||||
|
n_has_token = True
|
||||||
|
m = n-1
|
||||||
|
|
||||||
|
print("The largest palindrome number which is a product of two numbers of length {} is {} ... ".format(factor_length,max(palindrome_list)))
|
||||||
|
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user