Created
January 18, 2019 10:48
-
-
Save hkurokawa/ecb0fdee1e1997bc0d9fc2c40df96a79 to your computer and use it in GitHub Desktop.
LucasTest
This file contains hidden or 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
import java.math.BigInteger; | |
public class LucasTest { | |
public static void main(String[] args) { | |
int p = 521; | |
System.out.println(lucas(p - 1, BigInteger.valueOf(2).pow(p).subtract(BigInteger.ONE))); | |
} | |
static BigInteger lucas(int n, BigInteger mod) { | |
return lucasIter(BigInteger.valueOf(4), n, mod); | |
} | |
static BigInteger lucasIter(BigInteger a, int n, BigInteger m) { | |
BigInteger r = a.mod(m); | |
if (n == 1) return r; | |
BigInteger b = r.multiply(r).mod(m).subtract(BigInteger.valueOf(2)); | |
if (b.compareTo(BigInteger.ZERO) < 0) b = b.add(m); | |
return lucasIter(b, n - 1, m); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment