scrimba
Learn Python
Bonus: Project Euler Q4 - Palindromes 2
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Bonus: Project Euler Q4 - Palindromes 2
AboutCommentsNotes
Bonus: Project Euler Q4 - Palindromes 2
Expand for more info
index.py
run
preview
console
#project Euler #4 - largest palindrome of two 3 - digit numbers 
# a palindrome is a number that is the same backwards and forwards, like 101 or 990099

import time

def is_palindrome(val):
val = str(val)
if val == val[::-1]:
return(True)
else:
return(False)
#def is_palindrome(val):
# return str(val) == str(val)[::-1]

def palindrome():
start_time = time.time()
palindromes_list=[]
debug_list=[]
low_val =900
high_val = 999
iterations = 0

for num1 in range(low_val,high_val):
for num2 in range(low_val,high_val):
iterations += 1
#print(num1,num2)
if is_palindrome(num1*num2):
palindromes_list.append(num1*num2)
debug_list.append([num1,num2,num1*num2])
if num1 == num2:
break
print('print of palindromes:',palindromes_list, num1, num2)
print('debug_list:', debug_list)
print('Iterations:' , iterations)
print('Largest palindrome:', max(palindromes_list))
print('Runtime:', time.time()-start_time)
print('---------End Run--------') #110 seconds, 55 secs

def palindrome_back():
start_time = time.time()
palindromes_list=[]
debug_list=[]
low_val =500
high_val = 2000
iterations = 0
low_num2_val =500

for num1 in range(high_val,low_val,-1):
if num1 < low_val:
break
for num2 in range(high_val,low_num2_val,-1):
iterations += 1
#print(num1,num2)
if is_palindrome(num1*num2):
palindromes_list.append(num1*num2)
low_val = max((num1*num2)/high_val,low_val)
low_num2_val = num2
debug_list.append([num1,num2,(num1*num2)/high_val,low_val])
if num1 == num2:
break
print('print of palindromes:',palindromes_list, num1, num2)
print('debug_list:', debug_list)
print('Iterations:' , iterations)
print('Largest palindrome:', max(palindromes_list))
print('Runtime:', time.time()-start_time)
print('---------End Run--------')

#110 seconds, 55 secs, 0.6 sec ver 1, 1.5 and 2


palindrome()
palindrome_back()
Console
â€ș
"using indexedDB for stdlib modules cache"
,
â€ș
"print of palindromes: [819918, 824428, 861168, 888888, 886688, 906609] 998 998 "
,
â€ș
"debug_list: [[909, 902, 819918], [914, 902, 824428], [932, 924, 861168], [962, 924, 888888], [968, 916, 886688], [993, 913, 906609]] "
,
â€ș
"Iterations: 4950 "
,
â€ș
"Largest palindrome: 906609 "
,
â€ș
"Runtime: 0.8689999580383301 "
,
â€ș
"---------End Run-------- "
,
â€ș
"print of palindromes: [3810183, 3848483] 1924 1994 "
,
â€ș
"debug_list: [[1941, 1963, 1905.0915, 1905.0915], [1931, 1993, 1924.2415, 1924.2415]] "
,
â€ș
"Iterations: 2242 "
,
â€ș
"Largest palindrome: 3848483 "
,
â€ș
"Runtime: 0.4499998092651367 "
,
â€ș
"---------End Run-------- "
,
/index.html
-7:49