Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,23 +1,30 @@
|
|||
import std.stdio, std.bigint;
|
||||
module modular_exponentiation;
|
||||
|
||||
BigInt powMod(BigInt base, BigInt exponent, BigInt modulus)
|
||||
in {
|
||||
private import std.bigint;
|
||||
|
||||
BigInt powMod(BigInt base, BigInt exponent, in BigInt modulus)
|
||||
pure nothrow /*@safe*/ in {
|
||||
assert(exponent >= 0);
|
||||
} body {
|
||||
BigInt result = 1;
|
||||
while (exponent > 0) {
|
||||
if (exponent % 2 == 1)
|
||||
|
||||
while (exponent) {
|
||||
if (exponent & 1)
|
||||
result = (result * base) % modulus;
|
||||
exponent /= 2;
|
||||
base = base ^^ 2 % modulus;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
powMod(BigInt("29883481620585741369158914214988194" ~
|
||||
"66320163312926952423791023078876139"),
|
||||
BigInt("235139930337346448646612254452369009" ~
|
||||
"4744975233415544072992656881240319"),
|
||||
BigInt(10) ^^ 40).writeln();
|
||||
}
|
||||
version (modular_exponentiation)
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
powMod(BigInt("29883481620585741369158914214988194" ~
|
||||
"66320163312926952423791023078876139"),
|
||||
BigInt("235139930337346448646612254452369009" ~
|
||||
"4744975233415544072992656881240319"),
|
||||
BigInt(10) ^^ 40).writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
/*REXX program displays modular exponentation: a**b mod M */
|
||||
parse arg a b mm /*get optional args from CL.*/
|
||||
if a=='' | a==',' then a=2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
if b=='' | b==',' then b=2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
if mm='' | mm=',' then mm=40 /*MM specified? Use default.*/
|
||||
say 'a=' a; say ' ('length(a) "digits)" /*show value of A.*/
|
||||
say 'b=' b; say ' ('length(b) "digits)" /* " " " B.*/
|
||||
|
||||
do j=1 for words(mm); m=word(mm,j) /*use one of the MM powers.*/
|
||||
say copies('─',linesize()-1) /*show a nice separator line*/
|
||||
say 'a**b (mod 10**'m")=" powerModulated(a,b,10**m) /*show ans*/
|
||||
end /*j*/
|
||||
exit /*stick a fork in it; done.*/
|
||||
/*──────────────────────────────────────POWERMODULATED subroutine───────*/
|
||||
powerModulated: procedure; parse arg x,p,n /*fast modular exponentation*/
|
||||
if p==0 then return 1 /*special case of P = zero. */
|
||||
if p==1 then return x /* " " " " = unity.*/
|
||||
if p<0 then do; say '***error!*** power is negative:' p; exit 13; end
|
||||
parse value max(x**2,p,n)'E0' with "E" e /*pick biggest of the three.*/
|
||||
numeric digits max(20,e*2) /*big enough to handle A² */
|
||||
_=1 /*use this for the 1st value*/
|
||||
do while p\==0; if p//2==1 then _=_*x//n /*is P odd? */
|
||||
p=p%2; x=x*x//n /*halve P; calc x² mod n */
|
||||
end /*while*/ /* [↑] keep moding 'til =0.*/
|
||||
return _
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
/* Modular exponentiation */
|
||||
|
||||
numeric digits 100
|
||||
say powerMod(,
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139,,
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319,,
|
||||
1e40)
|
||||
exit
|
||||
|
||||
powerMod: procedure
|
||||
|
||||
parse arg base, exponent, modulus
|
||||
|
||||
exponent = strip(x2b(d2x(exponent)), 'L', '0')
|
||||
result = 1
|
||||
base = base // modulus
|
||||
do exponentPos=length(exponent) to 1 by -1
|
||||
if substr(exponent, exponentPos, 1) = '1'
|
||||
then result = (result * base) // modulus
|
||||
base = (base * base) // modulus
|
||||
end
|
||||
return result
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
/*REXX program to show modular exponentation: a**b mod M */
|
||||
parse arg a b mm /*get the arguments (maybe).*/
|
||||
if a=='' | a==',' then a=,
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
|
||||
if b=='' | b==',' then b=,
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
|
||||
if mm=='' then mm=40
|
||||
say 'a=' a; say ' ('length(a) "digits)"
|
||||
say 'b=' b; say ' ('length(b) "digits)"
|
||||
|
||||
do j=1 for words(mm); m=word(mm,j); say copies('─',linesize()-1)
|
||||
say 'a**b (mod 10**'m")=" powerModulated(a,b,10**m)
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────────POWERMODULATED subroutine───────*/
|
||||
powerModulated: procedure; parse arg x,p,n /*fast modular exponentation*/
|
||||
if p==0 then return 1 /*special case. */
|
||||
if p==1 then return x /*special case. */
|
||||
if p<0 then do; say '***error!*** power is negative:' p; exit 13; end
|
||||
parse value max(x**2,p,n)'E0' with "E" e /*pick biggest of the three.*/
|
||||
numeric digits max(20,e*2) /*big enough to handle A² */
|
||||
_=1
|
||||
do while p\==0; if p//2==1 then _=_*x//n
|
||||
p=p%2; x=x*x // n
|
||||
end /*while*/
|
||||
return _
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#lang racket
|
||||
(require math)
|
||||
(define a 2988348162058574136915891421498819466320163312926952423791023078876139)
|
||||
(define b 2351399303373464486466122544523690094744975233415544072992656881240319)
|
||||
(define m (expt 10 40))
|
||||
(modular-expt a b m)
|
||||
Loading…
Add table
Add a link
Reference in a new issue