Programming Comparisons: Primes up to N


Problem: give the smallest possible complete subroutine/function/method definition in a language of your choice which, when called with a number, returns a list (in whatever way your given language can return a list) of the prime numbers lesser than or equal to that number. Each entry is preceded by a commented example of how to call it with the argument: 99 followed by a newline. (The character count includes the call to avoid cheating :-)

APL: 27 ⍝F 99 F R (~R∈R∘.×R)/R←1↓ιR
perl: 46 (87 bz2)
#f(99)
sub f{grep{(1x$_)!~/^(11+)\1+$/}2..pop}
haskell: 46 (86 bz2)
--f 99
f n=[x|x<-[2..n],all((0<).mod x).f$x-1]
ruby: 56 (86 bz2)
#f 99
def f(n)(2..n).reject{|i|f(i-1).find{|j|i%j<1}}end
false: 59 (90 bz2)
{99f;!}
[$[1-$][\$@$@$@$@\/*=[1-$$[%\1-$@]?~[\$@]?]?]#%%]f:
nvi: 60 (105 bz2)
"99g
map g iPYpwX##2bd(x@1dk:g/^\(###*\)\1\1*\</d
:%s/#*
python: 74 (110 bz2)
#f(99)
f=lambda n:[i for i in range(2,n+1)if 0not in[i%j for j in f(i-1)]]
C: 74 (106 bz2)
//int a[99];f(99,a);a
p(n,a)int*a;{for(*a=1;n%++*a||(a+=--n<*a,*a=!!n););}
PHP: 82 (104 bz2)
#f(99)
function f($n){for($i=$n;--$i;)if(!($n%$i))--$i?$i=--$n:$a[]=$n;return $a;}
erlang: 85 (118 bz2)
%f(99)
f(1)->[];f(N)->[X||X<-lists:seq(2,N),lists:all(fun(I)->X rem I>0 end,f(X-1))].
ocaml: 94 (128 bz2)
(*f(ref[])99*)
let f a n=for i=2to n do if List.for_all(fun x->i/x*x<i)!a then a:=i::!a done;a
lisp: 96 (118 bz2)
;(p nil 99)
(defun p(a n)(dotimes(i n)(or(< i 2)(some
#'(lambda(x)(=(mod i x)0))a)(push i a)))a)
prolog: 100 (123 bz2) %p(5,X). r(_,[]).r(X,[Y|Z]):-X mod Y>0,r(X,Z).p(2,[2]).p(N,U):-M is N-1,p(M,L),(r(N,L),[N|L]=U;L=U).
befunge: 102 (114 bz2)
v>-:!v            >15g1-v
01 v _:15p\:05p\%!|v1g50_05g:1v
&^<       ;#g51g50<>-:>#;<-1g5<
>:^>$$>:#._@
scheme: 116 (131 bz2)
;(p nil 99)
(define(p a n)(do((i 2(+ i 1)))((= i n))(if(for-all?
a(lambda(x)(=(modulo i x)0)))(set! a(cons i a))))a)
postscript: 125 (118 bz2)
%99 f
/f{/n exch def 2 1 n{/i exch def/t 0 def 2 1 i 1 sub{/j exch def i j
mod 0 eq{/t t 1 add def}if}for t 0 eq{i}if}for}def