Duel, Apl, and the shortest program to find prime numbers 
Author Message
 Duel, Apl, and the shortest program to find prime numbers

Hi!
I wonder how many characters is the shortest program to find the first
10 prime numbers over 1000, in APL ....

Background: I am the developer of Duel, a language for C debugging.
I have never used APL seriously, though I'm aware of the semantics,
some of which found their way into Duel. What is Duel? under gdb,
given array x[100] of structs with field level, you could do:

(gdb) dl x[..100].level >? 0
x[42].level = 5
x[67].level = 19

That is, find x[i].level that are positive (the nice symbolic output is
automatic). care for more details? anon ftp to: ftp.cs.princeton.edu:/duel.

Anyway, I needed some prime numbers over 1000, and I ended up using Duel!
This raises the question of whether Duel provides anything interesting to
people who use APL one-liners, or plain old Unix 'bc'.

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.

Comments appreciated!
 Michael Golan



Tue, 05 Sep 1995 23:55:08 GMT  
 Duel, Apl, and the shortest program to find prime numbers
Quote:

>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

1009 1013 1019 1021 1031 1033 1039 1049 1051 1061
   NB.   Selects the first ten primes between 1000 and 1100

   NB.   A statement like

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 :

   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.

   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|
+------+



Wed, 06 Sep 1995 14:36:36 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. finding prime numbers

2. long = short*short ; (short,short) = long/short

3. Prime number program?

4. Prime Number Program

5. Prime number program

6. Prime Number program

7. Prime Numbers Program

8. Program for prime numbers??

9. A simple prime number program

10. Prime Numbers Program

11. Prime numbers program problems

12. Simple program to find average of 3 numbers

 

 
Powered by phpBB® Forum Software