In mathematics, the Golomb sequence is a non-decreasing integer sequence where n-th term is equal to number of times n appears in the sequence.
The first few values are
1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, ……
Explanation of few terms:
Third term is 2, note that three appears 2 times.
Second term is 2, note that two appears 2 times.
Fourth term is 3, note that four appears 3 times.
Given a positive integer n. The task is to find the first n terms of Golomb sequence.
Examples:
Input : n = 4
Output : 1 2 2 3
Input : n = 6
Output : 1 2 2 3 3 4
The recurrence relation to find the nth term of Golomb sequence:
a(1) = 1
a(n + 1) = 1 + a(n + 1 – a(a(n)))
Below is the implementation using Recursion:
C++
#include <bits/stdc++.h>
using namespace std;
int findGolomb( int n)
{
if (n == 1)
return 1;
return 1 + findGolomb(n -
findGolomb(findGolomb(n - 1)));
}
void printGolomb( int n)
{
for ( int i = 1; i <= n; i++)
cout << findGolomb(i) << " " ;
}
int main()
{
int n = 9;
printGolomb(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int findGolomb( int n)
{
if (n == 1 )
return 1 ;
return 1 + findGolomb(n -
findGolomb(findGolomb(n - 1 )));
}
public static void printGolomb( int n)
{
for ( int i = 1 ; i <= n; i++)
System.out.print(findGolomb(i) +
" " );
}
public static void main (String[] args)
{
int n = 9 ;
printGolomb(n);
}
}
|
Python3
def findGolomb(n):
if (n = = 1 ):
return 1
return 1 + findGolomb(n -
findGolomb(findGolomb(n - 1 )))
def printGolomb(n):
for i in range ( 1 , n + 1 ):
print (findGolomb(i), end = " " )
n = 9
printGolomb(n)
|
C#
using System;
class GFG
{
static int findGolomb( int n)
{
if (n == 1)
return 1;
return 1 + findGolomb(n -
findGolomb(findGolomb(n - 1)));
}
static void printGolomb( int n)
{
for ( int i = 1; i <= n; i++)
Console .Write(findGolomb(i) +
" " );
}
public static void Main ()
{
int n = 9;
printGolomb(n);
}
}
|
PHP
<?php
function findGolomb( $n )
{
if ( $n == 1)
return 1;
return 1 + findGolomb( $n -
findGolomb(findGolomb( $n - 1)));
}
function printGolomb( $n )
{
for ( $i = 1; $i <= $n ; $i ++)
echo findGolomb( $i ) , " " ;
}
$n = 9;
printGolomb( $n );
?>
|
Javascript
<script>
function findGolomb(n)
{
if (n == 1)
return 1;
return 1 + findGolomb(n - findGolomb(findGolomb(n - 1)));
}
function printGolomb(n)
{
for (let i = 1; i <= n; i++)
document.write(findGolomb(i) + " " );
}
var n = 9;
printGolomb(n);
</script>
|
Output :
1 2 2 3 3 4 4 4 5
Below is the implementation using Dynamic Programming:
C++
#include <bits/stdc++.h>
using namespace std;
void printGolomb( int n)
{
int dp[n + 1];
dp[1] = 1;
cout << dp[1] << " " ;
for ( int i = 2; i <= n; i++)
{
dp[i] = 1 + dp[i - dp[dp[i - 1]]];
cout << dp[i] << " " ;
}
}
int main()
{
int n = 9;
printGolomb(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void printGolomb( int n)
{
int dp[] = new int [n + 1 ];
dp[ 1 ] = 1 ;
System.out.print(dp[ 1 ] + " " );
for ( int i = 2 ; i <= n; i++)
{
dp[i] = 1 + dp[i - dp[dp[i - 1 ]]];
System.out.print(dp[i] + " " );
}
}
public static void main (String[] args)
{
int n = 9 ;
printGolomb(n);
}
}
|
Python3
def Golomb( n):
dp = [ 0 ] * (n + 1 )
dp[ 1 ] = 1
print (dp[ 1 ], end = " " )
for i in range ( 2 , n + 1 ):
dp[i] = 1 + dp[i - dp[dp[i - 1 ]]]
print (dp[i], end = " " )
n = 9
Golomb(n)
|
C#
using System;
class GFG
{
static void printGolomb( int n)
{
int []dp = new int [n + 1];
dp[1] = 1;
Console.Write(dp[1] + " " );
for ( int i = 2; i <= n; i++)
{
dp[i] = 1 + dp[i - dp[dp[i - 1]]];
Console.Write( dp[i] + " " );
}
}
public static void Main ()
{
int n = 9;
printGolomb(n);
}
}
|
PHP
<?php
function printGolomb( $n )
{
$dp [1] = 1;
echo $dp [1], " " ;
for ( $i = 2; $i <= $n ; $i ++)
{
$dp [ $i ] = 1 + $dp [ $i -
$dp [ $dp [ $i - 1]]];
echo $dp [ $i ], " " ;
}
}
$n = 9;
printGolomb( $n );
?>
|
Javascript
<script>
function printGolomb( n) {
let dp = Array(n + 1).fill(0);
dp[1] = 1;
document.write(dp[1] + " " );
for ( i = 2; i <= n; i++) {
dp[i] = 1 + dp[i - dp[dp[i - 1]]];
document.write(dp[i] + " " );
}
}
let n = 9;
printGolomb(n);
</script>
|
Output :
1 2 2 3 3 4 4 4 5
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
23 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...