Given a positive Integer n, The task is to print Gould’s sequence up to nth term.
In Mathematics, Gould’s sequence is an integer sequence whose nth term tells the count of odd numbers in n-1th row of the pascal triangle. This sequence is consist of only power’s of 2.
For Example:
Row Number Pascal's triangle count of odd numbers in ith row
0th row 1 1
1st row 1 1 2
2nd row 1 2 1 2
3rd row 1 3 3 1 4
4th row 1 4 6 4 1 2
5th row 1 5 10 10 5 1 4
6th row 1 6 15 20 15 6 1 4
7th row 1 7 21 35 35 21 7 1 8
8th row 1 8 28 56 70 56 28 8 1 2
9th row 1 9 36 84 126 126 84 36 9 1 4
10th row 1 10 45 120 210 256 210 120 45 10 1 4
So first few terms of Gould’s sequence are-
1, 2, 2, 4, 2, 4, 4, 8, 2, 4, 4, 8, 4, 8, 8, 16, 2, 4, 4, 8, 4, 8, 8, 16, 4, 8, 8, 16, 8, 16, 16, 32
A Simple Solution to generate Gould’s sequence is to generate every row of pascal’s triangle from 0th row to nth row and count odd numbers appearing in each row.
ith element of nth row can be calculated easily by calculating binomial coefficient C(n, i).
For More detail about this approach Refer This –Pascal’s triangle.
Below is the implementation of above idea
C++
#include <bits/stdc++.h>
using namespace std;
void gouldSequence( int n)
{
for ( int row_num = 1; row_num <= n; row_num++) {
int count = 1;
int c = 1;
for ( int i = 1; i <= row_num; i++) {
c = c * (row_num - i) / i;
if (c % 2 == 1)
count++;
}
cout << count << " " ;
}
}
int main()
{
int n = 16;
gouldSequence(n);
return 0;
}
|
Java
class GFG {
static void gouldSequence( int n)
{
for ( int row_num = 1 ; row_num <= n; row_num++) {
int count = 1 ;
int c = 1 ;
for ( int i = 1 ; i <= row_num; i++) {
c = c * (row_num - i) / i;
if (c % 2 == 1 )
count++;
}
System.out.print(count + " " );
}
}
public static void main(String[] args)
{
int n = 16 ;
gouldSequence(n);
}
}
|
Python 3
def gouldSequence(n):
for row_num in range ( 1 , n):
count = 1
c = 1
for i in range ( 1 , row_num):
c = c * (row_num - i) / i
if (c % 2 = = 1 ):
count + = 1
print (count, end = " " )
n = 16 ;
gouldSequence(n)
|
C#
using System;
class GFG {
static void gouldSequence( int n)
{
for ( int row_num = 1; row_num <= n; row_num++) {
int count = 1;
int c = 1;
for ( int i = 1; i <= row_num; i++) {
c = c * (row_num - i) / i;
if (c % 2 == 1)
count++;
}
Console.Write(count + " " );
}
}
public static void Main()
{
int n = 16;
gouldSequence(n);
}
}
|
PHP
<?php
function gouldSequence( $n )
{
for ( $row_num = 1; $row_num <= $n ; $row_num ++) {
$count = 1;
$c = 1;
for ( $i = 1; $i <= $row_num ; $i ++) {
$c = $c * ( $row_num - $i ) / $i ;
if ( $c % 2 == 1)
$count ++;
}
echo $count , " " ;
}
}
$n = 16;
gouldSequence( $n );
?>
|
Javascript
<script>
function gouldSequence(n)
{
for ( var row_num = 1; row_num <= n; row_num++) {
var count = 1;
var c = 1;
for ( var i = 1; i <= row_num; i++) {
c = c * (row_num - i) / i;
if (c % 2 == 1)
count++;
}
document.write( count + " " );
}
}
var n = 16;
gouldSequence(n);
</script>
|
Output
1 2 2 4 2 4 4 8 2 4 4 8 4 8 8 16
Time Complexity: O(n2)
Auxiliary Space: O(1)
An Efficient Solution is based on the fact that the count of odd numbers in ith row of pascal’s Triangle is 2 raised to the count of 1’s in binary representation of i.
For Example
for row=5
5 in binary = 101
count of 1's =2
22= 4
So, 5th row of pascal triangle will have 4 odd number
By counting 1’s in binary representation of every row number up to n, we can generate Gould’s Sequence up to n.
Below is the implementation of above idea-
C++
#include <bits/stdc++.h>
using namespace std;
int countOddNumber( int row_num)
{
unsigned int count = 0;
while (row_num) {
count += row_num & 1;
row_num >>= 1;
}
return (1 << count);
}
void gouldSequence( int n)
{
for ( int row_num = 0; row_num < n; row_num++) {
cout << countOddNumber(row_num) << " " ;
}
}
int main()
{
int n = 16;
gouldSequence(n);
return 0;
}
|
Java
class GFG {
static int countOddNumber( int row_num)
{
int count = 0 ;
while (row_num > 0 ) {
count += row_num & 1 ;
row_num >>= 1 ;
}
return ( 1 << count);
}
static void gouldSequence( int n)
{
for ( int row_num = 0 ; row_num < n; row_num++) {
System.out.print(countOddNumber(row_num) + " " );
}
}
public static void main(String[] args)
{
int n = 16 ;
gouldSequence(n);
}
}
|
Python3
def countOddNumber(row_num):
count = 0
while row_num ! = 0 :
count + = row_num & 1
row_num >> = 1
return ( 1 << count)
def gouldSequence(n):
for row_num in range ( 0 , n):
print (countOddNumber(row_num), end = " " )
if __name__ = = "__main__" :
n = 16
gouldSequence(n)
|
C#
using System;
class GFG {
static int countOddNumber( int row_num)
{
int count = 0;
while (row_num > 0) {
count += row_num & 1;
row_num >>= 1;
}
return (1 << count);
}
static void gouldSequence( int n)
{
for ( int row_num = 0; row_num < n; row_num++) {
Console.Write(countOddNumber(row_num) + " " );
}
}
public static void Main()
{
int n = 16;
gouldSequence(n);
}
}
|
PHP
<?php
function countOddNumber( $row_num )
{
$count = 0;
while ( $row_num )
{
$count += $row_num & 1;
$row_num >>= 1;
}
return (1 << $count );
}
function gouldSequence( $n )
{
for ( $row_num = 0;
$row_num < $n ; $row_num ++)
{
echo countOddNumber( $row_num ), " " ;
}
}
$n = 16;
gouldSequence( $n );
?>
|
Javascript
<script>
function countOddNumber(row_num)
{
var count = 0;
while (row_num > 0) {
count += row_num & 1;
row_num >>= 1;
}
return (1 << count);
}
function gouldSequence(n)
{
for ( var row_num = 0; row_num < n; row_num++) {
document.write(countOddNumber(row_num) + " " );
}
}
var n = 16;
gouldSequence(n);
</script>
|
Output
1 2 2 4 2 4 4 8 2 4 4 8 4 8 8 16
A Better Solution ( Using Dynamic programming ) is based on the observation that after every power of 2 earlier terms got double up.
For Example
first term of the sequence is - 1
Now After every power of 2 we will double the value of previous terms
Terms up to 21 1 2
Terms up to 22 1 2 2 4
Terms up to 23 1 2 2 4 2 4 4 8
Terms up to 24 1 2 2 4 2 4 4 8 2 4 4 8 4 8 8 16
So, We can compute Gould’s Sequence terms after 2i by doubling the value of previous terms
Below is the implementation of above approach-
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 32768
int arr[2 * MAX];
int gouldSequence()
{
arr[0] = 1;
int i = 1;
int p = 1;
while (i <= MAX) {
int j = 0;
while (j < i) {
arr[i + j] = 2 * arr[j];
j++;
}
i = (1 << p);
p++;
}
}
void printSequence( int n)
{
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
}
int main()
{
gouldSequence();
int n = 16;
printSequence(n);
return 0;
}
|
Java
class GFG {
static final int MAX = 32768 ;
static int [] arr = new int [ 2 * MAX];
static void gouldSequence()
{
arr[ 0 ] = 1 ;
int i = 1 ;
int p = 1 ;
while (i <= MAX) {
int j = 0 ;
while (j < i) {
arr[i + j] = 2 * arr[j];
j++;
}
i = ( 1 << p);
p++;
}
}
static void printSequence( int n)
{
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
gouldSequence();
int n = 16 ;
printSequence(n);
}
}
|
Python3
MAX = 32768
arr = [ None ] * ( 2 * MAX )
def gouldSequence():
arr[ 0 ] = 1
i = 1
p = 1
while i < = MAX :
j = 0
while j < i:
arr[i + j] = 2 * arr[j]
j + = 1
i = ( 1 << p)
p + = 1
def printSequence(n):
for i in range ( 0 , n):
print (arr[i], end = " " )
if __name__ = = "__main__" :
gouldSequence()
n = 16
printSequence(n)
|
C#
using System;
class GFG {
static int MAX = 32768;
static int [] arr = new int [2 * MAX];
static void gouldSequence()
{
arr[0] = 1;
int i = 1;
int p = 1;
while (i <= MAX) {
int j = 0;
while (j < i) {
arr[i + j] = 2 * arr[j];
j++;
}
i = (1 << p);
p++;
}
}
static void printSequence( int n)
{
for ( int i = 0; i < n; i++) {
Console.Write(arr[i] + " " );
}
}
public static void Main()
{
gouldSequence();
int n = 16;
printSequence(n);
}
}
|
PHP
<?php
$MAX = 32768;
$arr = array_fill (0, 2 * $MAX , 0);
function gouldSequence()
{
global $MAX , $arr ;
$arr [0] = 1;
$i = 1;
$p = 1;
while ( $i <= $MAX )
{
$j = 0;
while ( $j < $i )
{
$arr [ $i + $j ] = 2 * $arr [ $j ];
$j ++;
}
$i = (1 << $p );
$p ++;
}
}
function printSequence( $n )
{
global $MAX , $arr ;
for ( $i = 0; $i < $n ; $i ++)
{
echo $arr [ $i ]. " " ;
}
}
gouldSequence();
$n = 16;
printSequence( $n );
?>
|
Javascript
<script>
var MAX = 32768;
var arr = Array(2 * MAX);
function gouldSequence()
{
arr[0] = 1;
var i = 1;
var p = 1;
while (i <= MAX) {
var j = 0;
while (j < i) {
arr[i + j] = 2 * arr[j];
j++;
}
i = (1 << p);
p++;
}
}
function printSequence(n)
{
for ( var i = 0; i < n; i++) {
document.write( arr[i] + " " );
}
}
gouldSequence();
var n = 16;
printSequence(n);
</script>
|
Output
1 2 2 4 2 4 4 8 2 4 4 8 4 8 8 16
Last Updated :
25 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...