FORTH for the SHARP PC-E500 (S)
06-21-2022, 02:16 AM
Post: #70
 robve Senior Member Posts: 459 Joined: Sep 2020
RE: FORTH for the SHARP PC-E500 (S)
Compute digits of pi.
The following Forth500 program is based on the C code to compute the digits of pi by Dik T. Winter, CWI Amsterdam. It computes up to 9864 digits of pi, but is memory-restricted to about 6000 digits max:
Code:
.( Loading BIG-PI...) ANEW _BIG_PI_ DECIMAL 0 VALUE b       0 VALUE c 0 VALUE e       0 VALUE g 0. 2VALUE d      \ array f located at HERE + 40 bytes (hold area) : f!    CELLS HERE + 40 + ! ; : f@    CELLS HERE + 40 + @ ; : big-pi    ( +n -- )   DUP 4 < ABORT" too small"   \ c=7*n/2; c-=c%14   7 UM* D2/ D>S DUP 14 MOD - TO c   \ check for sufficient space to store array f   c UNUSED 40 - 1 RSHIFT U> ABORT" out of memory"   \ f[0...c-1]=2000   c 0 DO 2000 I f! LOOP   \ e=0   0 TO e   CR   BEGIN     \ d=0; g=2*c-1; b=c     0. TO d     c 2* 1- TO g     c TO b     BEGIN       \ d+=f[b]*10000; f[b]=d%g; d/=g; g-=2; b--       b f@ 10000 UM* +TO d       d g 0 D/MOD       TO d       D>S b f!       -2 +TO g       -1 +TO b     b WHILE       \ d*=b       d b UMD* TO d     REPEAT     \ printf("%.4u",e+d/10000); e=d%10000     d 10000 SM/REM     e + 0 <# # # # # #> TYPE     TO e     \ c-=14     -14 +TO c   c 0= UNTIL ;

A screenful with the first 152 digits of pi (this takes one minute to compute):
Code:
152 big-pi 3141592653589793238462643383279502884197 1693993751058209749445923078164062862089 9862803482534211706798214808651328230664 70938446095505822317253594081284 OK[0]

A C program for the PC-G850(V)(S):
Code:
1 unsigned long a=10000,d; 2 unsigned b,c,e,*f,g; 3 main(){ 4  printf("digits?");scanf("%u",&c); 5  c=7*c/2;c-=c%14;f=malloc(4*c+4); 6  for(;b-c;)f[b++]=a/5; 7  for(;d=0,g=c*2;c-=14,printf("%.4lu",e+d/a),e=d%a) 8   for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b); 9 }

The PC-G850VS calculates the first 152 digits of pi in 1 minute and 15 seconds.

This slightly modified version in C with 64 bit integers correctly computes up to 54935 digits of pi:
Code:
uint64_t a=10000,b,c,d,e,*f,g; main(){printf("digits?");scanf("%llu",&c);c*=3.5;c-=c%14;f=malloc(sizeof(*f)*(c+1)); for(;b-c;)f[b++]=a/5;for(;d=0,g=c*2;c-=14,printf("%.4llu",e+d/a),e=d%a) for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);}

The 54936th digit is off by one, followed by 10000 instead of 0000:

362524395716152714669005814610000
___________________________^x____
where a 7 should appear in place of the 6, falling the 1 to carry over to the 6. When pushing for 100000 digits this happens a few more times. I reckon the algorithm can be fixed to perform the carry. But this algorithm is quite slow for long digit sequences. There are also better algorithms, e.g. BBP, Chudnovsky.

- Rob

"I count on old friends to remain rational"
 « Next Oldest | Next Newest »

 Messages In This Thread FORTH for the SHARP PC-E500 (S) - Helix - 09-06-2021, 11:41 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-12-2021, 02:00 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-12-2021, 11:40 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-13-2021, 01:12 AM RE: FORTH for the SHARP PC-E500 (S) - xerxes - 09-15-2021, 11:51 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-08-2021, 04:22 AM RE: FORTH for the SHARP PC-E500 (S) - xerxes - 11-09-2021, 10:59 PM RE: FORTH for the SHARP PC-E500 (S) - dmh - 10-02-2022, 02:29 PM RE: FORTH for the SHARP PC-E500 (S) - xerxes - 10-04-2022, 12:02 AM RE: FORTH for the SHARP PC-E500 (S) - dmh - 10-04-2022, 12:46 PM RE: FORTH for the SHARP PC-E500 (S) - xerxes - 10-04-2022, 10:39 PM RE: FORTH for the SHARP PC-E500 (S) - dmh - 10-04-2022, 10:55 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-16-2021, 12:47 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-16-2021, 05:06 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-17-2021, 08:45 PM RE: FORTH for the SHARP PC-E500 (S) - toml_12953 - 09-17-2021, 09:25 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-22-2021, 10:55 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-21-2021, 06:12 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-19-2021, 07:14 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-28-2021, 03:28 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-28-2021, 09:03 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-29-2021, 09:23 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-29-2021, 10:27 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-30-2021, 09:42 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-04-2021, 06:43 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-05-2021, 12:28 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-05-2021, 01:02 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-06-2021, 02:41 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-06-2021, 11:35 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-07-2021, 05:55 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-07-2021, 11:48 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-08-2021, 01:04 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-10-2021, 01:38 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-08-2021, 05:36 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-08-2021, 07:16 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-10-2021, 09:11 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-10-2021, 01:22 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-10-2021, 01:36 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-13-2021, 02:38 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-26-2021, 05:45 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-29-2021, 11:46 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-28-2021, 03:11 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-29-2021, 04:03 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-30-2021, 07:09 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-30-2021, 09:12 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-31-2021, 01:57 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 11-01-2021, 12:34 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-02-2021, 07:58 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 11-03-2021, 11:58 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-04-2021, 05:48 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-05-2021, 02:04 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-06-2021, 10:40 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-06-2021, 07:31 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-07-2021, 01:15 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-07-2021, 01:00 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-07-2021, 03:40 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-07-2021, 05:44 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-07-2021, 08:19 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-09-2021, 08:23 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-13-2021, 12:07 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-13-2021, 07:19 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-22-2021, 05:42 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 11-24-2021, 11:33 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-25-2021, 02:54 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 01-12-2022, 11:06 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 01-13-2022, 03:40 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 01-16-2022, 09:22 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 01-18-2022, 12:32 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 02-19-2022, 09:40 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 02-20-2022, 09:13 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 02-20-2022, 11:12 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 04-03-2022, 12:53 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 04-03-2022, 11:06 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 04-04-2022, 12:47 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 06-21-2022 02:16 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-16-2022, 06:54 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-13-2022, 11:30 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-16-2022, 11:09 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-17-2022, 12:38 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-17-2022, 10:44 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-18-2022, 12:21 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-15-2023, 10:06 AM RE: FORTH for the SHARP PC-E500 (S) - rprosperi - 09-15-2023, 11:47 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-15-2023, 10:15 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-06-2022, 04:15 AM RE: FORTH for the SHARP PC-E500 (S) - rprosperi - 11-06-2022, 01:20 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 11-14-2022, 12:55 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-14-2022, 01:18 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 11-14-2022, 02:17 AM

User(s) browsing this thread: 1 Guest(s)