Last active
August 29, 2015 14:16
-
-
Save adrian17/d0d55dd1b5f3fc26705c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prime_sieve =: 3 : 0 | |
sieve_size =. y | |
sieve =. sieve_size # 0 | |
indices =. i. sieve_size | |
sieve =. 1 (0 1) } sieve | |
result =. '' | |
while. sieve_size ~: +/ sieve do. NB. while it has at least one 0 | |
num =. {. (-. sieve) # indices NB. get first number not marked with 1 | |
result =. result, num | |
divisible =. 0 = num | indices | |
divisible =. divisible # indices | |
sieve =. 1 divisible } sieve | |
end. | |
result | |
) | |
NB. adapted from Wikipedia's article on APL; O(n^2) complexity. | |
slower_prime =. 3 : 0 | |
a =. 1 + 1 }. i. y | |
b =. a */ a | |
c =. -. a e. ;b | |
c # a | |
) | |
NB. tried to shorten it to tacit definition, but I have no idea how to merge these two lines together | |
shorter_slower_prime =. 3 : 0 | |
a =. 1 + 1 }. i. y | |
(-. a e. ; a */ a) # a | |
) | |
prime_sieve 100 | |
slower_prime 100 | |
shorter_slower_prime 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment