Sylvester’s sequence

21 Jun, 2022
In number system, Sylvester’s sequence is an integer sequence in which each member of the sequence is the product of the previous members, plus one. Given a positive integer N. The task is to print the first N member of the sequence. Since numbers can be very big, use %10^9 + 7.Examples:  
Input : N = 6
Output : 2 3 7 43 1807 3263443

Input : N = 2
Output : 2 3
 
The idea is to run a loop and take two variables and initialise them as 1 and 2, one to store the product till now and other to store the current number which is nothing but the first number + 1 and for each step multiply both using arithmetic modular operation i.e (a + b)%N = (a%N + b%N)%N where N is a modular number.Below is the implementation of this approach:  
C++
Java
Python
C#
PHP
Javascript
// CPP program to print terms of Sylvester's sequence#include <bits/stdc++.h>using namespace std;#define N 1000000007 void printSequence(int n){    int a = 1; // To store the product.    int ans = 2; // To store the current number.     // Loop till n.    for (int i = 1; i <= n; i++) {        cout << ans << " ";        ans = ((a % N) * (ans % N)) % N;        a = ans;        ans = (ans + 1) % N;    }} // Driven Programint main(){    int n = 6;    printSequence(n);    return 0;}
// JAVA Code for Sylvester sequenceimport java.util.*; class GFG {         public static void printSequence(int n)    {        int a = 1; // To store the product.        int ans = 2; // To store the current number.        int N = 1000000007;                 // Loop till n.        for (int i = 1; i <= n; i++) {           System.out.print(ans + " ");            ans = ((a % N) * (ans % N)) % N;            a = ans;            ans = (ans + 1) % N;        }    }     /* Driver program to test above function */    public static void main(String[] args)    {        int n = 6;        printSequence(n);             }}   // This code is contributed by Arnav Kr. Mandal.
# Python Code for Sylvester sequence def printSequence(n) :    a = 1 # To store the product.    ans = 2 # To store the current number.    N = 1000000007         # Loop till n.    i = 1    while i <= n :        print ans,        ans = ((a % N) * (ans % N)) % N        a = ans        ans = (ans + 1) % N        i = i + 1          # Driver program to test above functionn = 6printSequence(n) # This code is contributed by Nikita Tiwari.
// C# Code for Sylvester sequenceusing System; class GFG {         public static void printSequence(int n)    {         // To store the product.        int a = 1;                 // To store the current number.        int ans = 2;                 int N = 1000000007;                 // Loop till n.        for (int i = 1; i <= n; i++)        {            Console.Write(ans + " ");            ans = ((a % N) * (ans % N)) % N;            a = ans;            ans = (ans + 1) % N;        }    }     // Driver program    public static void Main()    {        int n = 6;        printSequence(n);             }} // This code is contributed by vt_m.
<?php// PHP program to print// terms of Sylvester's sequence $N = 1000000007; function printSequence($n){    global $N;         // To store    // the product.    $a = 1;         // To store the    // current number.    $ans = 2;     // Loop till n.    for ($i = 1; $i <= $n; $i++)    {        echo $ans ," ";        $ans = (($a % $N) * ($ans % $N)) % $N;        $a = $ans;        $ans = ($ans + 1) % $N;    }}     // Driver Code    $n = 6;    printSequence($n);     // This code is contributed by anuj_67.?>
<script>// Javascript program to print// terms of Sylvester's sequence let N = 1000000007; function printSequence(n){     // To store    // the product.    let a = 1;         // To store the    // current number.    let ans = 2;     // Loop till n.    for (let i = 1; i <= n; i++)    {        document.write(ans + " ");        ans = ((a % N) * (ans % N)) % N;        a = ans;        ans = (ans + 1) % N;    }}     // Driver Code    let n = 6;    printSequence(n);     // This code is contributed by gfgking.</script>
Output:  
2 3 7 43 1807 3263443
Time complexity : O(n) Auxiliary Space : O(1)
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
gfgking
technophpfij
series
Mathematical
School Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java