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.
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:
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 firstname.lastname@example.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Look-and-Say Sequence
- Sum of the sequence 2, 22, 222, .........
- k-th number in the Odd-Even sequence
- Gijswijt's Sequence
- Alcuin's Sequence
- Aronson's Sequence
- Gould's Sequence
- Connell Sequence
- Find F(n) when F(i) and F(j) of a sequence are given
- Farey Sequence
- Increasing sequence with given GCD
- Beatty sequence
- Golomb sequence
- Padovan Sequence
- Aliquot Sequence
- Juggler Sequence
- Recaman's sequence
- Smarandache-Wellin Sequence
- Program to find sum of the given sequence
- Sum of numbers from 1 to N which are in Lucas Sequence
Improved By : vt_m