-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
sterns_diatomic_series.jl
52 lines (43 loc) · 1.19 KB
/
sterns_diatomic_series.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Julia program to find the n-th element from Stern's Diatomic Series
Stern's diatomic series is the sequence which generates the integer sequence that arises
in the Calkin-Wilf tree. This sequence can be computed by the fusc function.
"""
function stern_diatomic_num(n)
if(n == 0)
return 0
end
dp = zeros(Int, n)
dp[1] = 1
# Traverse and Fill the `dp` array.
for i in 2:n
# If i is even
if(i % 2 == 0)
dp[i] = dp[i ÷ 2]
# If i is odd
else
dp[i] = dp[(i - 1) ÷ 2 ] + dp[(i + 1) ÷ 2]
end
end
return dp[n]
end
print("Enter the value of n(where you need the nth Stern's Diatomic number): ")
n = readline()
n = parse(Int, n)
if(n < 0)
print("Invalid Value of n !!!")
exit()
end
res = stern_diatomic_num(n)
println("The $n'th Stern's Diatomic is $res.")
"""
Time Complexity: O(n), where 'n' is the given number
Space Complexity: O(1)
SAMPLE INPUT AND OUTPUT
SAMPLE 1
Enter the value of n(where you need the nth Stern's Diatomic number): 258
The 258'th Stern's Diatomic is 8.
SAMPLE 2
Enter the value of n(where you need the nth Stern's Diatomic number): -98
Invalid Value of n !!!
"""