-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import java.util.Scanner; | ||
|
||
public class SylvesterSequence { | ||
public static void main(String[] args) { | ||
Scanner scan = new Scanner(System.in); | ||
System.out.print("Enter the nth value to find : "); | ||
int n = scan.nextInt(); | ||
System.out.println(n + "th value in Sylvester Sequence is :" + seq(n)); | ||
} | ||
|
||
private static int seq(int n) { | ||
int pdt = 1, ans = 2, N = 1000000007; | ||
for(int i=1; i<n+1; i++){ | ||
ans = ((pdt % N) * (ans % N)) % N; | ||
pdt = ans; | ||
ans = (ans + 1) % N; | ||
} | ||
return ans; | ||
} | ||
} |