Skip to content
Related Articles

Related Articles

Program to find Nth term of the Van Eck’s Sequence
  • Last Updated : 07 May, 2021

Given a positive integer N, the task is to print Nth term of the Van Eck’s sequence.
In mathematics, Van Eck’s sequence is an integer sequence which is defined recursively as follows: 
 

  • Let the first term be 0 i.e a0 = 0.
  • Then for n >= 0, if there exists an m < n such that
am = an
  • take the largest such m and set an+1 = n − m;
  • Otherwise an+1 = 0.
  • Start with a(1)=0.

First few terms of Van Eck’s Sequence are as follows: 
 

0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5 … 
 

Example: 
 

Input: N = 5
Output: 2

Input: N = 10
Output: 6 

Approach: 
As described above we can follow the below steps to generate Van Eck’s sequence: 
 



  • Set the first term of the sequence as 0.
  • Then Repeatedly apply: 
    • If the last term has not occurred yet and is new to the sequence so far then, set the next term as zero.
    • Otherwise, the next term is how far back this last term has occurred previously.
  • Once the sequence is generated we can get our nth term easily.

Below is the implementation of above approach:
 

C++




// C++ program to print Nth
// term of Van Eck's sequence
 
#include <bits/stdc++.h>
using namespace std;
 
#define MAX 1000
int sequence[MAX];
 
// Utility function to compute
// Van Eck's sequence
void vanEckSequence()
{
 
    // Initialize sequence array
    for (int i = 0; i < MAX; i++) {
        sequence[i] = 0;
    }
 
    // Loop to generate sequence
    for (int i = 0; i < MAX; i++) {
 
        // Check if sequence[i] has occured
        // previousely or is new to sequence
        for (int j = i - 1; j >= 0; j--) {
            if (sequence[j] == sequence[i]) {
 
                // If occurrence found
                // then the next term will be
                // how far back this last term
                // occured previousely
                sequence[i + 1] = i - j;
                break;
            }
        }
    }
}
 
// Utility function to return
// Nth term of sequence
int getNthTerm(int n)
{
 
    return sequence[n];
}
 
// Driver code
int main()
{
 
    // Pre-compute Van Eck's sequence
    vanEckSequence();
 
    int n = 6;
 
    // Print nth term of the sequence
    cout << getNthTerm(n) << endl;
 
    n = 100;
 
    // Print nth term of the sequence
    cout << getNthTerm(n) << endl;
 
    return 0;
}

Java




// Java program to print Nth
// term of Van Eck's sequence
 
class GFG {
 
    static int MAX = 1000;
 
    // Array to store terms of sequence
    static int sequence[] = new int[MAX];
 
    // Utility function to compute
    // Van Eck's sequence
    static void vanEckSequence()
    {
 
        // Initialize sequence array
        for (int i = 0; i < MAX - 1; i++) {
            sequence[i] = 0;
        }
 
        // Loop to generate sequence
        for (int i = 0; i < MAX - 1; i++) {
 
            // Check if sequence[i] has occured
            // previousely or is new to sequence
            for (int j = i - 1; j >= 0; j--) {
                if (sequence[j] == sequence[i]) {
 
                    // If occurrence found
                    // then the next term will be
                    // how far back this last term
                    // occured previousely
                    sequence[i + 1] = i - j;
                    break;
                }
            }
        }
    }
 
    // Utility function to return
    // Nth term of sequence
    static int getNthTerm(int n)
    {
 
        return sequence[n];
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Pre-compute Van Eck's sequence
        vanEckSequence();
 
        int n = 6;
 
        // Print nth term of the sequence
        System.out.println(getNthTerm(n));
 
        n = 100;
 
        // Print nth term of the sequence
        System.out.println(getNthTerm(n));
    }
}

Python3




# Python3 program to print Nth
# term of Van Eck's sequence
MAX = 1000
sequence = [0] * (MAX + 1);
 
# Utility function to compute
# Van Eck's sequence
def vanEckSequence() :
 
    # Initialize sequence array
    for i in range(MAX) :
        sequence[i] = 0;
 
    # Loop to generate sequence
    for i in range(MAX) :
         
        # Check if sequence[i] has occured
        # previousely or is new to sequence
        for j in range(i - 1 , -1, -1) :
            if (sequence[j] == sequence[i]) :
 
                # If occurrence found
                # then the next term will be
                # how far back this last term
                # occured previousely
                sequence[i + 1] = i - j;
                break;
 
# Utility function to return
# Nth term of sequence
def getNthTerm(n) :
 
    return sequence[n];
 
# Driver code
if __name__ == "__main__" :
 
    # Pre-compute Van Eck's sequence
    vanEckSequence();
 
    n = 6;
 
    # Print nth term of the sequence
    print(getNthTerm(n));
 
    n = 100;
 
    # Print nth term of the sequence
    print(getNthTerm(n));
 
# This code is contributed by kanugargng

C#




// C# program to print Nth
// term of Van Eck's sequence
 
using System;
class GFG {
 
    static int MAX = 1000;
 
    // Array to store terms of sequence
    static int[] sequence = new int[MAX];
 
    // Utility function to compute
    // Van Eck's sequence
    static void vanEckSequence()
    {
 
        // Initialize sequence array
        for (int i = 0; i < MAX - 1; i++) {
            sequence[i] = 0;
        }
 
        // Loop to generate sequence
        for (int i = 0; i < MAX - 1; i++) {
 
            // Check if sequence[i] has occured
            // previousely or is new to sequence
            for (int j = i - 1; j >= 0; j--) {
                if (sequence[j] == sequence[i]) {
 
                    // If occurrence found
                    // then the next term will be
                    // how far back this last term
                    // occured previousely
                    sequence[i + 1] = i - j;
                    break;
                }
            }
        }
    }
 
    // Utility function to return
    // Nth term of sequence
    static int getNthTerm(int n)
    {
 
        return sequence[n];
    }
 
    // Driver code
    public static void Main()
    {
 
        // Pre-compute Van Eck's sequence
        vanEckSequence();
 
        int n = 6;
 
        // Print nth term of the sequence
        Console.WriteLine(getNthTerm(n));
 
        n = 100;
 
        // Print nth term of the sequence
        Console.WriteLine(getNthTerm(n));
    }
}

Javascript




<script>
 
// Javascript program to print Nth
// term of Van Eck's sequence
 
var MAX = 1000;
var sequence = Array(MAX).fill(0);
 
// Utility function to compute
// Van Eck's sequence
function vanEckSequence()
{
 
    // Initialize sequence array
    for (var i = 0; i < MAX; i++) {
        sequence[i] = 0;
    }
 
    // Loop to generate sequence
    for (var i = 0; i < MAX; i++) {
 
        // Check if sequence[i] has occured
        // previousely or is new to sequence
        for (var j = i - 1; j >= 0; j--) {
            if (sequence[j] == sequence[i]) {
 
                // If occurrence found
                // then the next term will be
                // how far back this last term
                // occured previousely
                sequence[i + 1] = i - j;
                break;
            }
        }
    }
}
 
// Utility function to return
// Nth term of sequence
function getNthTerm(n)
{
 
    return sequence[n];
}
 
// Driver code
// Pre-compute Van Eck's sequence
vanEckSequence();
var n = 6;
 
// Print nth term of the sequence
document.write( getNthTerm(n) + "<br>");
n = 100;
 
// Print nth term of the sequence
document.write( getNthTerm(n) + "<br>");
 
// This code is contributed by itsok.
</script>
Output: 
2
23

 

Method 2: Using lambda functions 
There is no need to store the entire sequence to get the value of nth term. We can recursively build the series upto nth term and find only the value of nth term using the lambda expression.
Below is the implementation of the above approach using lambda function: 
 

Python3




# Python3 program to find
# Nth term of Van Eck's sequence
# using the lambda expression
 
# Lambda function 
f = lambda n, l = 0, *s : f(n-1, l in s and ~s.index(l), l, *s) \
if n else -l
 
# The above lambda function recursively
# build the sequence and store it in tuple s
 
# The expression l in s and ~s.index(l)
# returns False if l is not present in tuple s
# otherwise, returns the negation of the value of
# the index of l in tuple s
# and is appended to tuple s
# Thus, tuple s store negation of all the elements
# of the sequence in reverse order
 
# At the end, when n reaches 0, function converts
# the nth term back to its actual value
# and returns it.
   
 
# Driver code
n = 6
 
# Get Nth term of the sequence
print(f(n))
 
n = 100
# Get Nth term of the sequence
print(f(n))
Output: 
2
23

 

Reference: https://codegolf.stackexchange.com/questions/186654/nth-term-of-van-eck-sequence
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :