Skip to content

Instantly share code, notes, and snippets.

@adrian17
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrian17/d0d55dd1b5f3fc26705c to your computer and use it in GitHub Desktop.
Save adrian17/d0d55dd1b5f3fc26705c to your computer and use it in GitHub Desktop.
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