recaman sequence in python

Solutions on MaxInterview for recaman sequence in python by the best coders in the world

showing results for - "recaman sequence in python"
Jona
14 Oct 2018
1def recaman(n):
2    if not isinstance(n, int):
3        raise TypeError("%r is not an integer" %n)
4    elif n < 0:
5        raise ValueError("%d is negative" %n)
6    elif n == 0:
7        return 0
8    else:
9        lst = [recaman(k) for k in range(n)]
10        if (recaman(n-1)-n < 0) or (recaman(n-1)-n in lst):
11            return recaman(n-1)+n
12        else:
13            return recaman(n-1)-n
14        
15for i in range(1, 11):
16    print(recaman(i))
Lotta
22 Sep 2020
1
2def recaman(n):
3    if not isinstance(n, int):
4        raise TypeError("%r is not an integer" %n)
5    elif n < 0:
6        raise ValueError("%d is negative" %n)
7    elif n == 0:
8        return 0
9    else:
10        lst = [recaman(k) for k in range(n)]
11        if (recaman(n-1)-n < 0) or (recaman(n-1)-n in lst):
12            return recaman(n-1)+n
13        else:
14            return recaman(n-1)-n
15        
16for i in range(1, 11):
17    print(recaman(i))