Saturday, January 11, 2025
Google search engine
HomeData Modelling & AISylvester’s sequence

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.
?>


Javascript




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

If you like neveropen 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 neveropen 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.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments