Related Articles

# Sylvester’s sequence

• Difficulty Level : Basic
• Last Updated : 16 Apr, 2021

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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

 ``

## Javascript

 ``

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.