mirror of
https://github.com/zach-sb/SENG2250_Assignment3.git
synced 2025-11-09 04:47:36 +11:00
Bigint
This commit is contained in:
parent
a0ee0db03b
commit
e5967e6e7a
49
Q2Tools.java
49
Q2Tools.java
@ -26,36 +26,33 @@ public class Q2Tools {
|
|||||||
* @param B
|
* @param B
|
||||||
* @param C
|
* @param C
|
||||||
*/
|
*/
|
||||||
public void fastModularExpon(int A, int B, int C)
|
public BigInteger fastModularExpon(String base, String exponent, String modulus)
|
||||||
{
|
{
|
||||||
String bInBinary = Integer.toBinaryString(B);
|
// BigInteger baseVal = new BigInteger(base);
|
||||||
|
// BigInteger exponentVal = new BigInteger(exponent);
|
||||||
double test = 1;
|
// BigInteger modulusVal = new BigInteger(modulus);
|
||||||
|
|
||||||
{
|
//modulusVal.equals(BigInteger.valueOf(1));
|
||||||
int k = 0;
|
if(modulusVal == 1)
|
||||||
//Loop through the Binary String, starting from the right
|
{
|
||||||
for(int i = bInBinary.length()-1; i >= 0; i--)
|
return BigInteger.valueOf(0);
|
||||||
{
|
|
||||||
|
|
||||||
if(bInBinary.charAt(i)=='1')
|
|
||||||
{
|
|
||||||
System.out.println("Loop: "+k);
|
|
||||||
double step1 = Math.pow(2, k);
|
|
||||||
System.out.println("STEP1: "+step1);
|
|
||||||
|
|
||||||
double step2 = Math.pow(A, step1)%C;
|
|
||||||
System.out.println("STEP2: "+step2);
|
|
||||||
test= test*step2;
|
|
||||||
|
|
||||||
}
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BigInteger temp = new BigInteger("1");
|
||||||
System.out.println(test);
|
|
||||||
|
|
||||||
System.out.println(Math.pow(5, 64)%19);
|
while(exponentVal>0)
|
||||||
|
{
|
||||||
|
if((exponentVal & 1)==1)
|
||||||
|
{
|
||||||
|
// temp = (temp * baseVal) % modulusVal;
|
||||||
|
temp = temp.multiply(BigInteger.valueOf(baseVal));
|
||||||
|
temp = temp.mod(BigInteger.valueOf(modulusVal));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
exponentVal = exponentVal >> 1;
|
||||||
|
baseVal = (baseVal*baseVal) % modulusVal;
|
||||||
|
}
|
||||||
|
return temp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user