45 lines
922 B
Plaintext
45 lines
922 B
Plaintext
#include <pari/pari.h>
|
|
|
|
#define HAILSTONE1 "n=1;print1(%d,\": \");apply(x->while(x!=1,if(x/2==x\\2,x/=2,x=x*3+1);n++;print1(x,\", \")),%d);print(\"(\",n,\")\n\")"
|
|
#define HAILSTONE2 "m=n=0;for(i=2,%d,h=1;apply(x->while(x!=1,if(x/2==x\\2,x/=2,x=x*3+1);h++),i);if(m<h,m=h;n=i));print(n,\": \",m)"
|
|
|
|
void hailstone1(int x)
|
|
{
|
|
char buf[1024];
|
|
|
|
snprintf(buf, sizeof(buf), HAILSTONE1, x, x);
|
|
|
|
pari_init(1000000, 2);
|
|
|
|
geval(strtoGENstr(buf));
|
|
|
|
pari_close();
|
|
}
|
|
|
|
void hailstone2(int range)
|
|
{
|
|
char buf[1024];
|
|
|
|
snprintf(buf, sizeof(buf), HAILSTONE2, range);
|
|
|
|
pari_init(1000000, 2);
|
|
|
|
geval(strtoGENstr(buf));
|
|
|
|
pari_close();
|
|
}
|
|
|
|
#if __i386__
|
|
const char _hail[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
|
|
#else // __x86_64__
|
|
const char _hail[] __attribute__((section(".interp"))) = "/lib64/ld-linux-x86-64.so.2";
|
|
#endif
|
|
|
|
int main(void)
|
|
{
|
|
hailstone1(27);
|
|
hailstone2(100000);
|
|
|
|
exit(0);
|
|
}
|