Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Newfags can't Bruteforce

Name: Anonymous 2010-08-22 21:39

    for(i=2;i<max;i++) {
        for(j=2;j<i;j++) {
            if((i%j)==0) { tmp=1;}
        }
        if(tmp==0) { primes++; }
        tmp=0;
    }

Name: Anonymous 2010-08-25 4:58

Here's an APL dialect, "J".

  pr=:(1&p:#[)@i.
  pr 30
2 3 5 7 11 13 17 19 23 29]


Let's take a look at (1&p:#[)@i.
n&f = monadic g where g p = n f p.
f@g = monadic h where h n = f (g n).
(f g h) n = (f n) g (h n) (called a "monadic fork"), so
((f g h) @ i) n = (f (i n)) g (h (i n)).
So that means pr y is ((1&p:) (i. y)) # ([ (i. y)).
1 p: y returns 1 only if y is prime. i. y returns the first y non-negative integers. [ is the identity function. # is "copy", but with 0 and 1 it can be "filter"]]
  1 2 3#4 5 6
4 5 5 6 6 6
  1 0 1 0 1#1 2 3 4 5
1 3 5

1&p: is applied to all of i. y, returning 1's for all primes. [ i. y is just i. y, so for each 1 in the left list an element of the right list is returned]

Have you read your Learning J today, /prog/?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List