This page (revision-48) was last changed on 03-Feb-2023 15:21 by Stefan Haubenthal 

This page was created on 04-Dec-2010 12:59 by Carsten Strotmann

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
48 03-Feb-2023 15:21 163 bytes Stefan Haubenthal to previous typo
47 01-Jan-2021 10:29 164 bytes Carsten Strotmann to previous | to last
46 14-Dec-2014 21:43 35 KB Carsten Strotmann to previous | to last
45 14-Dec-2014 21:42 35 KB Carsten Strotmann to previous | to last
44 01-Nov-2014 17:28 35 KB Carsten Strotmann to previous | to last
43 01-Nov-2014 17:23 35 KB Carsten Strotmann to previous | to last
42 01-Nov-2014 17:22 35 KB Carsten Strotmann to previous | to last Results VCFB 2014
41 22-Sep-2014 19:43 33 KB Carsten Strotmann to previous | to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 153 added 34 lines
!! Bench PI(n)
COPYRIGHT : Albert van der Horst FIG Chapter Holland
This program and modified versions thereof may be distributed
and used freely provided:
1. this copyright and a www reference to the original is kept
2. the following line states correctly either original or modified.
This version is : modified for Forth83.
The original version is available at http://home.hccnet.nl/a.w.m.van.der.horst/benchpin.frt
DESCRIPTION: This (highly recursive) function calculates PI(n), i.e. the number of primes less or equal to n. It doesn't use a sieve, nor does it inspect numbers larger than the square root of n for primeness. It may be used for benchmarking, because it takes considerable time for large numbers. It is one of the few highly recursive algorithms that actually calculate something sensible.
{{{
\ benchpin -- a highly recursiv function for PI(n) cas 20101204
: ?PRIME ( p -- flag )
>R R@ 4 U< IF R> DROPTRUE EXIT THEN
R@ 1 AND 0= IF R> DROP FALSE EXIT THEN
2 3 BEGIN
R@ OVER /MOD SWAP
0= IF R> DROP 2DROP FALSE EXIT THEN
OVER < IF R> DROP DROP TRUE EXIT THEN
2+ AGAIN ;
: DISMISS ( n1 p -- n2 )
>R R@ / DUP R@ < IF DROP R> 1 EXIT THEN
DUP R> 2 ?DO I ?PRIME IF OVER I RECURSIVE - THEN LOOP
SWAP DROP ;
: PI ( n1 -- n2 ) DUP >R 1- R@ 2 / 1- - 3 BEGIN
DUP DUP * R@ > 0= WHILE DUP ?PRIME IF CR DUP . ." is prime"
R@ OVER DISMISS 1- SWAP >R - R> THEN 2+ REPEAT DROP R> DROP ;
}}}