Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print 'So when was Ernie born?'
- # determines if a number is a perfect square
- def is_square(apositiveint):
- x = apositiveint // 2
- seen = set([x])
- while x * x != apositiveint:
- x = (x + (apositiveint // x)) // 2
- if x in seen: return False
- seen.add(x)
- return True
- # determines if a number is prime
- def isprime(n):
- '''check if integer n is a prime'''
- # make sure n is a positive integer
- n = abs(int(n))
- # 0 and 1 are not primes
- if n < 2:
- return False
- # 2 is the only even prime number
- if n == 2:
- return True
- # all other even numbers are not primes
- if not n & 1:
- return False
- # range starts with 3 and only needs to go up the squareroot of n
- # for all odd numbers
- for x in range(3, int(n**0.5)+1, 2):
- if n % x == 0:
- return False
- return True
- # This is where the magic happens...
- days = xrange(1, 32) # 1-31
- months = xrange(1,13) # 1-12
- years = xrange(1900, 2020) # 1900-2019
- ages = xrange(10,99) # 10-98
- # These are possible dates that might solve the riddle
- possibleDates = []
- # boolean flag
- dateIsBad = False
- for year in years:
- for month in months:
- for day in days:
- for age in ages:
- date = str(day) + ' ' + str(month) + ' ' + str(year)
- ageAndDate = 'age = ' + str(age) + ' date = ' + date
- concat1 = int(str(day) + str(month) + str(year))
- if concat1 % 7 != 0:
- dateIsBad = True
- concat2 = int(str(year) + str(month) + str(day))
- if concat2 % 11 != 0:
- dateIsBad = True
- concat3 = int(str(month) + str(day) + str(year))
- if concat3 % 13 != 0:
- dateIsBad = True
- number1 = day + month + year
- if isprime(number1) and is_square(number1 + age):
- pass
- else:
- dateIsBad = True
- if not dateIsBad:
- print 'found one! ' + ageAndDate
- possibleDates.append(ageAndDate)
- # reset the boolean flag
- dateIsBad = False
- print possibleDates
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement