Sylvester’s sequence

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++

// 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 Program
int main()
{
    int n = 6;
    printSequence(n);
    return 0;
}

Java

// JAVA Code for Sylvester sequence
import 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

# 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 function 
n = 6
printSequence(n)
  
# This code is contributed by Nikita Tiwari.

C#

// C# Code for Sylvester sequence
using 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
// 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.
?>


Output:

2 3 7 43 1807 3263443

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.



My Personal Notes arrow_drop_up

Improved By : vt_m




Practice Tags :

Recommended Posts:



1 Average Difficulty : 1/5.0
Based on 1 vote(s)






User Actions