fork(1) download
  1. import math,time
  2.  
  3. def tevemadar_version(maxInt):
  4. maxpal=0
  5. for x in range(maxInt,0,-1):
  6. if x*x<maxpal: # 4.
  7. break
  8. for y in range(x,max(maxpal//x,10**int(math.log10(x))-1),-1): # 1. 3. 5.
  9. num=x*y
  10. if str(num) == str(num)[::-1]:
  11. maxpal=num
  12. break # 2.
  13. return maxpal
  14.  
  15. def mushif_version(maxInt):
  16. largest_palindrome = 9
  17. digit_count = len(str(maxInt))
  18. a = maxInt
  19. while a >= 10**(digit_count-1):
  20. if a % 11 == 0:
  21. b = maxInt
  22. divided_by = 1
  23. else:
  24. b = maxInt - maxInt % 11
  25. divided_by = 11
  26.  
  27. while b >= a:
  28. if a * b <= largest_palindrome:
  29. break
  30. prod = a * b
  31. str_num = str(prod)
  32. if str_num == str_num[::-1]:
  33. largest_palindrome = prod
  34.  
  35. b = b - divided_by
  36. a = a - 1
  37. return largest_palindrome
  38.  
  39.  
  40. print("tevemadar_version:")
  41. start=time.time()
  42. print(tevemadar_version(9))
  43. print(tevemadar_version(99))
  44. print(tevemadar_version(999))
  45. print(tevemadar_version(9999))
  46. print(tevemadar_version(99999))
  47. print(tevemadar_version(999999))
  48. print("{0} seconds".format(time.time()-start))
  49.  
  50. start=time.time()
  51. print("\nmushif_version:")
  52. print(mushif_version(9))
  53. print(mushif_version(99))
  54. print(mushif_version(999))
  55. print(mushif_version(9999))
  56. print(mushif_version(99999))
  57. print(mushif_version(999999))
  58. print("{0} seconds".format(time.time()-start))
Success #stdin #stdout 1.21s 9432KB
stdin
Standard input is empty
stdout
tevemadar_version:
9
9009
906609
99000099
9966006699
999000000999
0.7758157253265381 seconds

mushif_version:
9
9009
906609
99000099
9966006699
999000000999
0.42333054542541504 seconds