Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Modular-exponentiation/00-META.yaml
Normal file
2
Task/Modular-exponentiation/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Modular_exponentiation
|
||||
13
Task/Modular-exponentiation/00-TASK.txt
Normal file
13
Task/Modular-exponentiation/00-TASK.txt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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 [[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>.
|
||||
<br><br>
|
||||
|
||||
14
Task/Modular-exponentiation/11l/modular-exponentiation.11l
Normal file
14
Task/Modular-exponentiation/11l/modular-exponentiation.11l
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
F pow_mod(BigInt =base, BigInt =exponent, BigInt modulus)
|
||||
BigInt result = 1
|
||||
|
||||
L exponent != 0
|
||||
I exponent % 2 != 0
|
||||
result = (result * base) % modulus
|
||||
exponent I/= 2
|
||||
base = (base * base) % modulus
|
||||
|
||||
R result
|
||||
|
||||
print(pow_mod(BigInt(‘2988348162058574136915891421498819466320163312926952423791023078876139’),
|
||||
BigInt(‘2351399303373464486466122544523690094744975233415544072992656881240319’),
|
||||
BigInt(10) ^ 40))
|
||||
|
|
@ -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
|
||||
65
Task/Modular-exponentiation/ATS/modular-exponentiation.ats
Normal file
65
Task/Modular-exponentiation/ATS/modular-exponentiation.ats
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
(* You will need
|
||||
https://sourceforge.net/p/chemoelectric/ats2-xprelude/ *)
|
||||
|
||||
#include "share/atspre_staload.hats"
|
||||
|
||||
#include "xprelude/HATS/xprelude.hats"
|
||||
staload "xprelude/SATS/exrat.sats"
|
||||
staload _ = "xprelude/DATS/exrat.dats"
|
||||
|
||||
val a = exrat_make_string_exn "2988348162058574136915891421498819466320163312926952423791023078876139"
|
||||
val b = exrat_make_string_exn "2351399303373464486466122544523690094744975233415544072992656881240319"
|
||||
|
||||
val modulus = exrat_make (10, 1) ** 40
|
||||
|
||||
(* xprelude/SATS/exrat.sats includes the "exrat_numerator_modular_pow"
|
||||
function, based on GMP's mpz_powm. *)
|
||||
val result1 = exrat_numerator_modular_pow (a, b, modulus)
|
||||
|
||||
(* But that was too easy. Here is the right-to-left binary method,
|
||||
https://en.wikipedia.org/w/index.php?title=Modular_exponentiation&oldid=1136216610#Right-to-left_binary_method
|
||||
*)
|
||||
val result2 =
|
||||
(lam (base : exrat,
|
||||
exponent : exrat,
|
||||
modulus : exrat) : exrat =>
|
||||
let
|
||||
val zero = exrat_make (0, 1)
|
||||
and one = exrat_make (1, 1)
|
||||
and two = exrat_make (2, 1)
|
||||
macdef divrem = exrat_numerator_euclid_division
|
||||
macdef rem = exrat_numerator_euclid_remainder
|
||||
in
|
||||
if modulus = one then
|
||||
zero
|
||||
else
|
||||
let
|
||||
fun
|
||||
loop (result : exrat,
|
||||
base : exrat,
|
||||
exponent : exrat) : exrat =
|
||||
if iseqz exponent then
|
||||
result
|
||||
else
|
||||
let
|
||||
val @(exponent, remainder) = exponent \divrem two
|
||||
val result =
|
||||
if remainder = one then
|
||||
(result * base) \rem modulus
|
||||
else
|
||||
result
|
||||
val base = (base * base) \rem modulus
|
||||
in
|
||||
loop (result, base, exponent)
|
||||
end
|
||||
in
|
||||
loop (one, base \rem modulus, exponent)
|
||||
end
|
||||
end) (a, b, modulus)
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
begin
|
||||
println! result1;
|
||||
println! result2
|
||||
end
|
||||
26
Task/Modular-exponentiation/Ada/modular-exponentiation.ada
Normal file
26
Task/Modular-exponentiation/Ada/modular-exponentiation.ada
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
with Ada.Text_IO, Ada.Command_Line, Crypto.Types.Big_Numbers;
|
||||
|
||||
procedure Mod_Exp is
|
||||
|
||||
A: String :=
|
||||
"2988348162058574136915891421498819466320163312926952423791023078876139";
|
||||
B: String :=
|
||||
"2351399303373464486466122544523690094744975233415544072992656881240319";
|
||||
|
||||
D: constant Positive := Positive'Max(Positive'Max(A'Length, B'Length), 40);
|
||||
-- the number of decimals to store A, B, and result
|
||||
Bits: constant Positive := (34*D)/10;
|
||||
-- (slightly more than) the number of bits to store A, B, and result
|
||||
package LN is new Crypto.Types.Big_Numbers (Bits + (32 - Bits mod 32));
|
||||
-- the actual number of bits has to be a multiple of 32
|
||||
use type LN.Big_Unsigned;
|
||||
|
||||
function "+"(S: String) return LN.Big_Unsigned
|
||||
renames LN.Utils.To_Big_Unsigned;
|
||||
|
||||
M: LN.Big_Unsigned := (+"10") ** (+"40");
|
||||
|
||||
begin
|
||||
Ada.Text_IO.Put("A**B (mod 10**40) = ");
|
||||
Ada.Text_IO.Put_Line(LN.Utils.To_String(LN.Mod_Utils.Pow((+A), (+B), M)));
|
||||
end Mod_Exp;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
a: 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
b: 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
|
||||
loop [40 80 180 888] 'm ->
|
||||
print ["(a ^ b) % 10 ^" m "=" powmod a b 10^m]
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#NoEnv
|
||||
#SingleInstance, Force
|
||||
SetBatchLines, -1
|
||||
#Include mpl.ahk
|
||||
|
||||
MP_SET(base, "2988348162058574136915891421498819466320163312926952423791023078876139")
|
||||
, MP_SET(exponent, "2351399303373464486466122544523690094744975233415544072992656881240319")
|
||||
, MP_SET(modulus, "10000000000000000000000000000000000000000")
|
||||
|
||||
, NumGet(exponent,0,"Int") = -1 ? return : ""
|
||||
, MP_SET(result, "1")
|
||||
, MP_SET(TWO, "2")
|
||||
while !MP_IS0(exponent)
|
||||
MP_DIV(q, r, exponent, TWO)
|
||||
, (MP_DEC(r) = 1
|
||||
? (MP_MUL(temp, result, base)
|
||||
, MP_DIV(q, result, temp, modulus))
|
||||
: "")
|
||||
, MP_DIV(q, r, exponent, TWO)
|
||||
, MP_CPY(exponent, q)
|
||||
, MP_CPY(base1, base)
|
||||
, MP_MUL(base2, base1, base)
|
||||
, MP_DIV(q, base, base2, modulus)
|
||||
|
||||
msgbox % MP_DEC(result)
|
||||
Return
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
INSTALL @lib$+"HIMELIB"
|
||||
PROC_himeinit("")
|
||||
|
||||
PROC_hiputdec(1, "2988348162058574136915891421498819466320163312926952423791023078876139")
|
||||
PROC_hiputdec(2, "2351399303373464486466122544523690094744975233415544072992656881240319")
|
||||
PROC_hiputdec(3, "10000000000000000000000000000000000000000")
|
||||
h1% = 1 : h2% = 2 : h3% = 3 : h4% = 4
|
||||
SYS `hi_PowMod`, ^h1%, ^h2%, ^h3%, ^h4%
|
||||
PRINT FN_higetdec(4)
|
||||
12
Task/Modular-exponentiation/Bc/modular-exponentiation.bc
Normal file
12
Task/Modular-exponentiation/Bc/modular-exponentiation.bc
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
define p(n, e, m) {
|
||||
auto r
|
||||
for (r = 1; e > 0; e /= 2) {
|
||||
if (e % 2 == 1) r = n * r % m
|
||||
n = n * n % m
|
||||
}
|
||||
return(r)
|
||||
}
|
||||
|
||||
a = 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
b = 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
p(a, b, 10 ^ 40)
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
( ( mod-power
|
||||
= base exponent modulus result
|
||||
. !arg:(?base,?exponent,?modulus)
|
||||
& !exponent:~<0
|
||||
& 1:?result
|
||||
& whl
|
||||
' ( !exponent:>0
|
||||
& ( ( mod$(!exponent.2):1
|
||||
& mod$(!result*!base.!modulus):?result
|
||||
& -1
|
||||
| 0
|
||||
)
|
||||
+ !exponent
|
||||
)
|
||||
* 1/2
|
||||
: ?exponent
|
||||
& mod$(!base^2.!modulus):?base
|
||||
)
|
||||
& !result
|
||||
)
|
||||
& ( a
|
||||
= 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
)
|
||||
& ( b
|
||||
= 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
)
|
||||
& out$("last 40 digits = " mod-power$(!a,!b,10^40))
|
||||
)
|
||||
13
Task/Modular-exponentiation/C++/modular-exponentiation.cpp
Normal file
13
Task/Modular-exponentiation/C++/modular-exponentiation.cpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include <iostream>
|
||||
#include <boost/multiprecision/cpp_int.hpp>
|
||||
#include <boost/multiprecision/integer.hpp>
|
||||
|
||||
int main() {
|
||||
using boost::multiprecision::cpp_int;
|
||||
using boost::multiprecision::pow;
|
||||
using boost::multiprecision::powm;
|
||||
cpp_int a("2988348162058574136915891421498819466320163312926952423791023078876139");
|
||||
cpp_int b("2351399303373464486466122544523690094744975233415544072992656881240319");
|
||||
std::cout << powm(a, b, pow(cpp_int(10), 40)) << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main() {
|
||||
var a = BigInteger.Parse("2988348162058574136915891421498819466320163312926952423791023078876139");
|
||||
var b = BigInteger.Parse("2351399303373464486466122544523690094744975233415544072992656881240319");
|
||||
var m = BigInteger.Pow(10, 40);
|
||||
Console.WriteLine(BigInteger.ModPow(a, b, m));
|
||||
}
|
||||
}
|
||||
25
Task/Modular-exponentiation/C/modular-exponentiation.c
Normal file
25
Task/Modular-exponentiation/C/modular-exponentiation.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include <gmp.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
mpz_t a, b, m, r;
|
||||
|
||||
mpz_init_set_str(a, "2988348162058574136915891421498819466320"
|
||||
"163312926952423791023078876139", 0);
|
||||
mpz_init_set_str(b, "2351399303373464486466122544523690094744"
|
||||
"975233415544072992656881240319", 0);
|
||||
mpz_init(m);
|
||||
mpz_ui_pow_ui(m, 10, 40);
|
||||
|
||||
mpz_init(r);
|
||||
mpz_powm(r, a, b, m);
|
||||
|
||||
gmp_printf("%Zd\n", r); /* ...16808958343740453059 */
|
||||
|
||||
mpz_clear(a);
|
||||
mpz_clear(b);
|
||||
mpz_clear(m);
|
||||
mpz_clear(r);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(defn powerMod "modular exponentiation" [b e m]
|
||||
(defn m* [p q] (mod (* p q) m))
|
||||
(loop [b b, e e, x 1]
|
||||
(if (zero? e) x
|
||||
(if (even? e) (recur (m* b b) (/ e 2) x)
|
||||
(recur (m* b b) (quot e 2) (m* b x))))))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(defn modpow
|
||||
" b^e mod m (using Java which solves some cases the pure clojure method has to be modified to tackle--i.e. with large b & e and
|
||||
calculation simplications when gcd(b, m) == 1 and gcd(e, m) == 1) "
|
||||
[b e m]
|
||||
(.modPow (biginteger b) (biginteger e) (biginteger m)))
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
(defun rosetta-mod-expt (base power divisor)
|
||||
"Return BASE raised to the POWER, modulo DIVISOR.
|
||||
This function is faster than (MOD (EXPT BASE POWER) DIVISOR), but
|
||||
only works when POWER is a non-negative integer."
|
||||
(setq base (mod base divisor))
|
||||
;; Multiply product with base until power is zero.
|
||||
(do ((product 1))
|
||||
((zerop power) product)
|
||||
;; Square base, and divide power by 2, until power becomes odd.
|
||||
(do () ((oddp power))
|
||||
(setq base (mod (* base base) divisor)
|
||||
power (ash power -1)))
|
||||
(setq product (mod (* product base) divisor)
|
||||
power (1- power))))
|
||||
|
||||
(let ((a 2988348162058574136915891421498819466320163312926952423791023078876139)
|
||||
(b 2351399303373464486466122544523690094744975233415544072992656881240319))
|
||||
(format t "~A~%" (rosetta-mod-expt a b (expt 10 40))))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
;; CLISP provides EXT:MOD-EXPT
|
||||
(let ((a 2988348162058574136915891421498819466320163312926952423791023078876139)
|
||||
(b 2351399303373464486466122544523690094744975233415544072992656881240319))
|
||||
(format t "~A~%" (mod-expt a b (expt 10 40))))
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(defun mod-expt (a n m)
|
||||
(loop with c = 1 while (plusp n) do
|
||||
(if (oddp n) (setf c (mod (* a c) m)))
|
||||
(setf n (ash n -1))
|
||||
(setf a (mod (* a a) m))
|
||||
finally (return c)))
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
require "big"
|
||||
|
||||
module Integer
|
||||
module Powmod
|
||||
|
||||
# Compute self**e mod m
|
||||
def powmod(e, m)
|
||||
r, b = 1, self.to_big_i
|
||||
while e > 0
|
||||
r = (b * r) % m if e.odd?
|
||||
b = (b * b) % m
|
||||
e >>= 1
|
||||
end
|
||||
r
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
struct Int; include Integer::Powmod end
|
||||
|
||||
a = "2988348162058574136915891421498819466320163312926952423791023078876139".to_big_i
|
||||
b = "2351399303373464486466122544523690094744975233415544072992656881240319".to_big_i
|
||||
m = 10.to_big_i ** 40
|
||||
|
||||
puts a.powmod(b, m)
|
||||
30
Task/Modular-exponentiation/D/modular-exponentiation.d
Normal file
30
Task/Modular-exponentiation/D/modular-exponentiation.d
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
module modular_exponentiation;
|
||||
|
||||
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) {
|
||||
if (exponent & 1)
|
||||
result = (result * base) % modulus;
|
||||
exponent /= 2;
|
||||
base = base ^^ 2 % modulus;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
version (modular_exponentiation)
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
powMod(BigInt("29883481620585741369158914214988194" ~
|
||||
"66320163312926952423791023078876139"),
|
||||
BigInt("235139930337346448646612254452369009" ~
|
||||
"4744975233415544072992656881240319"),
|
||||
BigInt(10) ^^ 40).writeln;
|
||||
}
|
||||
1
Task/Modular-exponentiation/Dc/modular-exponentiation.dc
Normal file
1
Task/Modular-exponentiation/Dc/modular-exponentiation.dc
Normal file
|
|
@ -0,0 +1 @@
|
|||
2988348162058574136915891421498819466320163312926952423791023078876139 2351399303373464486466122544523690094744975233415544072992656881240319 10 40^|p
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
program Modular_exponentiation;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
Velthuis.BigIntegers;
|
||||
|
||||
var
|
||||
a, b, m: BigInteger;
|
||||
|
||||
begin
|
||||
a := BigInteger.Parse('2988348162058574136915891421498819466320163312926952423791023078876139');
|
||||
b := BigInteger.Parse('2351399303373464486466122544523690094744975233415544072992656881240319');
|
||||
m := BigInteger.Pow(10, 40);
|
||||
Writeln(BigInteger.ModPow(a, b, m).ToString);
|
||||
readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
(lib 'bigint)
|
||||
|
||||
(define a 2988348162058574136915891421498819466320163312926952423791023078876139)
|
||||
(define b 2351399303373464486466122544523690094744975233415544072992656881240319)
|
||||
(define m 1e40)
|
||||
|
||||
(powmod a b m)
|
||||
→ 1527229998585248450016808958343740453059
|
||||
|
||||
;; powmod is a native function
|
||||
;; it could be defined as follows :
|
||||
|
||||
(define (xpowmod base exp mod)
|
||||
(define result 1)
|
||||
(while ( !zero? exp)
|
||||
(when (odd? exp) (set! result (% (* result base) mod)))
|
||||
(/= exp 2)
|
||||
(set! base (% (* base base) mod)))
|
||||
result)
|
||||
|
||||
(xpowmod a b m)
|
||||
→ 1527229998585248450016808958343740453059
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(let ((a "2988348162058574136915891421498819466320163312926952423791023078876139")
|
||||
(b "2351399303373464486466122544523690094744975233415544072992656881240319"))
|
||||
;; "$ ^ $$ mod (10 ^ 40)" performs modular exponentiation.
|
||||
;; "unpack(-5, x)_1" unpacks the integer from the modulo form.
|
||||
(message "%s" (calc-eval "unpack(-5, $ ^ $$ mod (10 ^ 40))_1" nil a b)))
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
%%% For details of the algorithms used see
|
||||
%%% https://en.wikipedia.org/wiki/Modular_exponentiation
|
||||
|
||||
-module modexp_rosetta.
|
||||
-export [mod_exp/3,binary_exp/2,test/0].
|
||||
|
||||
mod_exp(Base,Exp,Mod) when
|
||||
is_integer(Base),
|
||||
is_integer(Exp),
|
||||
is_integer(Mod),
|
||||
Base > 0,
|
||||
Exp > 0,
|
||||
Mod > 0 ->
|
||||
binary_exp_mod(Base,Exp,Mod).
|
||||
|
||||
binary_exp(Base,Exponent) ->
|
||||
binary_exp(Base,Exponent,1).
|
||||
binary_exp(_,0,Result) ->
|
||||
Result;
|
||||
binary_exp(Base,Exponent,Acc) ->
|
||||
binary_exp(Base*Base,Exponent bsr 1,Acc * exp_factor(Base,Exponent)).
|
||||
|
||||
|
||||
binary_exp_mod(Base,Exponent,Mod) ->
|
||||
binary_exp_mod(Base rem Mod,Exponent,Mod,1).
|
||||
binary_exp_mod(_,0,_,Result) ->
|
||||
Result;
|
||||
binary_exp_mod(Base,Exponent,Mod,Acc) ->
|
||||
binary_exp_mod((Base*Base) rem Mod,
|
||||
Exponent bsr 1,Mod,(Acc * exp_factor(Base,Exponent))rem Mod).
|
||||
|
||||
exp_factor(_,0) ->
|
||||
1;
|
||||
exp_factor(Base,1) ->
|
||||
Base;
|
||||
exp_factor(Base,Exponent) ->
|
||||
exp_factor(Base,Exponent band 1).
|
||||
|
||||
test() ->
|
||||
445 = mod_exp(4,13,497),
|
||||
%% Rosetta code example:
|
||||
mod_exp(2988348162058574136915891421498819466320163312926952423791023078876139,
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319,
|
||||
binary_exp(10,40)).
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
let expMod a b n =
|
||||
let rec loop a b c =
|
||||
if b = 0I then c else
|
||||
loop (a*a%n) (b>>>1) (if b&&&1I = 0I then c else c*a%n)
|
||||
loop a b 1I
|
||||
|
||||
[<EntryPoint>]
|
||||
let main argv =
|
||||
let a = 2988348162058574136915891421498819466320163312926952423791023078876139I
|
||||
let b = 2351399303373464486466122544523690094744975233415544072992656881240319I
|
||||
printfn "%A" (expMod a b (10I**40)) // -> 1527229998585248450016808958343740453059
|
||||
0
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
! Built-in
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
10 40 ^
|
||||
^mod .
|
||||
1005
Task/Modular-exponentiation/Fortran/modular-exponentiation.f
Normal file
1005
Task/Modular-exponentiation/Fortran/modular-exponentiation.f
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,258 @@
|
|||
'From first principles (No external library)
|
||||
Function _divide(n1 As String,n2 As String,decimal_places As Integer=10,dpflag As String="s") As String
|
||||
Dim As String number=n1,divisor=n2
|
||||
dpflag=Lcase(dpflag)
|
||||
'For MOD
|
||||
Dim As Integer modstop
|
||||
If dpflag="mod" Then
|
||||
If Len(n1)<Len(n2) Then Return n1
|
||||
If Len(n1)=Len(n2) Then
|
||||
If n1<n2 Then Return n1
|
||||
End If
|
||||
modstop=Len(n1)-Len(n2)+1
|
||||
End If
|
||||
If dpflag<>"mod" Then
|
||||
If dpflag<>"s" Then dpflag="raw"
|
||||
End If
|
||||
Dim runcount As Integer
|
||||
'_______ LOOK UP TABLES ______________
|
||||
Dim Qmod(0 To 19) As Ubyte
|
||||
Dim bool(0 To 19) As Ubyte
|
||||
For z As Integer=0 To 19
|
||||
Qmod(z)=(z Mod 10+48)
|
||||
bool(z)=(-(10>z))
|
||||
Next z
|
||||
Dim answer As String 'THE ANSWER STRING
|
||||
|
||||
'_______ SET THE DECIMAL WHERE IT SHOULD BE AT _______
|
||||
Dim As String part1,part2
|
||||
#macro set(decimal)
|
||||
#macro insert(s,char,position)
|
||||
If position > 0 And position <=Len(s) Then
|
||||
part1=Mid(s,1,position-1)
|
||||
part2=Mid(s,position)
|
||||
s=part1+char+part2
|
||||
End If
|
||||
#endmacro
|
||||
insert(answer,".",decpos)
|
||||
answer=thepoint+zeros+answer
|
||||
If dpflag="raw" Then
|
||||
answer=Mid(answer,1,decimal_places)
|
||||
End If
|
||||
#endmacro
|
||||
'______________________________________________
|
||||
'__________ SPLIT A STRING ABOUT A CHARACTRR __________
|
||||
Dim As String var1,var2
|
||||
Dim pst As Integer
|
||||
#macro split(stri,char,var1,var2)
|
||||
pst=Instr(stri,char)
|
||||
var1="":var2=""
|
||||
If pst<>0 Then
|
||||
var1=Rtrim(Mid(stri,1,pst),".")
|
||||
var2=Ltrim(Mid(stri,pst),".")
|
||||
Else
|
||||
var1=stri
|
||||
End If
|
||||
#endmacro
|
||||
|
||||
#macro Removepoint(s)
|
||||
split(s,".",var1,var2)
|
||||
#endmacro
|
||||
'__________ GET THE SIGN AND CLEAR THE -ve __________________
|
||||
Dim sign As String
|
||||
If Left(number,1)="-" Xor Left (divisor,1)="-" Then sign="-"
|
||||
If Left(number,1)="-" Then number=Ltrim(number,"-")
|
||||
If Left (divisor,1)="-" Then divisor=Ltrim(divisor,"-")
|
||||
|
||||
'DETERMINE THE DECIMAL POSITION BEFORE THE DIVISION
|
||||
Dim As Integer lennint,lenddec,lend,lenn,difflen
|
||||
split(number,".",var1,var2)
|
||||
lennint=Len(var1)
|
||||
split(divisor,".",var1,var2)
|
||||
lenddec=Len(var2)
|
||||
|
||||
If Instr(number,".") Then
|
||||
Removepoint(number)
|
||||
number=var1+var2
|
||||
End If
|
||||
If Instr(divisor,".") Then
|
||||
Removepoint(divisor)
|
||||
divisor=var1+var2
|
||||
End If
|
||||
Dim As Integer numzeros
|
||||
numzeros=Len(number)
|
||||
number=Ltrim(number,"0"):divisor=Ltrim (divisor,"0")
|
||||
numzeros=numzeros-Len(number)
|
||||
lend=Len(divisor):lenn=Len(number)
|
||||
If lend>lenn Then difflen=lend-lenn
|
||||
Dim decpos As Integer=lenddec+lennint-lend+2-numzeros 'THE POSITION INDICATOR
|
||||
Dim _sgn As Byte=-Sgn(decpos)
|
||||
If _sgn=0 Then _sgn=1
|
||||
Dim As String thepoint=String(_sgn,".") 'DECIMAL AT START (IF)
|
||||
Dim As String zeros=String(-decpos+1,"0")'ZEROS AT START (IF) e.g. .0009
|
||||
If dpflag<>"mod" Then
|
||||
If Len(zeros) =0 Then dpflag="s"
|
||||
End If
|
||||
Dim As Integer runlength
|
||||
If Len(zeros) Then
|
||||
runlength=decimal_places
|
||||
answer=String(Len(zeros)+runlength+10,"0")
|
||||
If dpflag="raw" Then
|
||||
runlength=1
|
||||
answer=String(Len(zeros)+runlength+10,"0")
|
||||
If decimal_places>Len(zeros) Then
|
||||
runlength=runlength+(decimal_places-Len(zeros))
|
||||
answer=String(Len(zeros)+runlength+10,"0")
|
||||
End If
|
||||
End If
|
||||
|
||||
Else
|
||||
decimal_places=decimal_places+decpos
|
||||
runlength=decimal_places
|
||||
answer=String(Len(zeros)+runlength+10,"0")
|
||||
End If
|
||||
'___________DECIMAL POSITION DETERMINED _____________
|
||||
|
||||
'SET UP THE VARIABLES AND START UP CONDITIONS
|
||||
number=number+String(difflen+decimal_places,"0")
|
||||
Dim count As Integer
|
||||
Dim temp As String
|
||||
Dim copytemp As String
|
||||
Dim topstring As String
|
||||
Dim copytopstring As String
|
||||
Dim As Integer lenf,lens
|
||||
Dim As Ubyte takeaway,subtractcarry
|
||||
Dim As Integer n3,diff
|
||||
If Ltrim(divisor,"0")="" Then Return "Error :division by zero"
|
||||
lens=Len(divisor)
|
||||
topstring=Left(number,lend)
|
||||
copytopstring=topstring
|
||||
Do
|
||||
count=0
|
||||
Do
|
||||
count=count+1
|
||||
copytemp=temp
|
||||
|
||||
Do
|
||||
'___________________ QUICK SUBTRACTION loop _________________
|
||||
|
||||
lenf=Len(topstring)
|
||||
If lens<lenf=0 Then 'not
|
||||
If Lens>lenf Then
|
||||
temp= "done"
|
||||
Exit Do
|
||||
End If
|
||||
If divisor>topstring Then
|
||||
temp= "done"
|
||||
Exit Do
|
||||
End If
|
||||
End If
|
||||
|
||||
diff=lenf-lens
|
||||
temp=topstring
|
||||
subtractcarry=0
|
||||
|
||||
For n3=lenf-1 To diff Step -1
|
||||
takeaway= topstring[n3]-divisor[n3-diff]+10-subtractcarry
|
||||
temp[n3]=Qmod(takeaway)
|
||||
subtractcarry=bool(takeaway)
|
||||
Next n3
|
||||
If subtractcarry=0 Then Exit Do
|
||||
If n3=-1 Then Exit Do
|
||||
For n3=n3 To 0 Step -1
|
||||
takeaway= topstring[n3]-38-subtractcarry
|
||||
temp[n3]=Qmod(takeaway)
|
||||
subtractcarry=bool(takeaway)
|
||||
If subtractcarry=0 Then Exit Do
|
||||
Next n3
|
||||
Exit Do
|
||||
|
||||
Loop 'single run
|
||||
temp=Ltrim(temp,"0")
|
||||
If temp="" Then temp= "0"
|
||||
topstring=temp
|
||||
Loop Until temp="done"
|
||||
' INDIVIDUAL CHARACTERS CARVED OFF ________________
|
||||
runcount=runcount+1
|
||||
If count=1 Then
|
||||
topstring=copytopstring+Mid(number,lend+runcount,1)
|
||||
Else
|
||||
topstring=copytemp+Mid(number,lend+runcount,1)
|
||||
End If
|
||||
copytopstring=topstring
|
||||
topstring=Ltrim(topstring,"0")
|
||||
If dpflag="mod" Then
|
||||
If runcount=modstop Then
|
||||
If topstring="" Then Return "0"
|
||||
Return Mid(topstring,1,Len(topstring)-1)
|
||||
End If
|
||||
End If
|
||||
answer[runcount-1]=count+47
|
||||
If topstring="" And runcount>Len(n1)+1 Then
|
||||
Exit Do
|
||||
End If
|
||||
Loop Until runcount=runlength+1
|
||||
|
||||
' END OF RUN TO REQUIRED DECIMAL PLACES
|
||||
set(decimal) 'PUT IN THE DECIMAL POINT
|
||||
'THERE IS ALWAYS A DECIMAL POINT SOMEWHERE IN THE ANSWER
|
||||
'NOW GET RID OF IT IF IT IS REDUNDANT
|
||||
answer=Rtrim(answer,"0")
|
||||
answer=Rtrim(answer,".")
|
||||
answer=Ltrim(answer,"0")
|
||||
If answer="" Then Return "0"
|
||||
Return sign+answer
|
||||
End Function
|
||||
|
||||
Dim Shared As Integer _Mod(0 To 99),_Div(0 To 99)
|
||||
For z As Integer=0 To 99:_Mod(z)=(z Mod 10+48):_Div(z)=z\10:Next
|
||||
|
||||
Function Qmult(a As String,b As String) As String
|
||||
Var flag=0,la = Len(a),lb = Len(b)
|
||||
If Len(b)>Len(a) Then flag=1:Swap a,b:Swap la,lb
|
||||
Dim As Ubyte n,carry,ai
|
||||
Var c =String(la+lb,"0")
|
||||
For i As Integer =la-1 To 0 Step -1
|
||||
carry=0:ai=a[i]-48
|
||||
For j As Integer =lb-1 To 0 Step -1
|
||||
n = ai * (b[j]-48) + (c[i+j+1]-48) + carry
|
||||
carry =_Div(n):c[i+j+1]=_Mod(n)
|
||||
Next j
|
||||
c[i]+=carry
|
||||
Next i
|
||||
If flag Then Swap a,b
|
||||
Return Ltrim(c,"0")
|
||||
End Function
|
||||
'=======================================================================
|
||||
#define mod_(a,b) _divide((a),(b),,"mod")
|
||||
#define div_(a,b) iif(len((a))<len((b)),"0",_divide((a),(b),-2))
|
||||
|
||||
Function Modular_Exponentiation(base_num As String, exponent As String, modulus As String) As String
|
||||
Dim b1 As String = base_num
|
||||
Dim e1 As String = exponent
|
||||
Dim As String result="1"
|
||||
b1 = mod_(b1,modulus)
|
||||
Do While e1 <> "0"
|
||||
Var L=Len(e1)-1
|
||||
If e1[L] And 1 Then
|
||||
result=mod_(Qmult(result,b1),modulus)
|
||||
End If
|
||||
b1=mod_(qmult(b1,b1),modulus)
|
||||
e1=div_(e1,"2")
|
||||
Loop
|
||||
Return result
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
|
||||
var base_num="2988348162058574136915891421498819466320163312926952423791023078876139"
|
||||
var exponent="2351399303373464486466122544523690094744975233415544072992656881240319"
|
||||
var modulus="10000000000000000000000000000000000000000"
|
||||
dim as double t=timer
|
||||
var ans=Modular_Exponentiation(base_num,exponent,modulus)
|
||||
print "Result:"
|
||||
Print ans
|
||||
print "time taken ";(timer-t)*1000;" milliseconds"
|
||||
Print "Done"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
a = 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
b = 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
println[modPow[a,b,10^40]]
|
||||
21
Task/Modular-exponentiation/GAP/modular-exponentiation.gap
Normal file
21
Task/Modular-exponentiation/GAP/modular-exponentiation.gap
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Built-in
|
||||
a := 2988348162058574136915891421498819466320163312926952423791023078876139;
|
||||
b := 2351399303373464486466122544523690094744975233415544072992656881240319;
|
||||
PowerModInt(a, b, 10^40);
|
||||
1527229998585248450016808958343740453059
|
||||
|
||||
# Implementation
|
||||
PowerModAlt := function(a, n, m)
|
||||
local r;
|
||||
r := 1;
|
||||
while n > 0 do
|
||||
if IsOddInt(n) then
|
||||
r := RemInt(r*a, m);
|
||||
fi;
|
||||
n := QuoInt(n, 2);
|
||||
a := RemInt(a*a, m);
|
||||
od;
|
||||
return r;
|
||||
end;
|
||||
|
||||
PowerModAlt(a, b, 10^40);
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
_powm(b, e, m, r) = (e == 0 ? r : (e % 2 == 1 ? _powm(b * b % m, e / 2, m, r * b % m) : _powm(b * b % m, e / 2, m, r)))
|
||||
powm(b, e, m) = _powm(b, e, m, 1)
|
||||
# Usage
|
||||
print powm(2, 3453, 131)
|
||||
# Where b is the base, e is the exponent, m is the modulus, i.e.: b^e mod m
|
||||
19
Task/Modular-exponentiation/Go/modular-exponentiation.go
Normal file
19
Task/Modular-exponentiation/Go/modular-exponentiation.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a, _ := new(big.Int).SetString(
|
||||
"2988348162058574136915891421498819466320163312926952423791023078876139", 10)
|
||||
b, _ := new(big.Int).SetString(
|
||||
"2351399303373464486466122544523690094744975233415544072992656881240319", 10)
|
||||
m := big.NewInt(10)
|
||||
r := big.NewInt(40)
|
||||
m.Exp(m, r, nil)
|
||||
|
||||
r.Exp(a, b, m)
|
||||
fmt.Println(r)
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
println 2988348162058574136915891421498819466320163312926952423791023078876139.modPow(
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319,
|
||||
10000000000000000000000000000000000000000)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
modPow :: Integer -> Integer -> Integer -> Integer -> Integer
|
||||
modPow b e 1 r = 0
|
||||
modPow b 0 m r = r
|
||||
modPow b e m r
|
||||
| e `mod` 2 == 1 = modPow b' e' m (r * b `mod` m)
|
||||
| otherwise = modPow b' e' m r
|
||||
where
|
||||
b' = b * b `mod` m
|
||||
e' = e `div` 2
|
||||
|
||||
main = do
|
||||
print (modPow 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
(10 ^ 40)
|
||||
1)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
powerMod :: Integer -> Integer -> Integer -> Integer
|
||||
powerMod b e m = x
|
||||
where
|
||||
(_, _, x) =
|
||||
until
|
||||
(\(_, e, _) -> e <= 0)
|
||||
(\(b, e, x) ->
|
||||
( mod (b * b) m
|
||||
, div e 2
|
||||
, if 0 /= mod e 2
|
||||
then mod (b * x) m
|
||||
else x))
|
||||
(b, e, 1)
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
print $
|
||||
powerMod
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
(10 ^ 40)
|
||||
17
Task/Modular-exponentiation/Icon/modular-exponentiation.icon
Normal file
17
Task/Modular-exponentiation/Icon/modular-exponentiation.icon
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
procedure main()
|
||||
a := 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
b := 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
write("last 40 digits = ",mod_power(a,b,(10^40)))
|
||||
end
|
||||
|
||||
procedure mod_power(base, exponent, modulus) # fast modular exponentation
|
||||
if exponent < 0 then runerr(205,m) # added for this task
|
||||
result := 1
|
||||
while exponent > 0 do {
|
||||
if exponent % 2 = 1 then
|
||||
result := (result * base) % modulus
|
||||
exponent /:= 2
|
||||
base := base ^ 2 % modulus
|
||||
}
|
||||
return result
|
||||
end
|
||||
1
Task/Modular-exponentiation/J/modular-exponentiation-1.j
Normal file
1
Task/Modular-exponentiation/J/modular-exponentiation-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
m&|@^
|
||||
6
Task/Modular-exponentiation/J/modular-exponentiation-2.j
Normal file
6
Task/Modular-exponentiation/J/modular-exponentiation-2.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
a =: 2988348162058574136915891421498819466320163312926952423791023078876139x
|
||||
b =: 2351399303373464486466122544523690094744975233415544072992656881240319x
|
||||
m =: 10^40x
|
||||
|
||||
a m&|@^ b
|
||||
1527229998585248450016808958343740453059
|
||||
13
Task/Modular-exponentiation/Java/modular-exponentiation.java
Normal file
13
Task/Modular-exponentiation/Java/modular-exponentiation.java
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import java.math.BigInteger;
|
||||
|
||||
public class PowMod {
|
||||
public static void main(String[] args){
|
||||
BigInteger a = new BigInteger(
|
||||
"2988348162058574136915891421498819466320163312926952423791023078876139");
|
||||
BigInteger b = new BigInteger(
|
||||
"2351399303373464486466122544523690094744975233415544072992656881240319");
|
||||
BigInteger m = new BigInteger("10000000000000000000000000000000000000000");
|
||||
|
||||
System.out.println(a.modPow(b, m));
|
||||
}
|
||||
}
|
||||
29
Task/Modular-exponentiation/Jq/modular-exponentiation.jq
Normal file
29
Task/Modular-exponentiation/Jq/modular-exponentiation.jq
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# To take advantage of gojq's arbitrary-precision integer arithmetic:
|
||||
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
|
||||
|
||||
# Returns (. ^ $exp) % $mod
|
||||
# where $exp >= 0, $mod != 0, and the input are integers.
|
||||
def modPow($exp; $mod):
|
||||
if $mod == 0 then "Cannot take modPow with modulus 0." | error
|
||||
elif $exp < 0 then "modPow with exp < 0 is not supported." | error
|
||||
else . as $x
|
||||
| {r: 1, base: ($x % $mod), exp: $exp}
|
||||
| until( .exp <= 0 or .emit;
|
||||
if .base == 0 then .emit = 0
|
||||
else if .exp%2 == 1
|
||||
then .r = (.r * .base) % $mod
|
||||
| .exp |= (. - 1) / 2
|
||||
else .exp /= 2
|
||||
end
|
||||
| .base |= (. * .) % $mod
|
||||
end )
|
||||
| if .emit then .emit else .r end
|
||||
end;
|
||||
|
||||
def task:
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139 as $a
|
||||
| 2351399303373464486466122544523690094744975233415544072992656881240319 as $b
|
||||
| (10|power(40)) as $m
|
||||
| $a | modPow($b; $m) ;
|
||||
|
||||
task
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
a = 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
b = 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
m = big(10) ^ 40
|
||||
@show powermod(a, b, m)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
// version 1.0.6
|
||||
|
||||
import java.math.BigInteger
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val a = BigInteger("2988348162058574136915891421498819466320163312926952423791023078876139")
|
||||
val b = BigInteger("2351399303373464486466122544523690094744975233415544072992656881240319")
|
||||
val m = BigInteger.TEN.pow(40)
|
||||
println(a.modPow(b, m))
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
We will call the lib_BN library for big numbers:
|
||||
|
||||
{require lib_BN}
|
||||
|
||||
In this library {BN.compare a b} returns 1 if a > b, 0 if a = b and -1 if a < b.
|
||||
For a better readability we define three small functions
|
||||
|
||||
{def BN.= {lambda {:a :b} {= {BN.compare :a :b} 0}}}
|
||||
-> BN.=
|
||||
{def BN.even? {lambda {:n} {= {BN.compare {BN.% :n 2} 0} 0}}}
|
||||
-> BN.even?
|
||||
{def BN.square {lambda {:n} {BN.* :n :n}}}
|
||||
-> BN.square
|
||||
|
||||
{def mod-exp
|
||||
{lambda {:a :n :mod}
|
||||
{if {BN.= :n 0}
|
||||
then 1
|
||||
else {if {BN.even? :n}
|
||||
then {BN.% {BN.square {mod-exp :a {BN./ :n 2} :mod}} :mod}
|
||||
else {BN.% {BN.* :a {mod-exp :a {BN.- :n 1} :mod}} :mod}}}}}
|
||||
-> mod-exp
|
||||
|
||||
{mod-exp
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
{BN.pow 10 40}}
|
||||
-> 1527229998585248450016808958343740453059 // 3300ms
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
a := 2988348162058574136915891421498819466320163312926952423791023078876139:
|
||||
b := 2351399303373464486466122544523690094744975233415544072992656881240319:
|
||||
a &^ b mod 10^40;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
a = 2988348162058574136915891421498819466320163312926952423791023078876139;
|
||||
b = 2351399303373464486466122544523690094744975233415544072992656881240319;
|
||||
m = 10^40;
|
||||
PowerMod[a, b, m]
|
||||
-> 1527229998585248450016808958343740453059
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
a: 2988348162058574136915891421498819466320163312926952423791023078876139$
|
||||
b: 2351399303373464486466122544523690094744975233415544072992656881240319$
|
||||
power_mod(a, b, 10^40);
|
||||
/* 1527229998585248450016808958343740453059 */
|
||||
18
Task/Modular-exponentiation/Nim/modular-exponentiation.nim
Normal file
18
Task/Modular-exponentiation/Nim/modular-exponentiation.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import bigints
|
||||
|
||||
proc powmod(b, e, m: BigInt): BigInt =
|
||||
assert e >= 0
|
||||
var e = e
|
||||
var b = b
|
||||
result = initBigInt(1)
|
||||
while e > 0:
|
||||
if e mod 2 == 1:
|
||||
result = (result * b) mod m
|
||||
e = e div 2
|
||||
b = (b.pow 2) mod m
|
||||
|
||||
var
|
||||
a = initBigInt("2988348162058574136915891421498819466320163312926952423791023078876139")
|
||||
b = initBigInt("2351399303373464486466122544523690094744975233415544072992656881240319")
|
||||
|
||||
echo powmod(a, b, 10.pow 40)
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
# -*- ObjectIcon -*-
|
||||
#
|
||||
# This program is close to being an exact copy of the Icon.
|
||||
#
|
||||
|
||||
import io # <-- Object Icon requires this for I/O.
|
||||
|
||||
procedure main()
|
||||
local a, b # <-- Object Icon forces you to declare your variables.
|
||||
|
||||
a := 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
b := 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
|
||||
# You could leave out the "io." in the call to "write" below,
|
||||
# because there is some "compatibility with regular Icon" support in
|
||||
# the io package.
|
||||
io.write("last 40 digits = ", mod_power(a,b,(10^40)))
|
||||
end
|
||||
|
||||
procedure mod_power(base, exponent, modulus)
|
||||
local result
|
||||
|
||||
result := 1
|
||||
while exponent > 0 do
|
||||
{
|
||||
if exponent % 2 = 1 then
|
||||
result := (result * base) % modulus
|
||||
exponent /:= 2
|
||||
base := base ^ 2 % modulus
|
||||
}
|
||||
return result
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
: powmod(base, exponent, modulus)
|
||||
1 exponent dup ifZero: [ return ]
|
||||
while ( dup 0 > ) [
|
||||
dup isEven ifFalse: [ swap base * modulus mod swap ]
|
||||
2 / base sq modulus mod ->base
|
||||
] drop ;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
a=2988348162058574136915891421498819466320163312926952423791023078876139;
|
||||
b=2351399303373464486466122544523690094744975233415544072992656881240319;
|
||||
lift(Mod(a,10^40)^b)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
$a = '2988348162058574136915891421498819466320163312926952423791023078876139';
|
||||
$b = '2351399303373464486466122544523690094744975233415544072992656881240319';
|
||||
$m = '1' . str_repeat('0', 40);
|
||||
echo bcpowmod($a, $b, $m), "\n";
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
Program ModularExponentiation(output);
|
||||
|
||||
uses
|
||||
gmp;
|
||||
|
||||
var
|
||||
a, b, m, r: mpz_t;
|
||||
fmt: pchar;
|
||||
|
||||
begin
|
||||
mpz_init_set_str(a, '2988348162058574136915891421498819466320163312926952423791023078876139', 10);
|
||||
mpz_init_set_str(b, '2351399303373464486466122544523690094744975233415544072992656881240319', 10);
|
||||
mpz_init(m);
|
||||
mpz_ui_pow_ui(m, 10, 40);
|
||||
|
||||
mpz_init(r);
|
||||
mpz_powm(r, a, b, m);
|
||||
|
||||
fmt := '%Zd' + chr(13) + chr(10);
|
||||
mp_printf(fmt, @r); (* ...16808958343740453059 *)
|
||||
|
||||
mpz_clear(a);
|
||||
mpz_clear(b);
|
||||
mpz_clear(m);
|
||||
mpz_clear(r);
|
||||
end.
|
||||
18
Task/Modular-exponentiation/Perl/modular-exponentiation.pl
Normal file
18
Task/Modular-exponentiation/Perl/modular-exponentiation.pl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
use bigint;
|
||||
|
||||
sub expmod {
|
||||
my($a, $b, $n) = @_;
|
||||
my $c = 1;
|
||||
do {
|
||||
($c *= $a) %= $n if $b % 2;
|
||||
($a *= $a) %= $n;
|
||||
} while ($b = int $b/2);
|
||||
$c;
|
||||
}
|
||||
|
||||
my $a = 2988348162058574136915891421498819466320163312926952423791023078876139;
|
||||
my $b = 2351399303373464486466122544523690094744975233415544072992656881240319;
|
||||
my $m = 10 ** 40;
|
||||
|
||||
print expmod($a, $b, $m), "\n";
|
||||
print $a->bmodpow($b, $m), "\n";
|
||||
35
Task/Modular-exponentiation/Phix/modular-exponentiation.phix
Normal file
35
Task/Modular-exponentiation/Phix/modular-exponentiation.phix
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">mpz_mod_exp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">mpz</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">exponent</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">modulus</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">result</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">exponent</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #004080;">mpz</span> <span style="color: #000000;">_exp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">exponent</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (use a copy)</span>
|
||||
<span style="color: #004080;">bool</span> <span style="color: #000000;">odd</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">_exp</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">odd</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">mpz_sub_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">_exp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">_exp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #7060A8;">mpz_fdiv_q_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">_exp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">_exp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mpz_mod_exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #000000;">_exp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">modulus</span><span style="color: #0000FF;">,</span><span style="color: #000000;">result</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">_exp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">_exp</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #000000;">result</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">odd</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #7060A8;">mpz_mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #000000;">modulus</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #004080;">mpz</span> <span style="color: #000000;">base</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"2988348162058574136915891421498819466320163312926952423791023078876139"</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">exponent</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"2351399303373464486466122544523690094744975233415544072992656881240319"</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">modulus</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1"</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">40</span><span style="color: #0000FF;">)),</span>
|
||||
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
|
||||
|
||||
<span style="color: #000000;">mpz_mod_exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #000000;">exponent</span><span style="color: #0000FF;">,</span><span style="color: #000000;">modulus</span><span style="color: #0000FF;">,</span><span style="color: #000000;">result</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- check against the builtin:</span>
|
||||
<span style="color: #7060A8;">mpz_powm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #000000;">exponent</span><span style="color: #0000FF;">,</span><span style="color: #000000;">modulus</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(de **Mod (X Y N)
|
||||
(let M 1
|
||||
(loop
|
||||
(when (bit? 1 Y)
|
||||
(setq M (% (* M X) N)) )
|
||||
(T (=0 (setq Y (>> 1 Y)))
|
||||
M )
|
||||
(setq X (% (* X X) N)) ) ) )
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
: (**Mod
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
10000000000000000000000000000000000000000 )
|
||||
-> 1527229998585248450016808958343740453059
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
Function Invoke-ModuloExponentiation ([BigInt]$Base, [BigInt]$Exponent, $Modulo) {
|
||||
$Result = 1
|
||||
$Base = $Base % $Modulo
|
||||
If ($Base -eq 0) {return 0}
|
||||
|
||||
While ($Exponent -gt 0) {
|
||||
If (($Exponent -band 1) -eq 1) {$Result = ($Result * $Base) % $Modulo}
|
||||
$Exponent = $Exponent -shr 1
|
||||
$Base = ($Base * $Base) % $Modulo
|
||||
}
|
||||
return ($Result % $Modulo)
|
||||
}
|
||||
|
||||
$a = [BigInt]::Parse('2988348162058574136915891421498819466320163312926952423791023078876139')
|
||||
$b = [BigInt]::Parse('2351399303373464486466122544523690094744975233415544072992656881240319')
|
||||
$m = [BigInt]::Pow(10, 40)
|
||||
|
||||
Invoke-ModuloExponentiation -Base $a -Exponent $b -Modulo $m
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
main:-
|
||||
A = 2988348162058574136915891421498819466320163312926952423791023078876139,
|
||||
B = 2351399303373464486466122544523690094744975233415544072992656881240319,
|
||||
M is 10 ** 40,
|
||||
P is powm(A, B, M),
|
||||
writeln(P).
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
a = 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
b = 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
m = 10 ** 40
|
||||
print(pow(a, b, m))
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
def power_mod(b, e, m):
|
||||
" Without using builtin function "
|
||||
x = 1
|
||||
while e > 0:
|
||||
b, e, x = (
|
||||
b * b % m,
|
||||
e // 2,
|
||||
b * x % m if e % 2 else x
|
||||
)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
a = 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
b = 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
m = 10 ** 40
|
||||
print(power_mod(a, b, m))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
[ temp put 1 unrot
|
||||
[ dup while
|
||||
dup 1 & if
|
||||
[ unrot tuck *
|
||||
temp share mod
|
||||
swap rot ]
|
||||
1 >>
|
||||
swap dup *
|
||||
temp share mod
|
||||
swap again ]
|
||||
2drop temp release ] is **mod ( n n n --> n )
|
||||
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
10 40 ** **mod echo
|
||||
25
Task/Modular-exponentiation/REXX/modular-exponentiation.rexx
Normal file
25
Task/Modular-exponentiation/REXX/modular-exponentiation.rexx
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* REXX Modular exponentiation */
|
||||
|
||||
say powerMod(,
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139,,
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319,,
|
||||
1e40)
|
||||
return
|
||||
|
||||
powerMod: procedure
|
||||
parse arg base, exponent, modulus
|
||||
|
||||
/* we need a numeric precision of twice the modulus size, */
|
||||
/* the exponent size, or the base size, whichever is largest */
|
||||
numeric digits max(2 * length(format(modulus, , , 0)),,
|
||||
length(format(exponent, , , 0)), length(format(base, , , 0)))
|
||||
|
||||
result = 1
|
||||
base = base // modulus
|
||||
do while exponent > 0
|
||||
if exponent // 2 = 1 then
|
||||
result = result * base // modulus
|
||||
base = base * base // modulus
|
||||
exponent = exponent % 2
|
||||
end
|
||||
return result
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#lang racket
|
||||
(require math)
|
||||
(define a 2988348162058574136915891421498819466320163312926952423791023078876139)
|
||||
(define b 2351399303373464486466122544523690094744975233415544072992656881240319)
|
||||
(define m (expt 10 40))
|
||||
(modular-expt a b m)
|
||||
13
Task/Modular-exponentiation/Raku/modular-exponentiation.raku
Normal file
13
Task/Modular-exponentiation/Raku/modular-exponentiation.raku
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
sub expmod(Int $a is copy, Int $b is copy, $n) {
|
||||
my $c = 1;
|
||||
repeat while $b div= 2 {
|
||||
($c *= $a) %= $n if $b % 2;
|
||||
($a *= $a) %= $n;
|
||||
}
|
||||
$c;
|
||||
}
|
||||
|
||||
say expmod
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139,
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319,
|
||||
10**40;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
a = 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
b = 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
m = 10**40
|
||||
puts a.pow(b, m)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
require 'openssl'
|
||||
a = 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
b = 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
m = 10 ** 40
|
||||
puts a.to_bn.mod_exp(b, m)
|
||||
11
Task/Modular-exponentiation/Ruby/modular-exponentiation-3.rb
Normal file
11
Task/Modular-exponentiation/Ruby/modular-exponentiation-3.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def mod_exp(n, e, mod)
|
||||
fail ArgumentError, 'negative exponent' if e < 0
|
||||
prod = 1
|
||||
base = n % mod
|
||||
until e.zero?
|
||||
prod = (prod * base) % mod if e.odd?
|
||||
e >>= 1
|
||||
base = (base * base) % mod
|
||||
end
|
||||
prod
|
||||
end
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import scala.math.BigInt
|
||||
|
||||
val a = BigInt(
|
||||
"2988348162058574136915891421498819466320163312926952423791023078876139")
|
||||
val b = BigInt(
|
||||
"2351399303373464486466122544523690094744975233415544072992656881240319")
|
||||
|
||||
println(a.modPow(b, BigInt(10).pow(40)))
|
||||
15
Task/Modular-exponentiation/Scheme/modular-exponentiation.ss
Normal file
15
Task/Modular-exponentiation/Scheme/modular-exponentiation.ss
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(define (square n)
|
||||
(* n n))
|
||||
|
||||
(define (mod-exp a n mod)
|
||||
(cond ((= n 0) 1)
|
||||
((even? n)
|
||||
(remainder (square (mod-exp a (/ n 2) mod))
|
||||
mod))
|
||||
(else (remainder (* a (mod-exp a (- n 1) mod))
|
||||
mod))))
|
||||
|
||||
(define result
|
||||
(mod-exp 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
(expt 10 40)))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
$ include "seed7_05.s7i";
|
||||
include "bigint.s7i";
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
writeln(modPow(2988348162058574136915891421498819466320163312926952423791023078876139_,
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319_,
|
||||
10_ ** 40));
|
||||
end func;
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
const func bigInteger: modPow (in var bigInteger: base,
|
||||
in var bigInteger: exponent, in bigInteger: modulus) is func
|
||||
result
|
||||
var bigInteger: power is 1_;
|
||||
begin
|
||||
if exponent < 0_ or modulus < 0_ then
|
||||
raise RANGE_ERROR;
|
||||
else
|
||||
while exponent > 0_ do
|
||||
if odd(exponent) then
|
||||
power := (power * base) mod modulus;
|
||||
end if;
|
||||
exponent >>:= 1;
|
||||
base := base ** 2 mod modulus;
|
||||
end while;
|
||||
end if;
|
||||
end func;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
say expmod(
|
||||
2988348162058574136915891421498819466320163312926952423791023078876139,
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319,
|
||||
10**40)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
func expmod(a, b, n) {
|
||||
var c = 1
|
||||
do {
|
||||
(c *= a) %= n if b.is_odd
|
||||
(a *= a) %= n
|
||||
} while (b //= 2)
|
||||
c
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
import BigInt
|
||||
|
||||
func modPow<T: BinaryInteger>(n: T, e: T, m: T) -> T {
|
||||
guard e != 0 else {
|
||||
return 1
|
||||
}
|
||||
|
||||
var res = T(1)
|
||||
var base = n % m
|
||||
var exp = e
|
||||
|
||||
while true {
|
||||
if exp & 1 == 1 {
|
||||
res *= base
|
||||
res %= m
|
||||
}
|
||||
|
||||
if exp == 1 {
|
||||
return res
|
||||
}
|
||||
|
||||
exp /= 2
|
||||
base *= base
|
||||
base %= m
|
||||
}
|
||||
}
|
||||
|
||||
let a = BigInt("2988348162058574136915891421498819466320163312926952423791023078876139")
|
||||
let b = BigInt("2351399303373464486466122544523690094744975233415544072992656881240319")
|
||||
|
||||
print(modPow(n: a, e: b, m: BigInt(10).power(40)))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
$ txr -p '(exptmod 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
(expt 10 40)))'
|
||||
1527229998585248450016808958343740453059
|
||||
13
Task/Modular-exponentiation/Tcl/modular-exponentiation-1.tcl
Normal file
13
Task/Modular-exponentiation/Tcl/modular-exponentiation-1.tcl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package require Tcl 8.5
|
||||
|
||||
# Algorithm from http://introcs.cs.princeton.edu/java/78crypto/ModExp.java.html
|
||||
# but Tcl has arbitrary-width integers and an exponentiation operator, which
|
||||
# helps simplify the code.
|
||||
proc tcl::mathfunc::modexp {a b n} {
|
||||
if {$b == 0} {return 1}
|
||||
set c [expr {modexp($a, $b / 2, $n)**2 % $n}]
|
||||
if {$b & 1} {
|
||||
set c [expr {($c * $a) % $n}]
|
||||
}
|
||||
return $c
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
set a 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
set b 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
set n [expr {10**40}]
|
||||
puts [expr {modexp($a,$b,$n)}]
|
||||
10
Task/Modular-exponentiation/Tcl/modular-exponentiation-3.tcl
Normal file
10
Task/Modular-exponentiation/Tcl/modular-exponentiation-3.tcl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package require Tcl 8.5
|
||||
proc modexp {a b n} {
|
||||
for {set c 1} {$b} {set a [expr {$a*$a % $n}]} {
|
||||
if {$b & 1} {
|
||||
set c [expr {$c*$a % $n}]
|
||||
}
|
||||
set b [expr {$b >> 1}]
|
||||
}
|
||||
return $c
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
set a 2988348162058574136915891421498819466320163312926952423791023078876139
|
||||
set b 2351399303373464486466122544523690094744975233415544072992656881240319
|
||||
set n [expr {10**40}]
|
||||
puts [modexp $a $b $n]
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
' Modular exponentiation - VB.Net - 21/01/2019
|
||||
Imports System.Numerics
|
||||
|
||||
Private Sub Main()
|
||||
Dim a, b, m, x As BigInteger
|
||||
a = BigInteger.Parse("2988348162058574136915891421498819466320163312926952423791023078876139")
|
||||
b = BigInteger.Parse("2351399303373464486466122544523690094744975233415544072992656881240319")
|
||||
m = BigInteger.Pow(10, 40) '=10^40
|
||||
x = ModPowBig(a, b, m)
|
||||
Debug.Print("x=" & x.ToString)
|
||||
End Sub 'Main
|
||||
|
||||
Function ModPowBig(ByVal base As BigInteger, ByVal exponent As BigInteger, ByVal modulus As BigInteger) As BigInteger
|
||||
Dim result As BigInteger
|
||||
result = 1
|
||||
Do While exponent > 0
|
||||
If (exponent Mod 2) = 1 Then
|
||||
result = (result * base) Mod modulus
|
||||
End If
|
||||
exponent = exponent / 2
|
||||
base = (base * base) Mod modulus
|
||||
Loop
|
||||
Return result
|
||||
End Function 'ModPowBig
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import "/big" for BigInt
|
||||
|
||||
var a = BigInt.new("2988348162058574136915891421498819466320163312926952423791023078876139")
|
||||
var b = BigInt.new("2351399303373464486466122544523690094744975233415544072992656881240319")
|
||||
var m = BigInt.ten.pow(40)
|
||||
System.print(a.modPow(b, m))
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
var BN=Import("zklBigNum");
|
||||
a:=BN("2988348162058574136915891421498819466320163312926952423791023078876139");
|
||||
b:=BN("2351399303373464486466122544523690094744975233415544072992656881240319");
|
||||
m:=BN(10).pow(40);
|
||||
a.powm(b,m).println();
|
||||
a.powm(b,m) : "%,d".fmt(_).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue