/**************************************************************************** Kalkulon - A programmable calculator for programmers This is an example Kalkulon script. To load from within Kalkulon: Load("examples/scriptname.k") then you can call functions like: funcname() or funcname(arg0, ...) Author: Juergen Holetzeck 2003 - 2007 e-mail: contact@kalkulon.de homepage: www.kalkulon.de ****************************************************************************/ // some simple routines to calculate primes etc. // check if number is prime primeQ(n)= ( if( n==1; prime=0; if(n==2; prime=1; div=2, sr_n=sqrt(n), do( prime=(fmod(n, div)!=0), if(div==2;++div; div=div+2); prime && div<=sr_n ) ) ), prime ); // build table of primes primeTable(max)= ( t={2,3,5}, next=Back(t), do( if(primeQ(next=next+2); t=PushBack(t, next)); next<=max ), t ); // prime decomposition primeDecomp(n)= ( res={}, if( n==1; res={1}; if(n==2; res={2}; div=2, sr_n=sqrt(n), do( if( fmod(n, div); if(div==2;++div; div=div+2); res=PushBack(res, div), n=n/div, sr_n=sqrt(n) ); div<=sr_n ), if(n>1; res=PushBack(res, n)) ) ), res ); // mirp <-> prim // helper to revert number, e.g. 1234 -> 4321 revNum(n)= ( rev_n=0, do(digit=ip(fp(n=n/10)*10+0.5), n=ip(n), rev_n=10*rev_n+digit; n), rev_n ); // build table of mirps mirpTable(max)= ( t={2,3,5}, next=Back(t), do( if(primeQ(next=next+2)&&primeQ(revNum(next)); t=PushBack(t, next)); next<=max ), t );