25 lines
1 KiB
Text
25 lines
1 KiB
Text
scope # factorials using the arbitrary precision mapm library
|
|
|
|
import mapm; # explicit import needed for: Linux, Mac OS X, Windows and Solaris
|
|
|
|
mapm.xdigits( 100 ); # need to set the precision - 100 is enough for 60!
|
|
|
|
# computes factorial n iteratively, using mapm
|
|
local constant factorial := proc( n :: number ) :: xnumber
|
|
if n < 2 then return mapm.xnumber( 1 )
|
|
else
|
|
local f := mapm.xnumber( 2 );
|
|
for i from 3 to n do f := f * i od;
|
|
return f
|
|
fi
|
|
end;
|
|
# returns a string representation of the integer portion of an xnumber
|
|
local constant asstring := proc( n :: xnumber ) :: string
|
|
local constant str := mapm.xtostring( n );
|
|
local constant point := "." in str;
|
|
return if point <> null then str[ 1 to point - 1 ] else str fi;
|
|
end;
|
|
|
|
for t from 0 to 9 do printf( "%2d! is %d\n", t, mapm.xtonumber( factorial( t ) ) ) od;
|
|
for t from 10 to 60 by 10 do printf( "%2d! is %s\n", t, asstring( factorial( t ) ) ) od;
|
|
end
|