Program to find Nth term of the Van Eck’s Sequence
Last Updated :
11 Oct, 2023
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++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
int sequence[MAX];
void vanEckSequence()
{
for ( int i = 0; i < MAX; i++) {
sequence[i] = 0;
}
for ( int i = 0; i < MAX; i++) {
for ( int j = i - 1; j >= 0; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1] = i - j;
break ;
}
}
}
}
int getNthTerm( int n)
{
return sequence[n];
}
int main()
{
vanEckSequence();
int n = 6;
cout << getNthTerm(n) << endl;
n = 100;
cout << getNthTerm(n) << endl;
return 0;
}
|
Java
class GFG {
static int MAX = 1000 ;
static int sequence[] = new int [MAX];
static void vanEckSequence()
{
for ( int i = 0 ; i < MAX - 1 ; i++) {
sequence[i] = 0 ;
}
for ( int i = 0 ; i < MAX - 1 ; i++) {
for ( int j = i - 1 ; j >= 0 ; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1 ] = i - j;
break ;
}
}
}
}
static int getNthTerm( int n)
{
return sequence[n];
}
public static void main(String[] args)
{
vanEckSequence();
int n = 6 ;
System.out.println(getNthTerm(n));
n = 100 ;
System.out.println(getNthTerm(n));
}
}
|
Python3
MAX = 1000
sequence = [ 0 ] * ( MAX + 1 );
def vanEckSequence() :
for i in range ( MAX ) :
sequence[i] = 0 ;
for i in range ( MAX ) :
for j in range (i - 1 , - 1 , - 1 ) :
if (sequence[j] = = sequence[i]) :
sequence[i + 1 ] = i - j;
break ;
def getNthTerm(n) :
return sequence[n];
if __name__ = = "__main__" :
vanEckSequence();
n = 6 ;
print (getNthTerm(n));
n = 100 ;
print (getNthTerm(n));
|
C#
using System;
class GFG {
static int MAX = 1000;
static int [] sequence = new int [MAX];
static void vanEckSequence()
{
for ( int i = 0; i < MAX - 1; i++) {
sequence[i] = 0;
}
for ( int i = 0; i < MAX - 1; i++) {
for ( int j = i - 1; j >= 0; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1] = i - j;
break ;
}
}
}
}
static int getNthTerm( int n)
{
return sequence[n];
}
public static void Main()
{
vanEckSequence();
int n = 6;
Console.WriteLine(getNthTerm(n));
n = 100;
Console.WriteLine(getNthTerm(n));
}
}
|
Javascript
<script>
function getVanEckSequence(n) {
let result = [0];
let dir = {};
for ( let i = 0 ; i < n ; i++) {
const currentData = result[i];
const currentPosition = i + 1;
if (dir[currentData]) {
result.push(currentPosition - dir[currentData]);
} else {
result.push(0);
}
dir[currentData] = currentPosition;
}
return result;
}
console.log(getVanEckSequence(100));
</script>
|
Time Complexity: O(MAX2)
Auxiliary Space: O(MAX)
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:
C++
#include <iostream>
#include <vector>
using namespace std;
int vanEck( int n) {
vector< int > s(n + 1, 0);
int l = 0;
for ( int i = 1; i <= n; i++) {
if (l == 0 || s[l] != 0) {
s[i] = 0;
l = (s[l] == 0) ? (i - 1) : (i - s[l]);
} else {
s[i] = 0;
l = i - 1;
}
}
return l;
}
int main() {
int n = 6;
cout << vanEck(n) << endl;
n = 100;
cout << vanEck(n) << endl;
return 0;
}
|
Java
public class VanEckSequence {
public static int vanEck( int n) {
int [] s = new int [n + 1 ];
int l = 0 ;
for ( int i = 1 ; i <= n; i++) {
if (l == 0 || s[l] != 0 ) {
s[i] = 0 ;
l = s[l] == 0 ? i - 1 : i - s[l];
} else {
s[i] = 0 ;
l = i - 1 ;
}
}
return l;
}
public static void main(String[] args) {
int n = 6 ;
System.out.println(vanEck(n));
n = 100 ;
System.out.println(vanEck(n));
}
}
|
Python3
f = lambda n, l = 0 , * s: f(n - 1 , l in s and ~s.index(l), l, * s) \
if n else - l
n = 6
print (f(n))
n = 100
print (f(n))
|
C#
using System;
public class VanEckSequence
{
public static int VanEck( int n)
{
int [] s = new int [n + 1];
int l = 0;
for ( int i = 1; i <= n; i++)
{
if (l == 0 || s[l] != 0)
{
s[i] = 0;
l = s[l] == 0 ? i - 1 : i - s[l];
}
else
{
s[i] = 0;
l = i - 1;
}
}
return l;
}
public static void Main( string [] args)
{
int n = 6;
Console.WriteLine(VanEck(n));
n = 100;
Console.WriteLine(VanEck(n));
}
}
|
Javascript
const f = (n, l = 0, ...s) =>
n ? f(n-1, s.includes(l) ? ~s.indexOf(l) : 0, l, ...s) :
-l;
let x= 6;
console.log(f(x));
x = 100;
console.log(f(x));
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Reference: https://codegolf.stackexchange.com/questions/186654/nth-term-of-van-eck-sequence
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...