There was an error while loading. Please reload this page.
2 parents bd1a0ad + 4acfcaa commit 676592dCopy full SHA for 676592d
Hashtable/Recaman’s-sequence.py
@@ -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