Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Perfect Square solution in Uncategorized category for Ghosts Age by goonbee
from math import sqrt
def is_perfect_square(n):
s = int(sqrt(n))
return s*s == n
def is_fibonacci_number(n):
return is_perfect_square(5*n*n + 4) or is_perfect_square(5*n*n - 4);
def checkio(opacity):
opacity_counter = 10000
for age in range(1, 5000):
if opacity_counter == opacity:
return age-1
if is_fibonacci_number(age):
opacity_counter -= age
else:
opacity_counter += 1
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(10000) == 0, "Newborn"
assert checkio(9999) == 1, "1 year"
assert checkio(9997) == 2, "2 years"
assert checkio(9994) == 3, "3 years"
assert checkio(9995) == 4, "4 years"
assert checkio(9990) == 5, "5 years"
Aug. 23, 2015
Comments: