Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. print 'So when was Ernie born?'
  2.  
  3. # determines if a number is a perfect square
  4. def is_square(apositiveint):
  5. x = apositiveint // 2
  6. seen = set([x])
  7. while x * x != apositiveint:
  8. x = (x + (apositiveint // x)) // 2
  9. if x in seen: return False
  10. seen.add(x)
  11. return True
  12.  
  13. # determines if a number is prime
  14. def isprime(n):
  15. '''check if integer n is a prime'''
  16. # make sure n is a positive integer
  17. n = abs(int(n))
  18. # 0 and 1 are not primes
  19. if n < 2:
  20. return False
  21. # 2 is the only even prime number
  22. if n == 2:
  23. return True
  24. # all other even numbers are not primes
  25. if not n & 1:
  26. return False
  27. # range starts with 3 and only needs to go up the squareroot of n
  28. # for all odd numbers
  29. for x in range(3, int(n**0.5)+1, 2):
  30. if n % x == 0:
  31. return False
  32. return True
  33.  
  34. # This is where the magic happens...
  35. days = xrange(1, 32) # 1-31
  36. months = xrange(1,13) # 1-12
  37. years = xrange(1900, 2020) # 1900-2019
  38. ages = xrange(10,99) # 10-98
  39.  
  40. # These are possible dates that might solve the riddle
  41. possibleDates = []
  42.  
  43. # boolean flag
  44. dateIsBad = False
  45.  
  46. for year in years:
  47. for month in months:
  48. for day in days:
  49. for age in ages:
  50.  
  51. date = str(day) + ' ' + str(month) + ' ' + str(year)
  52. ageAndDate = 'age = ' + str(age) + ' date = ' + date
  53.  
  54. concat1 = int(str(day) + str(month) + str(year))
  55. if concat1 % 7 != 0:
  56. dateIsBad = True
  57.  
  58. concat2 = int(str(year) + str(month) + str(day))
  59. if concat2 % 11 != 0:
  60. dateIsBad = True
  61.  
  62. concat3 = int(str(month) + str(day) + str(year))
  63. if concat3 % 13 != 0:
  64. dateIsBad = True
  65.  
  66. number1 = day + month + year
  67. if isprime(number1) and is_square(number1 + age):
  68. pass
  69. else:
  70. dateIsBad = True
  71.  
  72. if not dateIsBad:
  73. print 'found one! ' + ageAndDate
  74. possibleDates.append(ageAndDate)
  75.  
  76. # reset the boolean flag
  77. dateIsBad = False
  78.  
  79. print possibleDates
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement