challenges

my solutions to various "programming challenge" problems
git clone https://wehaveforgeathome.hates.computer/challenges.git
Log | Files | Refs | LICENSE

005.pez (747B)


      1 #! /usr/bin/env pez
      2 # real  0m0.008s
      3 # user  0m0.004s
      4 # sys 0m0.008s
      5 
      6 # I find this naive impl to be hi-larious -- it's the def of primality!
      7 # no need to speed it up, since it's only used 18 times with n < 20
      8 : prime?
      9   1
     10   begin
     11     1+ 2dup mod
     12   0= until
     13   =
     14 ;
     15 
     16 # the answer must be a multiple of the product of all the primes less than 20
     17 : productOfPrimes<20 ( -- product )
     18   1
     19   20 2 do
     20     i dup prime? if *  else drop then
     21   loop
     22 ;
     23 
     24 productOfPrimes<20 constant inc
     25 
     26 : evenly_divides_3_to_x ( number_to_try -- number_to_try x )
     27   2
     28   begin
     29     1 +
     30     2dup mod
     31   0 <> until
     32   1 -
     33 ;
     34 
     35 : find_first_to_divide_evenly_1_to_20 ( -- first )
     36   0
     37   begin
     38     inc +
     39     evenly_divides_3_to_x
     40   20 >= until
     41 ;
     42 
     43 find_first_to_divide_evenly_1_to_20 . cr