Skip to content

Commit

Permalink
Added Sylvester Sequence in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
PremApk committed Aug 24, 2021
1 parent cb74c07 commit 4644bf5
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Sylvester Sequence/SylvesterSequence.java
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;
}
}

0 comments on commit 4644bf5

Please sign in to comment.