Find n-th element from Stern’s Diatomic Series
Given an integer n. we have to find the nth term of Stern’s Diatomic Series.
Stern’s diatomic series is the sequence which generates the following integer sequence 0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, ……. It arises in the Calkin-Wilf tree. It is sometimes also known as the fusc function.
In mathematical terms, the sequence P(n) of Stern’s diatomic series is defined by the recurrence relation.

Examples :
Input : n = 7 Output : 3 Input : n = 15 Output : 4
Approach :
We solve this problem with a very simple concept of Dynamic programming which is used in finding fibonacci numbers. After saving the base case of DP[0] = 0, DP[1] = 1, we have to simply traverse from i = 2 to n and compute DP[i] as per explained definition of Stern’s diatomic series. And finally return the value of DP[n].
Algorithm :
// SET the Base case
DP[0] = 0;
DP[1] = 1;
// Traversing the array from 2nd Element to nth Element
for (int i=2; i<=n; i++)
{
// Case 1: for even n
if (i%2 == 0)
DP[i] = DP[i/2];
// Case 2: for odd n
else
DP[i] = DP[(i-1)/2] + DP[(i+1)/2];
}
return DP[n];
C++
// Program to find the nth element // of Stern's Diatomic Series #include <bits/stdc++.h> using namespace std; // function to find nth stern' // diatomic series int findSDSFunc(int n) { // Initializing the DP array int DP[n+1]; // SET the Base case DP[0] = 0; DP[1] = 1; // Traversing the array from // 2nd Element to nth Element for (int i = 2; i <= n; i++) { // Case 1: for even n if (i % 2 == 0) DP[i] = DP[i / 2]; // Case 2: for odd n else DP[i] = DP[(i - 1) / 2] + DP[(i + 1) / 2]; } return DP[n]; } // Driver program int main() { int n = 15; cout << findSDSFunc(n) << endl; return 0; } |
Java
// Java program to find the nth element // of Stern's Diatomic Series class GFG { // function to find nth stern' // diatomic series static int findSDSFunc(int n) { // Initializing the DP array int DP[] = new int[n+1]; // SET the Base case DP[0] = 0; DP[1] = 1; // Traversing the array from // 2nd Element to nth Element for (int i = 2; i <= n; i++) { // Case 1: for even n if (i % 2 == 0) DP[i] = DP[i / 2]; // Case 2: for odd n else DP[i] = DP[(i - 1) / 2] + DP[(i + 1) / 2]; } return DP[n]; } // Driver program public static void main(String[] args) { int n = 15; System.out.println(findSDSFunc(n)); } } // This code is contributed by Smita Semwal. |
Python 3
# Program to find the nth element # of Stern's Diatomic Series # function to find nth stern' # diatomic series def findSDSFunc(n): # Initializing the DP array DP = [0] * (n+1) # SET the Base case DP[0] = 0 DP[1] = 1 # Traversing the array from # 2nd Element to nth Element for i in range(2, n+1): # Case 1: for even n if (int(i % 2) == 0): DP[i] = DP[int(i / 2)] # Case 2: for odd n else: DP[i] = (DP[int((i - 1) / 2)] + DP[int((i + 1) / 2)]) return DP[n] # Driver program n = 15 print(findSDSFunc(n)) # This code is contribute by # Smitha Dinesh Semwal |
C#
// C# program to find the nth element // of Stern's Diatomic Series using System; class GFG { // function to find nth // stern' diatomic series static int findSDSFunc(int n) { // Initializing the DP array int []DP = new int[n + 1]; // SET the Base case DP[0] = 0; DP[1] = 1; // Traversing the array from // 2nd Element to nth Element for (int i = 2; i <= n; i++) { // Case 1: for even n if (i % 2 == 0) DP[i] = DP[i / 2]; // Case 2: for odd n else DP[i] = DP[(i - 1) / 2] + DP[(i + 1) / 2]; } return DP[n]; } // Driver Code static public void Main () { int n = 15; Console.WriteLine(findSDSFunc(n)); } } // This code is contributed by aj_36 |
PHP
<?php // PHP Program to find the nth element // of Stern's Diatomic Series // function to find nth stern' // diatomic series function findSDSFunc($n) { // SET the Base case $DP[0] = 0; $DP[1] = 1; // Traversing the array from // 2nd Element to nth Element for ($i = 2; $i <= $n; $i++) { // Case 1: for even n if ($i % 2 == 0) $DP[$i] = $DP[$i / 2]; // Case 2: for odd n else $DP[$i] = $DP[($i - 1) / 2] + $DP[($i + 1) / 2]; } return $DP[$n]; } // Driver Code $n = 15; echo(findSDSFunc($n)); // This code is contributed by Ajit. ?> |
4
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Find n-th element in a series with only 2 digits (4 and 7) allowed
- Find the Nth element of the modified Fibonacci series
- Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)
- Factorial of each element in Fibonacci series
- Find the sum of series 3, 7, 13, 21, 31....
- Find the sum of the series 2, 5, 13, 35, 97...
- Find the sum of the series x(x+y) + x^2(x^2+y^2) +x^3(x^3+y^3)+ ... + x^n(x^n+y^n)
- Find sum of the series 1-2+3-4+5-6+7.......
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Find nth term of the series 5 2 13 41
- Find the sum of infinite series 1^2.x^0 + 2^2.x^1 + 3^2.x^2 + 4^2.x^3 +.......
- Find Nth term of the series 0, 6, 0, 12, 0, 90...
- Find the sum of n terms of the series 1,8,27,64 ....
- Find the Nth term of the series 2, 15, 41, 80, 132...
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

