Find the n-th Element from Stern's Diatomic Series in C++

A series of numbers known as Stern's Diatomic Series which is derived from the sum of the two numbers that came before it. 0 and 1 are the starting numbers, while the next numbers are produced by adding the last two. For example:

0, 1, 1, 2, 3, 2, 3, 4, 5, 3, 4, 5, 6, 7,... It is sometimes also known as the fusc Function.

Approach:

We use the relatively basic idea of dynamic programming to discover the solution to this problem. It is easy to compute series[i] by traversing from q = 2 to num and saving the base case of series[0] = 0, series[1] = 1. It is done by the definition of Stern's diatomic series. After that, return the series[n]'s value at last. It is appropriate for various applications and analyses because it provides a systematic method to generate and retrieve series elements.

Approach to Find the n-th Element

  1. Initializing the Series: Initially, we are initializing a vector to store the series's elements. The vector's initial two elements, 0 and 1, are added.
  2. Generating the Series: We use a loop to generate the series up to the n-th Element. We add the two elements before it to calculate each successive Element inside the loop. After that, this new Element is pushed into the vector.
  3. Returning the n-th Element: After the series is formed up to that point, we return the n-th Element's value from the vector.

Formula

The recurrence relation functions as a mathematical definition of the sequence series(num) of Stern's diatomic series.

For base case

  • If num =0 value at position, num is 0
  • If num =1 value at position, num is 1

For iterative case

  • If num is even, series[num]=series[num/2]
  • If num is odd, series[num]=series[(num-1)/2] + series[(num+1)/2]

Algorithm:

Example:

Let us take an example to illustrate how to find nth element from stern's Diatomic Series in C++.

Output:

To determine the n-th Element in Stern's Diatomic Series, enter the value of num: 7
The 7-th Element in Stern's Diatomic Series is: 3

Explanation:

In this example, the TofindSDSFunction function is defined code to calculate the n-th Element of Stern's Diatomic Series through dynamic programming. With the first Element set to 1, it initializes a vector series for storing series elements. Subsequent elements are computed iteratively depending on whether q is even or odd. Ultimately, the specified Function computes the n-th Element and prints the result along with a descriptive message in the main Function, allowing the user to input the value of num.

Complexity Analysis:

Time Complexity

  • The time complexity is O(n).
  • It is because we loop once across the range from 2 to n to calculate every Element in the series.

Space Complexity

  • The space complexity is also O(n).
  • We use a vector of size n+1 to store the elements of the series.
  • Additionally, we use a constant amount of extra space for variables like num in the main Function.

Time and space complexity increases linearly with the input value n. This algorithm is efficient and can handle large values of n without significant overhead.






Latest Courses