Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -3,6 +3,6 @@ Find the last 40 decimal digits of <math>a^b</math>, where
* <math>a = 2988348162058574136915891421498819466320163312926952423791023078876139</math>
* <math>b = 2351399303373464486466122544523690094744975233415544072992656881240319</math>
A computer is too slow to find the entire value of <math>a^b</math>. Instead, the program must use a fast algorithm for modular exponentiation: <math>a^b \mod m</math>.
A computer is too slow to find the entire value of <math>a^b</math>. Instead, the program must use a fast algorithm for [[wp:Modular exponentiation|modular exponentiation]]: <math>a^b \mod m</math>.
The algorithm must work for any integers <math>a, b, m</math> where <math>b \ge 0</math> and <math>m > 0</math>.

View file

@ -0,0 +1,23 @@
BEGIN
PR precision=1000 PR
MODE LLI = LONG LONG INT; CO For brevity CO
PROC mod power = (LLI base, exponent, modulus) LLI :
BEGIN
LLI result := 1, b := base, e := exponent;
IF exponent < 0
THEN
put (stand error, (("Negative exponent", exponent, newline)))
ELSE
WHILE e > 0
DO
(ODD e | result := (result * b) MOD modulus);
e OVERAB 2; b := (b * b) MOD modulus
OD
FI;
result
END;
LLI a = 2988348162058574136915891421498819466320163312926952423791023078876139;
LLI b = 2351399303373464486466122544523690094744975233415544072992656881240319;
LLI m = 10000000000000000000000000000000000000000;
printf (($"Last 40 digits = ", 40dl$, mod power (a, b, m)))
END

View file

@ -0,0 +1,6 @@
let a = Z.of_string "2988348162058574136915891421498819466320163312926952423791023078876139" in
let b = Z.of_string "2351399303373464486466122544523690094744975233415544072992656881240319" in
let m = Z.pow (Z.of_int 10) 40 in
Z.powm a b m
|> Z.to_string
|> print_endline