Re: Computing many digits of Pi Message #18 Posted by Egan Ford on 18 Sept 2007, 10:35 p.m., in response to message #1 by Juan Pablo Martinez (Spain)
Using the Spigot Algorithm from Pi Unleashed + HPGCC I can compute 15000 digits of PI in 412 seconds on my 50g. If you are interested I can send you the binary so that you do not need to compile and install the ARM Toolbox. NOTE: Real 50g/49g+ required.
To use:
15000 pidf
Output to stack:
bytes used: 210056
time(s): 412
digits: 15000
digits/s: 36.41
SD card will have file PIDIGITS.TXT with the output formatted with 32 digits/line, recall to stack with:
3:PIDIGITS.TXT RCL
Up Arrow VIEW to see all digits.
NOTE: HPGCC binaries can be large. Best to store on SD card and run with:
3:pidf EVAL
Put that in a small UserRPL script and save to main memory.
You can hold down any key (but ON) to stop the run and collect partial output. When done exit with ON.
/*
pidf.c
Get n from stack and compute n number of pi digits using
Spigot method from Pi Unleashed by Jorg Arndt & Christoph Haenel.
Mem allocated = ((n/4)+1)*14 * 4 bytes
Return to stack statistics, e.g.
(n=5000):
bytes used: 70056
time(s): 46
digits: 5000
digits/s: 108.70
(n=15000):
bytes used: 210056
time(s): 412
digits: 15000
digits/s: 36.41
Only HP specific comments, read book for algorithm details.
*/
#include <hpgcc49.h> //the standard HP lib
int main(void){
int *a;
int b;
int c;
int d = 0;
int e = 0;
int f = 10000;
int g;
int h = 0;
int start;
int end;
int cc = 0;
char buf[40];
int ndigits;
FILE *fh;
char *filename = "pidigits.txt";
sys_slowOff(); //max speed
clear_screen(); //clear the screen
if((fh = fopen(filename,"w"))) {
}
else {
printf("Error opening %s\n",filename);
beep();
sys_slowOn(); //normal speed
WAIT_CANCEL; //loop until ON pressed
return(1);
}
ndigits = sat_pop_zint_llong(); //get ndigits from stack, must be int > 0
if(ndigits > 0) {
}
else {
printf("Number of PI digits must be an\ninteger > 0!\n");
beep();
sys_slowOn(); //normal speed
WAIT_CANCEL; //loop until ON pressed
return(1);
}
c=(ndigits/4+1)*14;
a=malloc(c*sizeof(*a));
start = sys_RTC_seconds(); //get start time in seconds since 1/1/1970
while ((b=c-=14) > 0) { //logic, compute 4 digits of pi at a time then display
while(--b > 0) {
d *= b;
if (h == 0)
d += 2000 * f;
else
d += a[b] * f;
g=b+b-1;
a[b] = d % g;
d /= g;
}
printf("%04d", e + d/f);
fprintf(fh,"%04d", e + d/f);
cc+=4;
if(cc % 32 == 0) {
printf("\n");
fprintf(fh,"\n");
}
if(keyb_isAnyKeyPressed()) //hold any key to stop, do not use ON
break;
d = e = d % f;
h = 4;
}
end = sys_RTC_seconds(); //get end time in seconds since 1/1/1970
fprintf(fh,"\n");
fclose(fh);
printf("\n\n");
sprintf(buf,"bytes used: %d",(ndigits/4+1)*14*(int)sizeof(*a));
printf("%s\n",buf);
sat_stack_push_string(buf); //push string to stack
sprintf(buf,"time(s): %d",end-start);
printf(" %s\n",buf);
sat_stack_push_string(buf); //push string to stack
sprintf(buf,"digits: %d",cc);
printf(" %s\n",buf);
sat_stack_push_string(buf); //push string to stack
if(end-start == 0)
sprintf(buf,"digits/s: NAN");
else
sprintf(buf,"digits/s: %.2f",(float)cc/(float)(end-start));
printf(" %s",buf);
sat_stack_push_string(buf); //push string to stack
beep();
sys_slowOn(); //normal speed
WAIT_CANCEL; //loop until ON pressed
return(0);
}
Edited: 24 Sept 2007, 11:37 a.m. after one or more responses were posted
|