Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,48 @@
|
|||
/* Add this line to the [dependencies] section of your Cargo.toml file:
|
||||
num = "0.2.0"
|
||||
*/
|
||||
|
||||
|
||||
use num::bigint::BigInt;
|
||||
use num::bigint::ToBigInt;
|
||||
|
||||
|
||||
// The modular_exponentiation() function takes three identical types
|
||||
// (which get cast to BigInt), and returns a BigInt:
|
||||
fn modular_exponentiation<T: ToBigInt>(n: &T, e: &T, m: &T) -> BigInt {
|
||||
// Convert n, e, and m to BigInt:
|
||||
let n = n.to_bigint().unwrap();
|
||||
let e = e.to_bigint().unwrap();
|
||||
let m = m.to_bigint().unwrap();
|
||||
|
||||
// Sanity check: Verify that the exponent is not negative:
|
||||
assert!(e >= Zero::zero());
|
||||
|
||||
use num::traits::{Zero, One};
|
||||
|
||||
// As most modular exponentiations do, return 1 if the exponent is 0:
|
||||
if e == Zero::zero() {
|
||||
return One::one()
|
||||
}
|
||||
|
||||
// Now do the modular exponentiation algorithm:
|
||||
let mut result: BigInt = One::one();
|
||||
let mut base = n % &m;
|
||||
let mut exp = e;
|
||||
|
||||
// Loop until we can return out result:
|
||||
loop {
|
||||
if &exp % 2 == One::one() {
|
||||
result *= &base;
|
||||
result %= &m;
|
||||
}
|
||||
|
||||
if exp == One::one() {
|
||||
return result
|
||||
}
|
||||
|
||||
exp /= 2;
|
||||
base *= base.clone();
|
||||
base %= &m;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
fn main() {
|
||||
let (a, b, num_digits) = (
|
||||
"2988348162058574136915891421498819466320163312926952423791023078876139",
|
||||
"2351399303373464486466122544523690094744975233415544072992656881240319",
|
||||
"40",
|
||||
);
|
||||
|
||||
// Covert a, b, and num_digits to numbers:
|
||||
let a = BigInt::parse_bytes(a.as_bytes(), 10).unwrap();
|
||||
let b = BigInt::parse_bytes(b.as_bytes(), 10).unwrap();
|
||||
let num_digits = num_digits.parse().unwrap();
|
||||
|
||||
// Calculate m from num_digits:
|
||||
let m = num::pow::pow(10.to_bigint().unwrap(), num_digits);
|
||||
|
||||
// Get the result and print it:
|
||||
let result = modular_exponentiation(&a, &b, &m);
|
||||
println!("The last {} digits of\n{}\nto the power of\n{}\nare:\n{}",
|
||||
num_digits, a, b, result);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue