Plan 9 from Bell Labs’s /usr/web/sources/contrib/fgb/root/sys/src/cmd/4th/examples/euler27.4th

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


\ code to support Project Euler problem 27

include lib/pickroll.4th

: ?prime  ( n -- f )                   \ check is number is prime
  dup 1 >                              \ only test when positive
  if  1 begin   1+ 2dup  /  2dup >     \ treat negative numbers,
          if 2drop dup 1 then          \ zero, and one as non-prime
          over * 2 pick =
        until  =
  else  drop FALSE
  then
;

: numprimes ( a b -- num )             \ find # of primes made by n^2+an+b
  1000 0
  do  2dup  i rot over + * +
    ?prime not                         \ test if prime
    if  2drop i  leave  then           \ if not, then return count
  loop                                 \ else keep testing
;

1 41 numprimes . cr                    \ should print 40
2 41 numprimes . cr                    \ should print 1 

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].