Answers for "recaman sequence in Python"

0

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))
Posted by: Guest on May-25-2021
0

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))
Posted by: Guest on May-25-2021

Browse Popular Code Answers by Language