Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!utnut!cs.utexas.edu!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!decwrl!csus.edu!sfsuvax1.sfsu.edu!emclean
From: emclean@sfsuvax1.sfsu.edu (Emmett McLean)
Subject: Re: Duel, Apl, and the shortest program to find prime numbers
Message-ID: <1993Mar20.063636.29115@csus.edu>
Sender: news@csus.edu
Organization: San Francisco State University
References: <1993Mar19.155508.13034@Princeton.EDU>
Date: Sat, 20 Mar 1993 06:36:36 GMT
Lines: 72

In article <1993Mar19.155508.13034@Princeton.EDU> mg@elan (Michael Golan) writes:
>Hi!
>I wonder how many characters is the shortest program to find the first
>10 prime numbers over 1000, in APL ....
>
>To get the first 10 prime numbers over 1000, in Duel:
>(all the spaces are redundant. x=>y creates an implied loop over x, returning
> y, where _ is the index of the loop)
>
>(1000.. => if(&&/( 2,3.._-1 => __%_)) _ )[[..10]]
>
>How much work it takes in APL? Duel uses Icon-like generators, so the above
>is iterative, not lists/arrays manipulations. The above code does not
>assume, e.g. that there must be 10 primes between 1000 and 2000.
>
   NB. (NB. means "comment" as what follows can loaded and run in J)
   
   NB.   A statment like 
   10&{.@(#~(1:=+/@(0&=)@(|/~i.))"0) (1000+i. 100)
1009 1013 1019 1021 1031 1033 1039 1049 1051 1061
   NB.   Selects the first ten primes between 1000 and 1100
   
   
   NB.   A statement like 
   >@{.@(({. , >:&.>@}.)`(<@(>@{. , >@}.) , >:&.>@}.)@.(''&$@(1&=@(+/@(0&=@((|/~ i.)@(>@}.))))))^:(10&~:@#@>@{.)) (i.0);1000
1009 1013 1019 1021 1031 1033 1039 1049 1051 1061
   NB.    Starts with 1000 and continues to select primes until 10 are found.
   NB.    The spaces could be deleted to make it somewhat leaner but it makes
   NB.    the look of the code unnatural. This solution requires
   NB.    significantly more characters than the Deul solution.
   NB.    Generally, if :
   d =. >@{.@(({. , >:&.>@}.)`(<@(>@{. , >@}.) , >:&.>@}.)@.(''&$@(1&=@(+/@(0&=@((|/~ i.)@(>@}.))))))^:(10&~:@#@>@{.)) 
   
   NB.    Then d (i.0;x) finds the next prime numbers succeeding x. So :
   d (i.0); 30
31 37 41 43 47 53 59 61 67 71
   
   NB.    A verb which finds all the primes up to a given number by going thru
   NB.    all odd numbers up to the ceiling of the square root of the input
   NB.    and selecting those which are prime is:
   NB. 
   ph =. (#~ (1&=)@(+/)@(0&=)@(|/~ i.@>:@<.@%:)"0)@(>:@+:@i.@>.@-:)
   
   NB. So the 168 prime numbers between 1 up to 1000 are
   
   21 8 $ ph 1000
  1   3   5   7  11  13  17  19
 23  29  31  37  41  43  47  53
 59  61  67  71  73  79  83  89
 97 101 103 107 109 113 127 131
137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223
227 229 233 239 241 251 257 263
269 271 277 281 283 293 307 311
313 317 331 337 347 349 353 359
367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457
461 463 467 479 487 491 499 503
509 521 523 541 547 557 563 569
571 577 587 593 599 601 607 613
617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719
727 733 739 743 751 757 761 769
773 787 797 809 811 821 823 827
829 839 853 857 859 863 877 881
883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997
   
   <'Emmett'
+------+
|Emmett|
+------+
