📜  古尔德的序列

📅  最后修改于: 2021-04-24 21:46:03             🧑  作者: Mango

给定正整数Integer,任务是打印第n个项的Gould序列。

在数学中,古尔德序列是一个整数序列,其n项表示帕斯卡三角形的n-1行中的奇数计数。此序列仅包含2的幂。

例如

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 

因此,古尔德序列的前几项是-

生成Gould序列的一种简单解决方案是生成从第0行到第n行的Pascal三角形的每一行,并对每行中出现的奇数进行计数。
通过计算二项式系数C(n,i)可以很容易地计算出第n行的第i个元素。
有关此方法的更多详细信息,请参考此–Pascal的三角形。
以下是上述想法的实现

C++
// CPP program to generate
// Gould's Sequence
  
#include 
using namespace std;
  
// Function to generate gould's Sequence
void gouldSequence(int n)
{
    // loop to generate each row
    // of pascal's Triangle up to nth row
    for (int row_num = 1; row_num <= n; row_num++) {
  
        int count = 1;
        int c = 1;
  
        // Loop to generate each element of ith row
        for (int i = 1; i <= row_num; i++) {
  
            c = c * (row_num - i) / i;
  
            // if c is odd
            // increment count
            if (c % 2 == 1)
                count++;
        }
  
        // print count of odd elements
        cout << count << " ";
    }
}
  
// Driver code
int main()
{
  
    // Get n
    int n = 16;
  
    // Function call
    gouldSequence(n);
  
    return 0;
}


Java
// JAVA program to generate
// Gould's Sequence
  
class GFG {
  
    // Function to generate gould's Sequence
    static void gouldSequence(int n)
    {
        // loop to generate each row
        // of pascal's Triangle up to nth row
        for (int row_num = 1; row_num <= n; row_num++) {
  
            int count = 1;
            int c = 1;
  
            // Loop to generate each element of ith row
            for (int i = 1; i <= row_num; i++) {
  
                c = c * (row_num - i) / i;
  
                // if c is odd
                // increment count
                if (c % 2 == 1)
                    count++;
            }
  
            // print count of odd elements
            System.out.print(count + " ");
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Get n
        int n = 16;
  
        // Function call
        gouldSequence(n);
    }
}


Python 3
# Python 3 program to generate
# Gould's Sequence
  
# Function to generate gould's Sequence
def gouldSequence(n):
  
    # loop to generate each row
    # of pascal's Triangle up to nth row
    for row_num in range (1, n): 
      
        count = 1
        c = 1
  
        # Loop to generate each 
        # element of ith row
        for i in range (1, row_num):
            c = c * (row_num - i) / i
  
            # if c is odd
            # increment count
            if (c % 2 == 1):
                count += 1
  
        # print count of odd elements
        print(count, end = " ")
          
# Driver code
  
# Get n
n = 16;
  
# Function call
gouldSequence(n)
  
# This code is contributed 
# by Akanksha Rai


C#
// C# program to generate
// Gould's Sequence
  
using System;
class GFG {
  
    // Function to generate gould's Sequence
    static void gouldSequence(int n)
    {
        // loop to generate each row
        // of pascal's Triangle up to nth row
        for (int row_num = 1; row_num <= n; row_num++) {
  
            int count = 1;
            int c = 1;
  
            // Loop to generate each element of ith row
            for (int i = 1; i <= row_num; i++) {
  
                c = c * (row_num - i) / i;
  
                // if c is odd
                // increment count
                if (c % 2 == 1)
                    count++;
            }
  
            // print count of odd elements
            Console.Write(count + " ");
        }
    }
  
    // Driver code
    public static void Main()
    {
  
        // Get n
        int n = 16;
  
        // Function call
        gouldSequence(n);
    }
}


PHP


C++
// CPP program to generate
// Gould's Sequence
  
#include 
using namespace std;
  
// Utility function to count odd numbers
// in ith row of Pascals's triangle
int countOddNumber(int row_num)
{
  
    // Count set bits in row_num
  
    // Initialize count as zero
    unsigned int count = 0;
    while (row_num) {
        count += row_num & 1;
        row_num >>= 1;
    }
  
    // Return 2^count
    return (1 << count);
}
  
// Function to generate gould's Sequence
void gouldSequence(int n)
{
    // loop to generate gould's Sequence up to n
    for (int row_num = 0; row_num < n; row_num++) {
  
        cout << countOddNumber(row_num) << " ";
    }
}
  
// Driver code
int main()
{
  
    // Get n
    int n = 16;
  
    // Function call
    gouldSequence(n);
  
    return 0;
}


Java
// JAVA program to generate
// Gould's Sequence
  
class GFG {
  
    // Utility function to count odd numbers
    // in ith row of Pascals's triangle
    static int countOddNumber(int row_num)
    {
  
        // Count set bits in row_num
  
        // Initialize count as zero
        int count = 0;
        while (row_num > 0) {
            count += row_num & 1;
            row_num >>= 1;
        }
  
        // Return 2^count
        return (1 << count);
    }
  
    // Function to generate gould's Sequence
    static void gouldSequence(int n)
    {
        // loop to generate gould's Sequence up to n
        for (int row_num = 0; row_num < n; row_num++) {
  
            System.out.print(countOddNumber(row_num) + " ");
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Get n
        int n = 16;
  
        // Function call
        gouldSequence(n);
    }
}


Python3
# Python3 program to generate 
# Gould's Sequence 
  
# Utility function to count odd numbers 
# in ith row of Pascals's triangle 
def countOddNumber(row_num): 
  
    # Count set bits in row_num 
    # Initialize count as zero 
    count = 0
    while row_num != 0: 
        count += row_num & 1
        row_num >>= 1
  
    # Return 2^count 
    return (1 << count) 
  
# Function to generate gould's Sequence 
def gouldSequence(n): 
  
    # loop to generate gould's
    # Sequence up to n 
    for row_num in range(0, n): 
        print(countOddNumber(row_num), end = " ") 
  
# Driver code 
if __name__ == "__main__":
  
    # Get n 
    n = 16
  
    # Function call 
    gouldSequence(n) 
  
# This code is contributed
# by Rituraj Jain


C#
// C# program to generate
// Gould's Sequence
  
using System;
class GFG {
  
    // Utility function to count odd numbers
    // in ith row of Pascals's triangle
    static int countOddNumber(int row_num)
    {
  
        // Count set bits in row_num
  
        // Initialize count as zero
        int count = 0;
        while (row_num > 0) {
            count += row_num & 1;
            row_num >>= 1;
        }
  
        // Return 2^count
        return (1 << count);
    }
  
    // Function to generate gould's Sequence
    static void gouldSequence(int n)
    {
        // loop to generate gould's Sequence up to n
        for (int row_num = 0; row_num < n; row_num++) {
  
            Console.Write(countOddNumber(row_num) + " ");
        }
    }
  
    // Driver code
    public static void Main()
    {
        // Get n
        int n = 16;
  
        // Function call
        gouldSequence(n);
    }
}


PHP
>= 1;
    }
  
    // Return 2^count
    return (1 << $count);
}
  
// Function to generate gould's Sequence
function gouldSequence($n)
{
    // loop to generate gould's Sequence up to n
    for ($row_num = 0; 
         $row_num < $n; $row_num++)
    {
  
        echo countOddNumber($row_num), " ";
    }
}
  
// Driver code
  
// Get n
$n = 16;
  
// Function call
gouldSequence($n);
  
// This code is contributed 
// by Sach_Code
?>


C++
// CPP program to generate
// Gould's Sequence
  
#include 
using namespace std;
  
// 32768 = 2^15
#define MAX 32768
  
// Array to store Sequence up to
// 2^16 = 65536
int arr[2 * MAX];
  
// Utility function to pre-compute odd numbers
// in ith row of Pascals's triangle
int gouldSequence()
{
  
    // First term of the Sequence is 1
    arr[0] = 1;
  
    // Initilize i to 1
    int i = 1;
  
    // Initilize p to 1 (i.e 2^i)
    // in each iteration
    // i will be pth power of 2
    int p = 1;
  
    // loop to generate gould's Sequence
    while (i <= MAX) {
  
        // i is pth power of 2
        // traverse the array
        // from j=0 to i i.e (2^p)
  
        int j = 0;
  
        while (j < i) {
  
            // double the value of arr[j]
            // and store to arr[i+j]
            arr[i + j] = 2 * arr[j];
            j++;
        }
  
        // upadate i to next power of 2
        i = (1 << p);
  
        // increment p
        p++;
    }
}
  
// Function to print gould's Sequence
void printSequence(int n)
{
    // loop to generate gould's Sequence up to n
  
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
}
  
// Driver code
int main()
{
  
    gouldSequence();
  
    // Get n
    int n = 16;
  
    // Function call
    printSequence(n);
  
    return 0;
}


Java
// JAVA program to generate
// Gould's Sequence
  
class GFG {
  
    // 32768 = 2^15
    static final int MAX = 32768;
  
    // Array to store Sequence up to
    // 2^16 = 65536
    static int[] arr = new int[2 * MAX];
  
    // Utility function to pre-compute odd numbers
    // in ith row of Pascals's triangle
    static void gouldSequence()
    {
  
        // First term of the Sequence is 1
        arr[0] = 1;
  
        // Initilize i to 1
        int i = 1;
  
        // Initilize p to 1 (i.e 2^i)
        // in each iteration
        // i will be pth power of 2
        int p = 1;
  
        // loop to generate gould's Sequence
        while (i <= MAX) {
  
            // i is pth power of 2
            // traverse the array
            // from j=0 to i i.e (2^p)
  
            int j = 0;
  
            while (j < i) {
                // double the value of arr[j]
                // and store to arr[i+j]
                arr[i + j] = 2 * arr[j];
                j++;
            }
  
            // upadate i to next power of 2
            i = (1 << p);
  
            // increment p
            p++;
        }
    }
  
    // Function to print gould's Sequence
    static void printSequence(int n)
    {
        // loop to generate gould's Sequence up to n
  
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        gouldSequence();
  
        // Get n
        int n = 16;
  
        // Function call
        printSequence(n);
    }
}


Python3
# Python3 program to generate 
# Gould's Sequence 
  
# 32768 = 2^15 
MAX = 32768
  
# Array to store Sequence up to 
# 2^16 = 65536 
arr = [None] * (2 * MAX)
  
# Utility function to pre-compute 
# odd numbers in ith row of Pascals's 
# triangle 
def gouldSequence(): 
  
    # First term of the Sequence is 1 
    arr[0] = 1
  
    # Initilize i to 1 
    i = 1
  
    # Initilize p to 1 (i.e 2^i) 
    # in each iteration 
    # i will be pth power of 2 
    p = 1
  
    # loop to generate gould's Sequence 
    while i <= MAX: 
  
        # i is pth power of 2 
        # traverse the array 
        # from j=0 to i i.e (2^p) 
        j = 0
  
        while j < i: 
  
            # double the value of arr[j] 
            # and store to arr[i+j] 
            arr[i + j] = 2 * arr[j] 
            j += 1
          
        # upadate i to next power of 2 
        i = (1 << p) 
  
        # increment p 
        p += 1
      
# Function to print gould's Sequence 
def printSequence(n): 
  
    # loop to generate gould's Sequence 
    # up to n 
    for i in range(0, n):
        print(arr[i], end = " ") 
      
# Driver code 
if __name__ == "__main__": 
  
    gouldSequence() 
  
    # Get n 
    n = 16
  
    # Function call 
    printSequence(n) 
  
# This code is contributed
# by Rituraj Jain


C#
// C# program to generate
// Gould's Sequence
  
using System;
class GFG {
  
    // 32768 = 2^15
    static int MAX = 32768;
  
    // Array to store Sequence up to
    // 2^16 = 65536
    static int[] arr = new int[2 * MAX];
  
    // Utility function to pre-compute odd numbers
    // in ith row of Pascals's triangle
    static void gouldSequence()
    {
  
        // First term of the Sequence is 1
        arr[0] = 1;
  
        // Initialize i to 1
        int i = 1;
  
        // Initialize p to 1 (i.e 2^i)
        // in each iteration
        // i will be pth power of 2
        int p = 1;
  
        // loop to generate gould's Sequence
        while (i <= MAX) {
  
            // i is pth power of 2
            // traverse the array
            // from j=0 to i i.e (2^p)
  
            int j = 0;
  
            while (j < i) {
                // double the value of arr[j]
                // and store to arr[i+j]
                arr[i + j] = 2 * arr[j];
                j++;
            }
  
            // upadate i to next power of 2
            i = (1 << p);
  
            // increment p
            p++;
        }
    }
  
    // Function to print gould's Sequence
    static void printSequence(int n)
    {
        // loop to generate gould's Sequence up to n
  
        for (int i = 0; i < n; i++) {
            Console.Write(arr[i] + " ");
        }
    }
  
    // Driver code
    public static void Main()
    {
  
        gouldSequence();
  
        // Get n
        int n = 16;
  
        // Function call
        printSequence(n);
    }
}


PHP


输出
1 2 2 4 2 4 4 8 2 4 4 8 4 8 8 16 

一个有效的解决方案基于以下事实:在i的二进制表示形式中,帕斯卡三角形的第i行的奇数计数增加到2,到1的计数增加。

例如

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

通过以最大n的每个行号的二进制表示形式计数1,我们可以生成n的古尔德序列。

以下是上述想法的实现-

C++

// CPP program to generate
// Gould's Sequence
  
#include 
using namespace std;
  
// Utility function to count odd numbers
// in ith row of Pascals's triangle
int countOddNumber(int row_num)
{
  
    // Count set bits in row_num
  
    // Initialize count as zero
    unsigned int count = 0;
    while (row_num) {
        count += row_num & 1;
        row_num >>= 1;
    }
  
    // Return 2^count
    return (1 << count);
}
  
// Function to generate gould's Sequence
void gouldSequence(int n)
{
    // loop to generate gould's Sequence up to n
    for (int row_num = 0; row_num < n; row_num++) {
  
        cout << countOddNumber(row_num) << " ";
    }
}
  
// Driver code
int main()
{
  
    // Get n
    int n = 16;
  
    // Function call
    gouldSequence(n);
  
    return 0;
}

Java

// JAVA program to generate
// Gould's Sequence
  
class GFG {
  
    // Utility function to count odd numbers
    // in ith row of Pascals's triangle
    static int countOddNumber(int row_num)
    {
  
        // Count set bits in row_num
  
        // Initialize count as zero
        int count = 0;
        while (row_num > 0) {
            count += row_num & 1;
            row_num >>= 1;
        }
  
        // Return 2^count
        return (1 << count);
    }
  
    // Function to generate gould's Sequence
    static void gouldSequence(int n)
    {
        // loop to generate gould's Sequence up to n
        for (int row_num = 0; row_num < n; row_num++) {
  
            System.out.print(countOddNumber(row_num) + " ");
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Get n
        int n = 16;
  
        // Function call
        gouldSequence(n);
    }
}

Python3

# Python3 program to generate 
# Gould's Sequence 
  
# Utility function to count odd numbers 
# in ith row of Pascals's triangle 
def countOddNumber(row_num): 
  
    # Count set bits in row_num 
    # Initialize count as zero 
    count = 0
    while row_num != 0: 
        count += row_num & 1
        row_num >>= 1
  
    # Return 2^count 
    return (1 << count) 
  
# Function to generate gould's Sequence 
def gouldSequence(n): 
  
    # loop to generate gould's
    # Sequence up to n 
    for row_num in range(0, n): 
        print(countOddNumber(row_num), end = " ") 
  
# Driver code 
if __name__ == "__main__":
  
    # Get n 
    n = 16
  
    # Function call 
    gouldSequence(n) 
  
# This code is contributed
# by Rituraj Jain

C#

// C# program to generate
// Gould's Sequence
  
using System;
class GFG {
  
    // Utility function to count odd numbers
    // in ith row of Pascals's triangle
    static int countOddNumber(int row_num)
    {
  
        // Count set bits in row_num
  
        // Initialize count as zero
        int count = 0;
        while (row_num > 0) {
            count += row_num & 1;
            row_num >>= 1;
        }
  
        // Return 2^count
        return (1 << count);
    }
  
    // Function to generate gould's Sequence
    static void gouldSequence(int n)
    {
        // loop to generate gould's Sequence up to n
        for (int row_num = 0; row_num < n; row_num++) {
  
            Console.Write(countOddNumber(row_num) + " ");
        }
    }
  
    // Driver code
    public static void Main()
    {
        // Get n
        int n = 16;
  
        // Function call
        gouldSequence(n);
    }
}

的PHP

>= 1;
    }
  
    // Return 2^count
    return (1 << $count);
}
  
// Function to generate gould's Sequence
function gouldSequence($n)
{
    // loop to generate gould's Sequence up to n
    for ($row_num = 0; 
         $row_num < $n; $row_num++)
    {
  
        echo countOddNumber($row_num), " ";
    }
}
  
// Driver code
  
// Get n
$n = 16;
  
// Function call
gouldSequence($n);
  
// This code is contributed 
// by Sach_Code
?>
输出
1 2 2 4 2 4 4 8 2 4 4 8 4 8 8 16 

更好的解决方案(使用动态编程)是基于以下观察结果:在获得2个较早项的乘方之后,结果加倍。

例如

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

因此,我们可以通过将先前项的值加倍来计算2 i之后的古尔德序列项

以下是上述方法的实现-

C++

// CPP program to generate
// Gould's Sequence
  
#include 
using namespace std;
  
// 32768 = 2^15
#define MAX 32768
  
// Array to store Sequence up to
// 2^16 = 65536
int arr[2 * MAX];
  
// Utility function to pre-compute odd numbers
// in ith row of Pascals's triangle
int gouldSequence()
{
  
    // First term of the Sequence is 1
    arr[0] = 1;
  
    // Initilize i to 1
    int i = 1;
  
    // Initilize p to 1 (i.e 2^i)
    // in each iteration
    // i will be pth power of 2
    int p = 1;
  
    // loop to generate gould's Sequence
    while (i <= MAX) {
  
        // i is pth power of 2
        // traverse the array
        // from j=0 to i i.e (2^p)
  
        int j = 0;
  
        while (j < i) {
  
            // double the value of arr[j]
            // and store to arr[i+j]
            arr[i + j] = 2 * arr[j];
            j++;
        }
  
        // upadate i to next power of 2
        i = (1 << p);
  
        // increment p
        p++;
    }
}
  
// Function to print gould's Sequence
void printSequence(int n)
{
    // loop to generate gould's Sequence up to n
  
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
}
  
// Driver code
int main()
{
  
    gouldSequence();
  
    // Get n
    int n = 16;
  
    // Function call
    printSequence(n);
  
    return 0;
}

Java

// JAVA program to generate
// Gould's Sequence
  
class GFG {
  
    // 32768 = 2^15
    static final int MAX = 32768;
  
    // Array to store Sequence up to
    // 2^16 = 65536
    static int[] arr = new int[2 * MAX];
  
    // Utility function to pre-compute odd numbers
    // in ith row of Pascals's triangle
    static void gouldSequence()
    {
  
        // First term of the Sequence is 1
        arr[0] = 1;
  
        // Initilize i to 1
        int i = 1;
  
        // Initilize p to 1 (i.e 2^i)
        // in each iteration
        // i will be pth power of 2
        int p = 1;
  
        // loop to generate gould's Sequence
        while (i <= MAX) {
  
            // i is pth power of 2
            // traverse the array
            // from j=0 to i i.e (2^p)
  
            int j = 0;
  
            while (j < i) {
                // double the value of arr[j]
                // and store to arr[i+j]
                arr[i + j] = 2 * arr[j];
                j++;
            }
  
            // upadate i to next power of 2
            i = (1 << p);
  
            // increment p
            p++;
        }
    }
  
    // Function to print gould's Sequence
    static void printSequence(int n)
    {
        // loop to generate gould's Sequence up to n
  
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        gouldSequence();
  
        // Get n
        int n = 16;
  
        // Function call
        printSequence(n);
    }
}

Python3

# Python3 program to generate 
# Gould's Sequence 
  
# 32768 = 2^15 
MAX = 32768
  
# Array to store Sequence up to 
# 2^16 = 65536 
arr = [None] * (2 * MAX)
  
# Utility function to pre-compute 
# odd numbers in ith row of Pascals's 
# triangle 
def gouldSequence(): 
  
    # First term of the Sequence is 1 
    arr[0] = 1
  
    # Initilize i to 1 
    i = 1
  
    # Initilize p to 1 (i.e 2^i) 
    # in each iteration 
    # i will be pth power of 2 
    p = 1
  
    # loop to generate gould's Sequence 
    while i <= MAX: 
  
        # i is pth power of 2 
        # traverse the array 
        # from j=0 to i i.e (2^p) 
        j = 0
  
        while j < i: 
  
            # double the value of arr[j] 
            # and store to arr[i+j] 
            arr[i + j] = 2 * arr[j] 
            j += 1
          
        # upadate i to next power of 2 
        i = (1 << p) 
  
        # increment p 
        p += 1
      
# Function to print gould's Sequence 
def printSequence(n): 
  
    # loop to generate gould's Sequence 
    # up to n 
    for i in range(0, n):
        print(arr[i], end = " ") 
      
# Driver code 
if __name__ == "__main__": 
  
    gouldSequence() 
  
    # Get n 
    n = 16
  
    # Function call 
    printSequence(n) 
  
# This code is contributed
# by Rituraj Jain

C#

// C# program to generate
// Gould's Sequence
  
using System;
class GFG {
  
    // 32768 = 2^15
    static int MAX = 32768;
  
    // Array to store Sequence up to
    // 2^16 = 65536
    static int[] arr = new int[2 * MAX];
  
    // Utility function to pre-compute odd numbers
    // in ith row of Pascals's triangle
    static void gouldSequence()
    {
  
        // First term of the Sequence is 1
        arr[0] = 1;
  
        // Initialize i to 1
        int i = 1;
  
        // Initialize p to 1 (i.e 2^i)
        // in each iteration
        // i will be pth power of 2
        int p = 1;
  
        // loop to generate gould's Sequence
        while (i <= MAX) {
  
            // i is pth power of 2
            // traverse the array
            // from j=0 to i i.e (2^p)
  
            int j = 0;
  
            while (j < i) {
                // double the value of arr[j]
                // and store to arr[i+j]
                arr[i + j] = 2 * arr[j];
                j++;
            }
  
            // upadate i to next power of 2
            i = (1 << p);
  
            // increment p
            p++;
        }
    }
  
    // Function to print gould's Sequence
    static void printSequence(int n)
    {
        // loop to generate gould's Sequence up to n
  
        for (int i = 0; i < n; i++) {
            Console.Write(arr[i] + " ");
        }
    }
  
    // Driver code
    public static void Main()
    {
  
        gouldSequence();
  
        // Get n
        int n = 16;
  
        // Function call
        printSequence(n);
    }
}

的PHP


输出
1 2 2 4 2 4 4 8 2 4 4 8 4 8 8 16