Sylvester 's sequence | CPP program to print terms of Sylvester 's sequence ; To store the product . ; To store the current number . ; Loop till n . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000000007 NEW_LINE void printSequence ( int n ) { int a = 1 ; int ans = 2 ; for ( int i = 1 ; i <= n ; i ++ ) { cout << ans << " ▁ " ; ans = ( ( a % N ) * ( ans % N ) ) % N ; a = ans ; ans = ( ans + 1 ) % N ; } } int main ( ) { int n = 6 ; printSequence ( n ) ; return 0 ; }