[ create a new paste ] login | about

Link: http://codepad.org/wD9u5sHW    [ raw code | output | fork | 1 comment ]

tenten321 - Python, pasted on Dec 30:
#Project Euler Problem 4

import time
start = time.time()
global bigO 
bigO = 0

n=999*999
pal=0

def is_pal(n):
  nstr = str(n)
  if nstr == nstr[::-1]:
    #print nstr + " is a palindrome."
    return 1
  else:
    return 0

def has_3digit_factors(n):
  global bigO
  for i in range(999,100,-1):
    bigO += 1
    if n % i == 0:
      if len(str(n/i)) == 3:
        #print str(n) + " is composed of two 3-digit factors: " + str(i) + ", " + str(n/i)
        return n
  return 0

while pal == 0:
  bigO += 1
  if n%11 == 0 and is_pal(n):
    pal = has_3digit_factors(n)
  n -= 1

print "The largest palindromic number that is the product of two 3 digit numbers is: ",pal

print "Iterations: ", bigO
print "Total elapsed time: " + str((time.time()-start)*1000) + " milliseconds."


Output:
1
2
3
The largest palindromic number that is the product of two 3 digit numbers is:  906609
Iterations:  173209
Total elapsed time: 169.070005417 milliseconds.


Create a new paste based on this one


Comments:
posted by tenten321 on Dec 30
169 ms runtime
reply