What is the standard (or best supported) big number (arbitrary precision) library for Lua?
Using lbc instead of lmapm would be easier because lbc is self-contained.
require"bc"
s=bc.pow(2,1000):tostring()
z=0
for i=1,#s do
z=z+s:byte(i)-("0"):byte(1)
end
print(z)
I used Norman Ramsey's suggestion to solve Project Euler problem #16. I don't think it's a spoiler to say that the crux of the problem is calculating a 303 digit integer accurately.
Here are the steps I needed to install and use the library:
Lua needs to be built with dynamic loading enabled. I use Cygwin, but I changed
PLAT
insrc/Makefile
to belinux
. The default,none
, doesn't enable dynamic loading.The MAMP needs to be built and installed somewhere that your C compiler can find it. I put
libmapm.a
in/usr/local/lib/
. Nextm_apm.h
andm_apm_lc.h
went to/usr/local/include/
.The makefile for lmamp needs to be altered to the correct location of the Lua and MAMP libraries. For me, that means uncommenting the second declaration of
LUA
,LUAINC
,LUALIB
, andLUABIN
and editing the declaration ofMAMP
.Finally,
mapm.so
needs to be placed somewhere that Lua will find it. I put it at/usr/local/lib/lua/5.1/
.
Thank you all for the suggestions!
The lmapm library by Luiz Figueiredo, one of the authors of the Lua language.