Bignum arithmetic

Bjorn Gustavsson bjorn@REDACTED
Mon May 19 15:58:05 CEST 2003


Assuming that you really want 2^129... modulo some other number,
you can do repeated multiplications and taking remainders as you go,
like this:

t() ->
    N = 12920652458263705111179368137496632825464920630924320894917832766446466504617309746646057975239671809,
    Base = 1000000000000000000000000000000,
    pow_mod(N, Base).

pow_mod(0, _) -> 1;
pow_mod(1, _) -> 2;
pow_mod(N, B) ->
    X = pow_mod(N div 2, B),
    P = case N rem 2 of
	    0 -> X*X;
	    1 -> 2*X*X
	end,
    P rem B.

I have not verified the result, but the program does seems to work for
smaller values of N.

/Bjorn

Sean Hinde <sean.hinde@REDACTED> writes:

> Hi all,
> 
> For a bit of light relief I figured it would be fun to make an ssh
> client in Erlang. I figured that with such good bignum support it
> wouldn't be too tricky. So. has anyone any good ideas how to calculate:
> 
> 2^1292065245826370511117936813749663282546492063092432089491783276644646
> 6504617309746646057975239671809
> 
> without a badarith error?
> 
> Tried so far:
> 
> math:pow(2, Big_num).   % Ha Ha!
> 
> 1 bsl Bignum.  % I had hopes for this one..
> 
> Thanks,
> 
> Sean
> 

-- 
Björn Gustavsson            Ericsson Utvecklings AB
bjorn@REDACTED      ÄT2/UAB/F/P
			    BOX 1505
+46 8 727 56 87 	    125 25 Älvsjö



More information about the erlang-questions mailing list