recaman sequence in Python code example
Example 1: recaman sequence in python
def recaman(n):
if not isinstance(n, int):
raise TypeError("%r is not an integer" %n)
elif n < 0:
raise ValueError("%d is negative" %n)
elif n == 0:
return 0
else:
lst = [recaman(k) for k in range(n)]
if (recaman(n-1)-n < 0) or (recaman(n-1)-n in lst):
return recaman(n-1)+n
else:
return recaman(n-1)-n
for i in range(1, 11):
print(recaman(i))
Example 2: recaman sequence in Python
def recaman(n):
if not isinstance(n, int):
raise TypeError("%r is not an integer" %n)
elif n < 0:
raise ValueError("%d is negative" %n)
elif n == 0:
return 0
else:
lst = [recaman(k) for k in range(n)]
if (recaman(n-1)-n < 0) or (recaman(n-1)-n in lst):
return recaman(n-1)+n
else:
return recaman(n-1)-n
for i in range(1, 11):
print(recaman(i))