Skip to content

Commit 676592d

Browse files
Merge pull request codemistic#489 from Aman5989/patch-21
Create Recaman’s-sequence.py
2 parents bd1a0ad + 4acfcaa commit 676592d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Hashtable/Recaman’s-sequence.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Python 3 program to print n-th
2+
# number in Recaman's sequence
3+
4+
# Prints first n terms of Recaman
5+
# sequence
6+
def recaman(n):
7+
8+
# Create an array to store terms
9+
arr = [0] * n
10+
11+
# First term of the sequence
12+
# is always 0
13+
arr[0] = 0
14+
print(arr[0], end=", ")
15+
16+
# Fill remaining terms using
17+
# recursive formula.
18+
for i in range(1, n):
19+
20+
curr = arr[i-1] - i
21+
for j in range(0, i):
22+
23+
# If arr[i-1] - i is
24+
# negative or already
25+
# exists.
26+
if ((arr[j] == curr) or curr < 0):
27+
curr = arr[i-1] + i
28+
break
29+
30+
arr[i] = curr
31+
print(arr[i], end=", ")
32+
33+
# Driver code
34+
n = 17
35+
36+
recaman(n)
37+

0 commit comments

Comments
 (0)