initial import of 1.02

This commit is contained in:
Dan Ballard 2011-03-07 23:01:27 -08:00
commit 670e39d546
52 changed files with 2473 additions and 0 deletions

12
ChangeLog Normal file
View File

@ -0,0 +1,12 @@
1.2 (2011 03 08)
Poked autoTest.pl so lisp now runs on SBCL
Added imports to .cpp and .m so they compiled again
Other changes so things run on Ubuntu 10.04
1.1 (2004 10 08)
Added languages Smalltalk and Nickle and M4
Fixes for autotest.pl
Added README and ChangeLog
1.0 (2003 12 31)
Full release with 34 languages

6
Makefile Normal file
View File

@ -0,0 +1,6 @@
default:
./autoTest.pl
clean:
rm -f *.txt primesc primescpp primesaltc primes.class primespas primes*.exe primes.o primesobjc primes.ali primesada primesfor primese primes.h primes1.c primes1.o primes.id primes.make primescbl link.res ppas.sh primes.beam primesocaml primesasm primes.cmi primes.cmx luac.out primeshs primes_clisp.fas primes.lib Main.hi primes_clisp.lib primes_clisp.o primes.hi primescsieve gst.im primesalt2c primesalt3c

12
README Normal file
View File

@ -0,0 +1,12 @@
http://www.mindstab.net/tags/primes/
haplo@mindstab.net
Mindstab.net Multi language prime number project:
Prime number finders in ~38 languages
and a test driver
2011: Re-release
This project is still a terrible blunt way to see how different languages/implementation perform and compare at basic arithmatic and looping and crudly look at speed. And it's a small roseta stone too. Autotest.pl drives it all and it's appaling speghetti code I wrote half a decade ago and I probably still knew better. I've made a few quick updates since 1.01 from 2006 to make it run on Ubuntu 10.04 which is what I'm running. Your milage may vary, but it's on github now, so feel free to offer suggestions in code.
2004-2006
With this project I want to implement prime number finders in as many languages as I can. It's pretty much a way for me to learn a littlebit about a lot of languages. This project also comes with a test program (autoTest.pl) which can run and benchmark all the languages so I can see their various performances (part of learning about the languages).

693
autoTest.pl Executable file
View File

@ -0,0 +1,693 @@
#!/usr/bin/env perl
# www.mindstab.net/primes
# Run prime number finders
# haplo@mindstab.net
print "Mindstab Development Multi-language Prime Number Project v1.02\n(www.mindstab.net/tag/primes/)\nhaplo\@mindstab.net\n\n";
# results file
$RESULTS="results.txt";
$TIME="time -p";
$os = `uname`;
$debug=0;
@langs = ("c",
# Uncomment for alternat C algorithms
# "c2", "ca", "ca2", "ca3",
"objc", "cpp", "asm", "for", "pas", "ada", "hs", "e", "cbl", "oml", "java", "cs", "vb", "erl", "awk", "pl", "php", "py", "tcl", "rb1.8", "rb1.9", "cl", "scm", "st", "sml", "4th", "pro", "m3", "pike", "lua", "rexx", "r", "fe", "5c", "m4", "sh");
$llen = $#langs+1;
print "Number of iterations to run tests [3]: ";
$itr = <stdin>;
chomp($itr);
print "\n";
if($itr < 1) { $itr = 3; }
print "Maximun number to search to [100000]: ";
$max = <stdin>;
chomp($max);
print "\n";
if($max < 1) { $max = 100000; }
#sources
$data{'c'}{'name'} = "C";
$data{'c'}{'src'}= "primes.c";
$data{'c'}{'prog'} = "primesc";
$data{'c'}{'compiler0'} = "gcc";
$data{'c'}{'cflags0'} = "-lm -O3 -o " . $data{'c'}{'prog'};
$data{'c2'}{'name'} = "C Sieve";
$data{'c2'}{'src'}= "primes.sieve.c";
$data{'c2'}{'prog'} = "primescsieve";
$data{'c2'}{'compiler0'} = "gcc";
$data{'c2'}{'cflags0'} = "-lm -O3 -o " . $data{'c2'}{'prog'};
$data{'ca'}{'name'} = "C Alternate Algorithm (Variable sqrt monitoring)";
$data{'ca'}{'src'}= "primes.alt.c";
$data{'ca'}{'prog'} = "primesaltc";
$data{'ca'}{'compiler0'} = "gcc";
$data{'ca'}{'cflags0'} = "-lm -O3 -o " . $data{'ca'}{'prog'};
$data{'ca2'}{'name'} = "C Alternate2 Algorithm (Variable sqrt monitoring and c math modulus)";
$data{'ca2'}{'src'}= "primes.alt2.c";
$data{'ca2'}{'prog'} = "primesalt2c";
$data{'ca2'}{'compiler0'} = "gcc";
$data{'ca2'}{'cflags0'} = "-lm -O3 -o " . $data{'ca2'}{'prog'};
$data{'ca3'}{'name'} = "C Alternate3 Algorithm (Variable sqrt monitoring and recursion)";
$data{'ca3'}{'src'}= "primes.alt3.c";
$data{'ca3'}{'prog'} = "primesalt3c";
$data{'ca3'}{'compiler0'} = "gcc";
$data{'ca3'}{'cflags0'} = "-lm -O3 -o " . $data{'ca3'}{'prog'};
$data{'asm'}{'name'} = "x86 Assembly";
$data{'asm'}{'src'} = "primes.x86.s";
$data{'asm'}{'prog'} = "primesasm";
$data{'asm'}{'compiler0'} = "gcc";
$data{'asm'}{'cflags0'} = "-O3 -o ". $data{'asm'}{'prog'};
$data{'asm'}{'pre'} = "echo \"$max\" | ";
$data{'asm'}{'osfilter'} = "OpenBSD";
$data{'objc'}{'name'} = "Objective C";
$data{'objc'}{'src'}= "primes.m";
$data{'objc'}{'compiler0'} = "gcc";
$data{'objc'}{'prog'} = "primesobjc";
$data{'objc'}{'cflags0'} = "-lobjc -lm -O3 -o " . $data{'objc'}{'prog'};
$data{'objc'}{'osfilter'} = "FreeBSD";
$data{'cpp'}{'name'} = "C++";
$data{'cpp'}{'src'}= "primes.cpp";
$data{'cpp'}{'compiler0'} = "g++";
$data{'cpp'}{'prog'} = "primescpp";
$data{'cpp'}{'cflags0'} = "-lm -O3 -o " . $data{'cpp'}{'prog'};
$data{'for'}{'name'} = "Fortran";
$data{'for'}{'src'} = "primes.f";
$data{'for'}{'compiler0'} = "g77";
$data{'for'}{'prog'} = "primesfor";
$data{'for'}{'cflags0'} = "-O3 -o $data{'for'}{'prog'}";
$data{'pas'}{'name'} = "Pascal";
$data{'pas'}{'src'} = "primes.pas";
$data{'pas'}{'compiler0'} = "fpc";
$data{'pas'}{'prog'} = "primespas";
$data{'pas'}{'cflags0'} = "-o$data{'pas'}{'prog'}";
$data{'ada'}{'name'} = "Ada";
$data{'ada'}{'src'} = "primes.adb";
$data{'ada'}{'compiler0'} = "gnatmake";
$data{'ada'}{'prog'} = "primesada";
$data{'ada'}{'cflags0'} = "-o $data{'ada'}{'prog'}";
$data{'ada'}{'cflagspre0'} = "rm -f primes.ali primes.o && ";
$data{'e'}{'name'} = "Eiffel";
$data{'e'}{'src'}= "primes.e";
$data{'e'}{'compiler0'} = "compile_to_c";
$data{'e'}{'prog'} = "primese";
$data{'e'}{'cflags0'} = "$data{'e'}{'src'} -boost -O3 -o " . $data{'e'}{'prog'}. " && sh primes.make";
$data{'e'}{'cflagspre0'} = "rm -f primes1.o && ";
$data{'awk'}{'name'} = "AWK";
$data{'awk'}{'src'} = "primes.awk";
$data{'awk'}{'pre0'} = "echo $max | ";
$data{'awk'}{'interp0'} = "awk";
$data{'awk'}{'iflags0'} = "-f " . $data{'awk'}{'src'};
$data{'sml'}{'name'} = "SML";
$data{'sml'}{'src'} = "primes.sml";
#$data{'sml'}{'iflagspre0'} = "echo primes $max | ";
$data{'sml'}{'iflagspre0'} = "cat $data{'sml'}{'src'} | sed -e 's/.*autotest.*/primes $max/' |";
$data{'sml'}{'interp0'} = "sml";
$data{'sml'}{'filter0'} = "sed -e '/[a-zA-Z\\-\\=]/g'";
$data{'m4'}{'name'} = "M4";
$data{'m4'}{'src'} = "primes.m4";
$data{'m4'}{'iflagspre0'} = "cat $data{'m4'}{'src'} | sed -e 's/.*autotest.*/PRIMES(3,$max,2,4)/' |";
$data{'m4'}{'interp0'} = "m4";
$data{'m4'}{'noarg'} = 1;
$data{'m4'}{'warn'} = "Execution is approximately 20-30 times slower than perl.";
$data{'m4'}{'reprompt'} = 1;
$data{'rexx'}{'name'} = "REXX";
$data{'rexx'}{'interp0'} = "rexx";
$data{'rexx'}{'prog'} = "primes.rexx";
$data{'r'}{'name'} = "REBOL";
$data{'r'}{'src'} = "primes.r";
$data{'r'}{'interp0'} = "rebol";
$data{'r'}{'iflags0'} = "--quiet $data{'r'}{'src'} $max";
$data{'oml'}{'name'} = "OCaml";
$data{'oml'}{'src'} = "primes.ml";
$data{'oml'}{'prog'} = "primesocaml";
$data{'oml'}{'compiler0'} = "ocamlopt";
$data{'oml'}{'cflags0'} = "-o $data{'oml'}{'prog'}";
$data{'oml'}{'cflagspre0'} = "rm -f primes.cmi primes.cmx && ";
$data{'erl'}{'name'} = "Erlang";
$data{'erl'}{'src'} = "primes.erl";
$data{'erl'}{'compiler0'} = "erlc";
$data{'erl'}{'interp0'} = "erl";
$data{'erl'}{'iflags0'} = "-noshell -s primes primes '$max'";
$data{'4th'}{'name'} = "Fourth";
$data{'4th'}{'src'} = "primes.fs";
$data{'4th'}{'interp0'} = "gforth-fast";
$data{'4th'}{'iflags0'} = "$data{'4th'}{'src'} -e '$max 2 4 primes bye'";
$data{'cbl'}{'name'} = "COBOL";
$data{'cbl'}{'src'} = "primes.cbl";
$data{'cbl'}{'prog'} = "primescbl";
$data{'cbl'}{'compiler'} = 2;
$data{'cbl'}{'compiler0'} = "cobc";
$data{'cbl'}{'cflags0'} = "-o $data{'cbl'}{'prog'} -O";
$data{'cbl'}{'compiler1'} = "htcobolpp";
$data{'cbl'}{'cflags1'} = "-o $data{'cbl'}{'prog'} -x ";
$data{'cbl'}{'pre'} = "echo \"$max\" | ";
$data{'cbl'}{'warn'} = "Execution is a slower than average, but nothing to worry about";
$data{'scm'}{'name'} = "Scheme";
$data{'scm'}{'src'} = "primes.scm";
$data{'scm'}{'interp'} = 3;
$data{'scm'}{'interp0'} = "scm";
$data{'scm'}{'iflags0'} = "-f ". $data{'scm'}{'src'}." -e \\\\\\\"(primes 3 $max)\\\\\\\"";
$data{'scm'}{'interp1'} = "mzscheme";
$data{'scm'}{'iflags1'} = "-m -f ". $data{'scm'}{'src'}." -e \\\\\\\"(and (primes 3 $max) (exit))\\\\\\\"";
$data{'scm'}{'interp2'} = "guile";
$data{'scm'}{'iflags2'} = "-l $data{'scm'}{'src'} -c '(primes 3 $max)'";
$data{'cl'}{'name'} = "Common Lisp";
$data{'cl'}{'src'} = "primes.lisp";
#$data{'cl'}{'compiler'} = 1;
#$data{'cl'}{'compiler0'} = "clisp";
#$data{'cl'}{'compiler0'} = "gcl";
#$data{'cl'}{'cflagspre0'} = "rm -f primes_clisp.lib primes_clisp.fas && ";
#$data{'cl'}{'cflagspre0'} = "rm -f primes2.o && ";
#$data{'cl'}{'cflags0'} = "-c";
#$data{'cl'}{'cflags0'} = "-compile";
$data{'cl'}{'interp'} = 1;#2
#$data{'cl'}{'interp0'} = "clisp";
#$data{'cl'}{'interp0'} = "gcl";
#$data{'cl'}{'iflags0'} = "-q -i primes2.fas -x '(primes $max)'";
#$data{'cl'}{'iflags0'} = "-load primes2.o -eval '(progn (primes $max) (bye))'";
$data{'cl'}{'interp0'} = "sbcl";
#$data{'cl'}{'interp0'} = "lisp";
$data{'cl'}{'iflags0'} = "--noinform --load primes.lisp --eval '(progn (primes $max) (quit))'";
#$data{'cl'}{'iflags0'} = "-quiet -load primes2.cl -eval '(progn (primes $max) (quit))'";
#"--noinform --load primes_clisp.cl --eval '(progn (primes $max) (quit))'";
$data{'cl'}{'filter0'} = "sed -e '/[;N]/g'";
#$data{'cl'}{'filter0'} = "sed -e '/[A-Z]/g'";
$data{'pro'}{'name'} = "Prolog";
$data{'pro'}{'src'} = "primes.prolog.pl";
$data{'pro'}{'interp'} = 2;
$data{'pro'}{'interp0'} = "pl";
$data{'pro'}{'iflagspre0'} = "echo '[primes_prolog]. primes($max).' | ";
$data{'pro'}{'iflags0'} = "-q";
$data{'pro'}{'filter0'} = "sed -e '/?/g'";
$data{'pro'}{'interp1'} = "gprolog";
$data{'pro'}{'iflags1'} = "--init-goal '[primes_prolog]' --entry-goal 'primes($max)' --query-goal 'halt(0)'";
$data{'pro'}{'iflagspre1'} = "export GLOBALSZ=40000; ";
$data{'pro'}{'filter1'} = "sed -e '/[?A-Za-z]/g'";
$data{'pl'}{'name'} = "PERL";
$data{'pl'}{'prog'} = "primes.perl.pl";
$data{'pl'}{'interp0'} = "perl";
$data{'5c'}{'name'} = "Nickle";
$data{'5c'}{'prog'} = "primes.5c";
$data{'5c'}{'interp0'} = "nickle";
$data{'5c'}{'warn'} = "Execution is approximately 20-30 times slower than perl.";
$data{'5c'}{'reprompt'} = 1;
$data{'st'}{'name'} = "Smalltalk";
$data{'st'}{'prog'} = "primes.st";
$data{'st'}{'interp0'} = "gst";
$data{'st'}{'iflags0'} = "-g $data{'st'}{'prog'} -a $max";
$data{'lua'}{'name'} = "Lua";
$data{'lua'}{'src'} = "primes.#VERSION#.lua";
#$data{'lua'}{'versionedsrc'} = 1;
$data{'lua'}{'versionfinder'} = "lua -v 2>&1 | awk '{print \$2}' | sed -e 's/[^54]//g'";
$data{'lua'}{'interp0'} = "lua";
$data{'lua'}{'iflagspre4'} = "echo $max | ";
$data{'pike'}{'name'} = "Pike";
$data{'pike'}{'prog'} = "primes.pike";
$data{'pike'}{'interp0'} = "pike";
$data{'m3'}{'name'} = "Modula-3";
$data{'m3'}{'compiler0'} = "m3build";
$data{'m3'}{'prog'} = "`ls */primesm3`";
$data{'tcl'}{'name'} = "TCL";
$data{'tcl'}{'src'} = "primes.tcl";
$data{'tcl'}{'interp'} = 2;
$data{'tcl'}{'interp0'} = "tclsh";
$data{'tcl'}{'iflags0'} = "$data{'tcl'}{'src'} $max";
$data{'tcl'}{'interp1'} = "tclsh8.3";
$data{'tcl'}{'iflags1'} = "$data{'tcl'}{'src'} $max";
$data{'tcl'}{'warn'} = "Execution is slower than average, but nothing to worry about";
$data{'sh'}{'name'} = "Shell";
$data{'sh'}{'prog'} = "primes.sh";
$data{'sh'}{'reprompt'} = 1;
$data{'sh'}{'interp0'} = "sh";
$data{'sh'}{'warn'} = "Execution is painfully slow.\n\t\t Do not try with numbers greater than a few thousand.";
$data{'hs'}{'name'} = "Haskell";
$data{'hs'}{'src'} = "primes.hs";
$data{'hs'}{'prog'} = "primeshs";
$data{'hs'}{'compiler'} = 2;
$data{'hs'}{'compiler0'} = "ghc";
$data{'hs'}{'cflags0'} = "-O -o $data{'hs'}{'prog'}";
$data{'hs'}{'filter0'} = "sed -e 's/\.\]/\ /g'";
$data{'hs'}{'cflagspre0'} = "rm -f Main.hi primes.o && ";
$data{'hs'}{'warn'} = "Compiler is slow";
$data{'hs'}{'compiler1'} = "interpB";
# Uncomment for Hugs, appears to be crazy slow or broken?
#$data{'hs'}{'interpB'} = "hugs";
#$data{'hs'}{'preB'} = "echo \'primes 3 $max 2 4\' | ";
#$data{'hs'}{'iflagsB'} = $data{'hs'}{'src'};
#$data{'hs'}{'filterB'} = "sed -e 's/\.\]/\ /g' | sed -e 's/Main>/\ /g' | sed -e '/[a-zA-Z\/]/g' | sed -e '/ /g' ";
#$data{'hs'}{'warnB'} = "Haskell compiler (ghc) not found.\nFalling back to Haskell interpreter (hugs).\nThis method is very slow!";
$data{'py'}{'name'} = "Python";
$data{'py'}{'prog'} = "primes.py";
$data{'py'}{'interp0'} = "python";
$data{'php'}{'name'} = "PHP";
$data{'php'}{'prog'} = "primes.php";
$data{'php'}{'interp0'} = "php";
$data{'fe'}{'name'} = "Ferite";
$data{'fe'}{'prog'} = "primes.fe";
$data{'fe'}{'interp0'} = "ferite";
$data{'fe'}{'pre0'} = "echo $max | ";
$data{'rb1.8'}{'name'} = "Ruby 1.8";
$data{'rb1.8'}{'prog'} = "primes.rb";
$data{'rb1.8'}{'interp0'} = "ruby1.8";
$data{'rb1.9'}{'name'} = "Ruby 1.9.1";
$data{'rb1.9'}{'prog'} = "primes.rb";
$data{'rb1.9'}{'interp0'} = "ruby1.9.1";
$data{'java'}{'name'} = "Java";
$data{'java'}{'src'} = "primes.java";
$data{'java'}{'compiler0'} = "javac";
$data{'java'}{'interp0'} = "java";
$data{'java'}{'prog'} = "primes";
$data{'java'}{'osfilter'} = "FreeBSD";
$data{'java'}{'osreprompt'} = "OpenBSD";
$data{'java'}{'oswarn'} = "OpenBSD";
$data{'java'}{'oswarnmsg'} = "This language has been flagged as unstable on this operating system";
$data{'cs'}{'name'} = "C# (Mono)";
$data{'cs'}{'src'} = "primes.cs";
$data{'cs'}{'compiler0'} = "mcs";
$data{'cs'}{'prog'} = "primescs.exe";
$data{'cs'}{'cflags0'} = "-o $data{'cs'}{'prog'}";
$data{'cs'}{'interp'} = 2;
$data{'cs'}{'interp0'} = "mono";
$data{'cs'}{'interp1'} = "mint";
$data{'vb'}{'name'} = "Visual Basic .Net (Mono)";
$data{'vb'}{'src'} = "primes.vb";
$data{'vb'}{'compiler0'} = "mbas";
$data{'vb'}{'prog'} = "primesvb.exe";
$data{'vb'}{'cflags0'} = "-o $data{'vb'}{'prog'}";
$data{'vb'}{'interp'} = 2;
$data{'vb'}{'interp0'} = "mono";
$data{'vb'}{'interp1'} = "mint";
$data{'vb'}{'osfilter'} = "FreeBSD";
sub checkValid
{
print "Checking outputed results for validity...\n";
local(@res) = `wc primes.*.txt`;
local($i);
for($i=0; $i < $#res; $i++)
{
$arr[$i][0] = `echo \"$res[$i]\" | awk '{print \$2}'`;
chomp($arr[$i][0]);
chomp($arr[$i][0]);
$arr[$i][1] = `echo \"$res[$i]\" | awk '{print \$4}'`;
chomp($arr[$i][1]);
chomp($arr[$i][1]);
}
# scan all others for differencees, for each match add 1 to [i][2]
$pt = 0;
for($i=0; $i<=$#arr; $i++)
{
$arr[$i][2] = 0;
for($p=0; $p <= $#arr; $p++)
{
if($p==$i) { next; }
if($arr[$i][0] == $arr[$p][0]) { $arr[$i][2]++; }
}
if($arr[$i][2] > $pt) {$pt = $arr[$i][2];}
}
print "Checked ". ($#arr+1)." ouput files of $execd executed languages for validity:\n";
if($pt != $#arr )
{
print "Differences detected in ".(($#arr+1)-($pt+1)) ." output files:\n";
for($i=0; $i<= $#arr; $i++)
{
if($arr[$i][2] < $pt || $pt == 0) {
print "\t". $arr[$i][1]."\n";
}
}
} else {
print "All output appears to be valid!\n";
}
print "View benchmark results in 'results.txt'\n";
}
sub parseTimes
{
if($debug) {
print "\n$cmd\n";
}
for($i=0; $i< $itr; $i++)
{
if($rcmd ne "") { `$rcmd`; }
my $tstr = `$cmd`;
print ".";
($tm[$i][0], $tm[$i][1], $tm[$i][2]) = $tstr =~
/[^\d]*(\d*\.\d*)[^\d]*(\d*\.\d*)[^\d]*(\d*\.\d*).*/;
}
my $tr = $tu = $ts =0.0;
for($i=0; $i< $itr; $i++)
{
$tr += $tm[$i][0];
$tu += $tm[$i][1];
$ts += $tm[$i][2];
}
$tr /= $itr;
$tu /= $itr;
$ts /= $itr;
$tr = substr($tr, 0,4);
$tu = substr($tu, 0,4);
$ts = substr($ts, 0,4);
#print results "$tr real\t$tu user\t$ts sys\n";
if(length($tr) == 1) { $tr .= "."; }
if(length($tr) == 2) { $tr .= "0"; }
if(length($tr) == 3) { $tr .= "0"; }
print results "$tr seconds\n";
}
sub getByLocate
{
local($where) = `locate $_[0] | grep bin | grep -v cgi | grep -v port | grep -v src`;
($where) = $where =~ /(.*\/$_[0])\n.*$/;
#print "WHERE: '$where'\n";
if($where eq "") { return ""; } else { return $where; }
}
sub getInterp
{
local($where) = `/usr/bin/whereis $_[0] | grep -v port`;
if(! ($where =~ "\:")) {
if($where eq "") { return getByLocate($_[0]); }
else { return "/usr/bin/env $_[0]";
}
} else
{
local($t1, $t2) = split(":", $where);
chomp($t2);
if($t2 ne "") {
return "/usr/bin/env $_[0]";
} else {
return getByLocate($_[0]);
}
}
}
# HEADER
open(results, ">".$RESULTS) || die("Cannot open $RESULTS\n");
print results "Prime number benchmarks\n";
$mach = `uname -a`;
print results "Benchmark machine: " . $mach."\n";
# TESTS
$yesall = 0;
$item = 0;
$execd = 0;
$compiler="";
foreach $l (@langs)
{
$item++;
$interpB = 0;
$compiler = "";
$version = "";
if($data{$l}{'osreprompt'} && $os =~ $data{$l}{'osreprompt'}) {
$data{$l}{'reprompt'} = 1;
}
if($data{$l}{'osreprompt'} && $os =~ $data{$l}{'oswarn'}) {
$data{$l}{'warn'} = $data{$l}{'oswarnmsg'};
}
if($yesall == 0 || $data{$l}{'reprompt'}){
print "($item/$llen) Run ".$data{$l}{"name"}." test\n";
if($data{$l}{'warn'} && !$data{$l}{"reprompt"}) { print "WARNING: $data{$l}{'warn'}\n";}
if($data{$l}{'reprompt'}) {
print "RE-PROMPT: WARNING: ";
if($data{$l}{"warn"}) {
print $data{$l}{'warn'};
} else {
print "Execution is excessively slow.";
}
print "\n";
}
print "(Y)es yes(A)ll (Q)uit [N]o : ";
$query = <stdin>;
$query = substr($query,0,1);
}
if($query eq "a" || $query eq "A")
{
$yesall = 1;
}
if(($yesall == 1 && !$data{$l}{'reprompt'}) || $query eq "y" || $query eq "Y" )
{
if($yesall == 1) {
if($query eq "n" || $query eq "N") { $query = "y";}
}
if($query eq "Y") {$query eq "y"; }
if($yesall == 1 && $query eq "y" && !$data{$l}{'reprompt'}) {
print "($item/$llen) ";
}
print "Testing ". $data{$l}{"name"}.":\n";
if($data{$l}{'warn'} && $yesall && $query eq "y") { print "WARNING: $data{$l}{'warn'}\n";}
if($query eq "A" || $query eq "a") {$query = "y";}
print results "($item/$llen) ". $data{$l}{"name"}." results:\n";
if( $data{$l}{'osfilter'} and $os =~ $data{$l}{'osfilter'})
{
print "ERROR: This language is flagged as incompatible with this operating system\n\n";
print results "ERROR: This language is flagged as incompatible with this operating system\n\n";
next;
}
$cmd="";
if( $data{$l}{"compiler0"})
{
print "Compiling";
print results "Compile";
if($l ne "java" && $l ne "m3") {$rcmd = "rm -f $data{$l}{'prog'}";}
else { $rcmd = "";}
$cmd .= "sh ";
if(!$debug) { $cmd .= "2>&1 "; }
$cmd .= "-c \"" .$TIME . " sh -c \\\"";
$cp = 0;
if(! $data{$l}{'compiler'})
{
$data{$l}{'compiler'} = 1;
}
for(; $cp < $data{$l}{'compiler'}; $cp++)
{
if($data{$l}{"compiler$cp"} eq "interpB")
{
$cp = $data{$l}{'compiler'};
$interpB = 1;
last;
}
$comp = getInterp($data{$l}{"compiler$cp"});
last if($comp ne "");
}
if($cp == $data{$l}{'compiler'}) {
$clist = "";
for($p=0;$p<$data{$l}{'compiler'};$p++)
{
last if($data{$l}{"compiler$p"} eq "interpB");
$clist .= "\t\t\t($p) ". $data{$l}{"compiler$p"}."\n";
}
print ":\tERROR - Unable to find one of the following compiler(s):\n$clist\n";
print results ":\tERROR - Unable to find one of the following compiler(s):\n$clist\n";
if(! $interpB) { next; }
}
if(!$interpB) {
$compiler = $data{$l}{"compiler$cp"};
print " [$compiler] ";
print results " [$compiler]:\t";
if(length($compiler) < 5) {print results "\t";}
if($data{$l}{"cflagspre$cp"}) { $cmd .= $data{$l}{"cflagspre$cp"};}
$cmd .= "$comp ";
if( $data{$l}{"cflags$cp"} )
{
$cmd .= $data{$l}{"cflags$cp"};
}
$cmd .= " ".$data{$l}{"src"}." ";
$cmd .= "\\\"\"";
#printf "CMD: '$cmd'\n";
parseTimes();
$rcmd = "";
print "\n";
}
}
if($interpB && $data{$l}{'warnB'})
{
print "WARNING: ".$data{$l}{'warnB'}."\n";
}
print "Executing";
print results "Execute";
$filter = "";
$interpreter = "";
$cmd = "sh ";
if(!$debug) { $cmd .= "2>&1 "; }
$cmd .= "-c \"". $TIME . " sh -c \\\"";
if( $data{$l}{"interp0"} or $interpB)
{
$ip=0;
if(! $data{$l}{'interp'}) {
$data{$l}{'interp'} = 1;
}
if($interpB) {
$interp = getInterp($data{$l}{'interpB'});
if($interp eq "") {
$ip = $data{$l}{'interp'};
} else {
$ip = "B";
}
}
else {
for(; $ip < $data{$l}{'interp'}; $ip++)
{
$interp = getInterp($data{$l}{"interp$ip"});
last if($interp ne "");
}
}
if($ip == $data{$l}{'interp'})
{
$ilist = "";
if($interpB) {
$ilist = "\t".$data{$l}{'interpB'}."\n";
} else
{
for($p=0;$p<$data{$l}{'interp'};$p++)
{
$ilist .= "\t\t\t($p) " .$data{$l}{"interp$p"}."\n";
}
}
print ":\tERROR - Unable to find one of the following interpreter(s):\n$ilist\n";
print results ":\tERROR - Unable to find one of the following interpreter(s):\n$ilist\n";
next;
}
$interpreter = $data{$l}{"interp$ip"};
print " [$interpreter]";
print results " [$interpreter]";
$filter = $data{$l}{"filter$ip"};
$cmd .= $data{$l}{"pre$ip"};
$cmd .= $data{$l}{"iflagspre$ip"};
$cmd .= "$interp ";
if(!$data{$l}{'noarg'})
{
if( $data{$l}{"iflags$ip"})
{
$cmd .= $data{$l}{"iflags$ip"}. " ";
} elsif( $data{$l}{"prog"} )
{
$cmd .= $data{$l}{"prog"}." $max ";
} else
{
if($data{$l}{"versionfinder"}) {
$version = `$data{$l}{"versionfinder"}`;
chomp($version);
$data{$l}{'src'} =~ s/#VERSION#/$version/;
}
if($version) {
if($data{$l}{"iflagspre$version"})
{
$cmd = $data{$l}{"iflagspre$version"} . $cmd;
}
}
$cmd .= $data{$l}{"src"}. " ";
$cmd .= "$max ";
}
}
} else
{
if($data{$l}{'pre'}) { $cmd .= $data{$l}{'pre'};}
$cmd .= "./" . $data{$l}{"prog"} . " $max";
$filter = $data{$l}{"filter$cp"};
}
if($filter) { $cmd .= " | $filter "; }
$cmd .= " > primes.$l.txt";
$cmd .= "\\\"\"";
print " ";
print results ":\t";
if(length($interpreter) < 5) { print results "\t";}
parseTimes();
print "\n";
print results "\n";
sleep 1;
$execd++;
}
elsif( $query eq "q" || $query eq "Q")
{
print "\n";
checkValid();
print "Quiting\n";
close(results);
exit;
}
print "\n";
}
checkValid();
close(results);

73
languages Normal file
View File

@ -0,0 +1,73 @@
LANGUAGES
* = implemented
$ = implemented but no interpreter compatible with autotest
Rankings from freashmeat late 2003
number = number of projects on freshmeat.net (my crude ranking)
C * 5631
PERL * 2802
C++ * 2545
Java * 2473
PHP * 2122
Python * 1235
Unix Shell * 565
TCL * 360
JavaScript $ 253
Assmembly * 179
Objctive C * 142
Ruby * 133
Lisp+CLisp * 98
Pascal+Delphi * 88
Scheme * 80
C# * 47
Fortran * 47
Ada * 44
Awk * 42
VBasic+Basic * 32
Haskell * 30
ML * 26
caml/OCaml * 23
SmallTalk * 21
Eiffel * 21
Forth * 17
Erlang * 11
Prolog * 8
Cobol *
Modula *
Pike *
Rexx *
lua *
REBOL *
Ferite *
APL? 3
Rankings from http://merd.net/pixel/language-study
Statistics on usenet 1999 and 2000 in 3 graphs
java *
c++ *
c *
perl *
basic *
python *
javascript *
tcl *
ruby *
pascal *
ruby *
lisp *
ada *
smalltalk *
cobol *
fortran *
forth *
scheme *
eiffel *
prolog *
ml *

34
primes.4.lua Normal file
View File

@ -0,0 +1,34 @@
-- Lua 4 version of a Prime Number Finder
-- haplo@mindstab.net
function primes(max)
cnt = 3
while cnt <= max do
sq = sqrt(cnt)
isp = 1
i = 3
while i <= sq and isp ~= 0 do
isp = mod(cnt, i)
i = i+2
end
if isp ~= 0 then
print(cnt)
end
cnt = cnt + 2
end
end
--argc = table.getn(arg)
argc = read()
max = tonumber(argc)
if max < 1 then
print("Usage: echo [Max Num] | lua primes.lua")
else
if not max then
print("Invalid Max Num")
else
primes(max)
end
end

32
primes.5.lua Normal file
View File

@ -0,0 +1,32 @@
-- Lua 5 version of a Prime Number Finder
-- haplo@mindstab.net
function primes(max)
cnt = 3
while cnt <= max do
sq = math.sqrt(cnt)
isp = 1
i = 3
while i <= sq and isp ~= 0 do
isp = math.mod(cnt, i)
i = i+2
end
if isp ~= 0 then
print(cnt)
end
cnt = cnt + 2
end
end
argc = table.getn(arg)
if argc < 1 then
print("Usage: primeslua [Max Num]")
else
max = tonumber(arg[1])
if not max then
print("Invalid Max Num")
else
primes(max)
end
end

26
primes.5c Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env nickle
# nickle version of a prime number finder
# haplo@mindstab.net
if (dim(argv) <= 1)
{
printf("Usage: primes.5c [Max]\n");
exit(1);
}
int max = atoi(argv[1]);
for ( int number = 3; number <= max; number += 2)
{
low_max = sqrt(number);
isPrime = true;
for ( int factor = 3; factor <= low_max; factor += 2)
{
if ( number % factor == 0)
{
isPrime = false;
break;
}
}
if (isPrime) printf("%d\n", number);
}

4
primes.R Normal file
View File

@ -0,0 +1,4 @@
for ( number in 3:100 by 2) {
print (number)
number <- number + 1
}

44
primes.adb Normal file
View File

@ -0,0 +1,44 @@
-- Ada version of a Prime Number Finder
-- Built with gnatmake 3.15p and gcc 3.2.3
-- haplo@mindstab.net
with Ada.Text_IO, Ada.Numerics.Elementary_Functions, Ada.Command_Line;
use Ada.Text_IO, Ada.Numerics.Elementary_Functions, Ada.Command_Line;
procedure primes is
package int_io is new integer_io(integer);
use int_io;
cnt, i, isPrime, test, MAX : integer;
begin
if argument_count >= 1 then
MAX := integer'value( argument(1) );
else
put_line("Useage: primesada [Max Num]");
return;
end if;
if MAX < 1 then
put_line("Max Num invalid");
return;
end if;
cnt := 3;
while cnt <= MAX loop
test := integer(sqrt(float(cnt)));
isPrime := 1;
i := 3;
while i <= test loop
if cnt mod i = 0 then
isPrime := 0;
exit;
end if;
i := i + 2;
end loop;
if isPrime /= 0 then
put(cnt, width=>0);
new_line(1);
end if;
cnt := cnt + 2;
end loop;
end primes;

43
primes.alt.c Normal file
View File

@ -0,0 +1,43 @@
// C version of a Prime Number Finder
// Alternate Algorithm: Instead of using sqrt function,
// track it with two variables
// haplo@mindstab.net
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
unsigned int i, x, max=2, isP, sqr=4;
unsigned int MAX_NUM;
if(argc < 2) {
printf("primesaltc <MAX NUM>\n");
return -1;
} else {
MAX_NUM = atoi(argv[1]);
}
if(MAX_NUM < 1)
{
printf("Invalid MAX_NUM specified\n");
return -1;
}
for(i=3; i<=MAX_NUM; i+=2)
{
isP=1;
for(x = 3; x<=max; x+=2)
{
if( ! (i%x)){
isP=0;
break;
}
}
if(isP) printf("%d\n", i,max, sqr);
if(i>=sqr) {
max++;
sqr = max*max -2;
}
}
}

42
primes.alt2.c Normal file
View File

@ -0,0 +1,42 @@
// C version of a Prime Number Finder
// Alternate Algorithm: Instead of using sqrt function,
// track it with two variables
// haplo@mindstab.net
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
unsigned int i, x, max=2, isP, sqr=4;
unsigned int MAX_NUM;
if(argc < 2) {
printf("primesaltc <MAX NUM>\n");
return -1;
} else {
MAX_NUM = atoi(argv[1]);
}
if(MAX_NUM < 1)
{
printf("Invalid MAX_NUM specified\n");
return -1;
}
for(i=3; i<=MAX_NUM; i+=2)
{
isP=1;
for(x = 3; x<=max; x+=2)
{
isP = i - (x* (i/x));
if(isP == 0) break;
}
if(isP) printf("%d\n", i,max, sqr);
if(i>=sqr) {
max++;
sqr = max*max -2;
}
}
}

49
primes.alt3.c Normal file
View File

@ -0,0 +1,49 @@
// C version of a Prime Number Finder
// Alternate Algorithm: Instead of using sqrt function,
// track it with two variables
// haplo@mindstab.net
#include <stdio.h>
#include <stdlib.h>
int check(int i, int cnt, int m)
{
if(i > m) return 1;
else {
if( (cnt%i)==0) return 0;
return check(i+2,cnt, m);
}
}
int primes(int c, int m, int sq, int sqm)
{
if(c > m) return 1; else
{
if(check(3,c,sq))
printf("%d\n", c);
if(c>=sqm) {
sq++;
sqm = sq*sq;
}
return primes(c+2, m, sq, sqm);
}
}
int main(int argc, char **argv)
{
unsigned int i, x, max=2, isP, sqr=4;
unsigned int MAX_NUM;
if(argc < 2) {
printf("primesaltc <MAX NUM>\n");
return -1;
} else {
MAX_NUM = atoi(argv[1]);
}
if(MAX_NUM < 1)
{
printf("Invalid MAX_NUM specified\n");
return -1;
}
primes(3, MAX_NUM, 2,4);
}

28
primes.awk Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/awk -f
# AWK version of a Prime Number Finder
# haplo@mindstab.net
{ max = $1; }
END {
if(max < 1)
{
print "primes.awk <MAX NUM>"
} else
for (i=3; i < max ; i+=2)
{
maxT = sqrt(i)+1
isPrime = 1
for (p=3; p < maxT; p+=2)
{
isPrime = i % p
if( isPrime == 0)
{
break
}
}
if(isPrime != 0) {
print i
}
}
}

41
primes.c Normal file
View File

@ -0,0 +1,41 @@
// C version of a Prime Number Finder
// haplo@mindstab.net
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char ** argv)
{
unsigned int counter, max_test, tester;
char isPrime;
unsigned int MAX_NUM;
if(argc < 2) {
printf("primesc <MAX NUM>\n");
return 1;
} else {
MAX_NUM = atoi(argv[1]);
}
if(MAX_NUM < 1)
{
printf("Invalid MAX_NUM specified\n");
return 1;
}
for(counter =3; counter<= MAX_NUM; counter +=2)
{
max_test = (int) sqrt((double) counter);
isPrime = 1;
for(tester =3; tester <= max_test; tester += 2)
{
if(! (counter % tester)) {
isPrime = 0;
break;
}
}
if(isPrime) printf("%d\n", counter);
}
return 0;
}

48
primes.cbl Normal file
View File

@ -0,0 +1,48 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. primes.
AUTHOR. Dan Ballard.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Cnt PIC S9(10) VALUE 3.
01 TMax PIC S9(10) VALUE 3.
01 I PIC S9(10) VALUE 0.
01 isp PIC S9(10) VALUE 1.
01 tmp PIC S9(10) VALUE 1.
01 break PIC 9 VALUE 0.
01 MAX PIC S9(10) VALUE 20.
01 SQT PIC S9(10) VALUE 4.
01 SQ PIC S9(10) VALUE 2.
PROCEDURE DIVISION.
Main.
ACCEPT MAX
IF MAX < 1
DISPLAY "Invalid Max Num"
EXIT
END-IF
PERFORM UNTIL Cnt > MAX
MOVE 3 TO I
MOVE 0 TO break
MOVE 1 TO isp
PERFORM UNTIL I > SQ OR break = 1
MOVE Cnt TO isp
DIVIDE Cnt BY i GIVING tmp REMAINDER isp
IF isp = 0
MOVE 1 TO break
END-IF
ADD 2 TO I
END-PERFORM
IF NOT isp = 0
DISPLAY Cnt
END-IF
ADD 2 TO Cnt
IF Cnt > SQT
ADD 1 TO SQ
MULTIPLY SQ BY SQ GIVING SQT
END-IF
END-PERFORM
STOP RUN.

61
primes.cpp Normal file
View File

@ -0,0 +1,61 @@
// C++ version of a Prime Number Finder
// haplo@mindstab.net
#include <iostream>
#include <math.h>
#include <string>
#include <stdlib.h>
using namespace std;
class CPrimes
{
public:
void countPrimes(unsigned int MAX);
};
void CPrimes::countPrimes(unsigned int MAX)
{
for(unsigned int cnt = 3; cnt <= MAX; cnt+=2)
{
int test = (int)sqrt((double)cnt);
char isPrime=1;
for(int i = 3; i<= test; i+=2)
{
if(! (cnt % i))
{
isPrime = 0;
break;
}
}
if(isPrime)
{
cout << cnt << endl;
}
}
}
int main(int argc, char **argv)
{
int MAX;
if(argc <= 1) {
cout << "Useage: primescpp [Max Num]" << endl;
return(1);
} else {
MAX = atoi(argv[1]);
}
if(MAX < 1) {
cout << "Invalide Max Num" << endl;
return(1);
}
CPrimes *primes = new CPrimes();
primes->countPrimes(MAX);
delete primes;
return(0);
}

42
primes.cs Normal file
View File

@ -0,0 +1,42 @@
// C# version of a Prime Number Finder
// Build under Mono .28
// haplo@mindstab.net
using System;
class Primes {
private static void Main(String[] argv)
{
int MAX=0;
if(argv.Length >= 1) {
try {
MAX = (int)Convert.ToDouble(argv[0]);
} catch {
Console.WriteLine("Invalid Max Num");
return;
}
} else {
Console.WriteLine("Useage: primes.exe [Max Num]");
return;
}
for(int cnt = 3; cnt <= MAX; cnt+= 2)
{
int test = (int) Math.Sqrt(cnt);
int isPrime = 1;
for(int i=3; i <= test; i+=2)
{
isPrime = cnt % i;
if( isPrime == 0) {
break;
}
}
if(isPrime != 0)
{
Console.WriteLine(cnt);
}
}
}
}

61
primes.e Normal file
View File

@ -0,0 +1,61 @@
-- Eiffel version of a Prime Number Finder
-- haplo@mindstab.net
-- Anyone know how to round off doubles? or cast them to integers please
-- email me. I should be able to speed this up then.
class PRIMES
creation make
feature
make is
local
max, i, cnt, isprime : INTEGER;
test: DOUBLE;
stop : BOOLEAN;
do
if argument_count < 1 or else not argument(1).is_integer then
io.put_string("Useage: primese [Max Num]%N");
die_with_code(exit_failure_code);
else
max := argument(1).to_integer;
end;
from
cnt := 3;
until
cnt > max
loop
test := cnt.sqrt;
isprime := 1;
stop := false;
from
i := 3;
until
i > test or stop
loop
from
isprime := cnt;
until
isprime < i
loop
isprime := isprime-i;
end;
--isprime := cnt - (i* do_round_down(cnt/i));
if isprime = 0 then
stop := true
end;
i := i + 2;
end;
if isprime /= 0 then
io.put_integer(cnt);
io.put_string("%N");
end;
cnt := cnt + 2;
end;
end;
end

23
primes.erl Normal file
View File

@ -0,0 +1,23 @@
% Erlang version of a Prime Number Finder
% haplo@mindstab.net
-module(primes).
-author('haplo@mindstab.net').
-export([primes/1]).
check(I, Cnt, Max) when I < Max, (Cnt rem I) == 0 -> 0;
check(I, Cnt, Max) when I < Max -> check(I+2, Cnt, Max);
check(I, Cnt, Max) -> 1.
primes([Max]) -> primes(3, list_to_integer(atom_to_list(Max)), 2, 4);
primes(Max) -> primes(3, Max, 2, 4).
primes(Cnt, Max, Sq, Sqm) when Cnt < Max, Cnt >= Sqm ->
primes(Cnt, Max, Sq+1, (Sq+1)*(Sq+1));
primes(Cnt, Max, Sq, Sqm) when Cnt < Max ->
case check(3, Cnt, Sq) of
0 -> 0;
C -> io:format("~p\n", [Cnt])
end,
primes(Cnt+2, Max, Sq, Sqm)
;
primes(Cnt, Max, Sq, Sqm) -> init:stop().

29
primes.f Normal file
View File

@ -0,0 +1,29 @@
c Fortran version of a Prime Number Finder
c haplo@mindstab.net
program primes
implicit none
real test, i;
integer isPrime, amax, cnt;
character *32 buff;
if (iargc() >= 1) then
call getarg(1, buff);
read(buff, *) amax;
else
print*, "Useage: primesf [Max Num]";
return
endif
do cnt = 3, amax, + 2
test = sqrt(float(cnt));
isPrime=1;
do test = 3, test, +2
if (mod(cnt, test) == 0) then
isPrime=0;
exit;
endif
enddo
if(isPrime /= 0) print*,cnt
c if(isPrime /= 0) write(*,'(I3.1)'),cnt
enddo
end

BIN
primes.fasl Normal file

Binary file not shown.

36
primes.fe Executable file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env ferite
/* Ferite version of a Prime Number Finder
* haplo@mindstab.net */
uses "array", "console", "math";
array fncArgs;
number MAX, cnt, test, i, remainder;
string foo;
MAX = 10;
foo = Console.readln();
foo = String.trim(foo, "\n");
if(String.isNumber(foo))
{
MAX = String.toNumber(foo);
} else {
Console.println("Useage: primes.fe [Max Num]\n");
}
if(MAX < 1) { Console.println("Invalid Max Num\n"); }
i = 0;
for(cnt = 3; cnt <= MAX; cnt +=2)
{
test = Math.sqrt(cnt);
remainder=1;
for(i=3; i<= test; i+=2)
{
remainder = cnt%i;
if(! remainder) break;
}
if(remainder)
Console.println(cnt);
}

38
primes.fs Normal file
View File

@ -0,0 +1,38 @@
( Forth version of a Prime Number Finder
haplo@mindstab.net )
: check ( m cnt -- b )
{ m cnt }
\ var i
-1 3 -1
begin
over m <= and while
nip
dup 2 + swap cnt swap mod tuck ( # cnt+2 # )
repeat
drop
;
: primes ( m sq sqm -- )
{ m sq sqm }
\ var i sq sqm
sqm sq 3
begin
dup m <= while
over over \ sq(max) cnt
check ( max cnt -- b )
if dup . CR then
2 +
rot ( sq cnt sqm ) over over
> if
rot ( cnt sqm sq )
1 + nip ( cnt sq )
dup dup * ( cnt sq sqm )
rot swap ( sq cnt sqm )
then
\ sqm sq 3
rot rot
repeat
drop drop drop
;

17
primes.hs Normal file
View File

@ -0,0 +1,17 @@
-- Haskell version of a Prime Number Finder
-- haplo@mindstab.net
import System
check i c m | i > m = [(show c) ++ " "]
| rem c i > 0 = check (i+2) c m
| otherwise = []
primes c m s t | c < m && c >= t = primes c m (s+1) ((s+1)*(s+1))
| c < m = check 3 c s ++ primes (c+2) m s t
| otherwise = []
main = do
x <- getArgs
print (primes 3 (read (x!!0)::Int) 2 4)

95
primes.html Normal file
View File

@ -0,0 +1,95 @@
<html>
<head>
<script language="JavaScript">
<!--
function primes(form)
{
today = new Date();
sec = today.getTime();
max = form.max.value;
form.txt.value="";
for(cnt=3; cnt<= max;cnt+=2)
{
sq = Math.round(Math.sqrt(cnt));
isprime = true;
for(i=3; i<=sq; i+=2)
{
if(cnt % i == 0)
{
isprime = false;
break;
}
}
if(isprime)
{
form.txt.value = form.txt.value + cnt + "\n";
form.last.value = ""+cnt;
delete today;
today = new Date();
t = Math.round((today.getTime()- sec)/1000);
m=0;
while(t >= 60)
{
m +=1;
t -= 60;
}
if(t<10) {
t = "" + "0"+t;
}
form.time.value = ""+m+":"+t ;
}
}
return false;
}
//-->
</script>
</head>
<body>
<font face="arial"><b>Prime Number Finder</b> in Java Script<br>
haplo@mindstab.net</font><br><br>
<i><b>Warning:</b> JavaScript prime number finding process is very slow, and locks browser while working, so don't set maximum too high.</i><br><br>
<form name="form">
<table>
<tr>
<td valign="top">
Maximum:<br>
<input name="max" value="1000" size="10"><br>
<input type="button" value="Calculate" onClick="primes(this.form)">
</td>
<td width="50">
&nbsp;
</td>
<td valign="top">
Time:<br>
<input name="time" value="0:00" size="5">
</td>
</tr>
<tr>
<td height="50">
&nbsp;
</td></tr>
<tr>
<td>
Primes:<br>
<textarea name="txt" cols="15" rows="10"></textarea>
</td>
<td width="50">
&nbsp;
</td>
<td valign="top">
Last Prime Number:<br>
<input name="last" value="0" size="15">
</td>
</tr>
</table>
</form>
</body>
</html>

35
primes.java Normal file
View File

@ -0,0 +1,35 @@
// Java version of a Prime Number Finder
// haplo@mindstab.net
class primes {
public static void main(String[] s)
{
if(s.length < 1) {
System.out.println("Useage: java primes [Max Num]\n");
System.exit(1);
}
int max = Integer.valueOf(s[0]).intValue();
if(max < 1) {
System.out.println("Invalid Max Num\n");
System.exit(1);
}
for(int i =3; i<=max; i+=2)
{
int maxTest = (int)Math.sqrt(i);
boolean isPrime = true;
for(int p = 3; p<= maxTest; p+=2)
{
if( (i%p) == 0)
{
isPrime = false;
break;
}
}
if(isPrime)
{
System.out.println(i);
}
}
}
}

18
primes.lisp Normal file
View File

@ -0,0 +1,18 @@
; Common Lisp version of a Prime Number Finder
; haplo@mindstab.net
(defun check (max i sq)
(if (> i sq)
(format t "~d~%" max)
(if (not (= (mod max i) 0))
(check max (+ i 2) sq))))
(defun _primes (max i)
(if (< i max)
(progn
(check i 3 (sqrt i))
(_primes max (+ i 2)))))
(defun primes (max)
(_primes max 3))

53
primes.m Normal file
View File

@ -0,0 +1,53 @@
// Objective C version of a Prime Number Finder
// haplo@mindstab.net
#include <objc/Object.h>
#include "math.h"
@interface Primes: Object
{
}
- print:(unsigned int)MAX;
@end
@implementation Primes
- print:(unsigned int)MAX
{
unsigned int cnt, test, i;
char isPrime;
for( cnt = 3; cnt <= MAX; cnt +=2)
{
test = (unsigned int)sqrt(cnt);
isPrime = 1;
for(i=3; i<= test; i+=2)
{
if( !(cnt%i))
{
isPrime=0;
break;
}
}
if(isPrime) {
printf("%d\n", cnt);
}
}
}
@end
int main(int argc, char **argv)
{
unsigned int max;
Primes *primes;
if(argc <= 1) {
printf("Useage: primesobjc [Max Num]\n");
return(1);
}
max = atoi(argv[1]);
if(max<1) {
printf("Invalid Max Num\n");
return(1);
}
primes = [Primes alloc];
[primes print:max];
}

9
primes.m4 Normal file
View File

@ -0,0 +1,9 @@
dnl M4 version of a prime number finder
dnl haplo@mindstab.net
dnl
dnl ARGS: CNT, I, MAX
define(CHECK, `ifelse(eval($2>$3),1,1, eval($1 % $2),0,0, `CHECK($1,eval($2+2),$3)')')dnl
dnl ARGS: CNT MAX SQ SQTOP
define(PRIMES,`ifelse(eval($1 < $2),1, `ifelse(eval($1>$4),1, `PRIMES($1,$2,eval($3+1),eval(($3+1)**2))', `ifelse(eval(CHECK($1,3,$3) != 0), 1, $1)
PRIMES(eval($1+2),$2,$3,$4)')')')dnl
dnl autotest call will be inserted here via sed

31
primes.ml Normal file
View File

@ -0,0 +1,31 @@
(* OCaml version of a prime number finder
haplo@mindstab.net *)
let primes max =
let sq = ref 2 in
let sqm = ref 4 in
let cnt = ref 3 in
let m = ref 0 in
while !cnt <= !max do
let i = ref 3 in
let stop = ref false in
m := 1;
while !i <= !sq && not !stop do
m := (!cnt mod !i);
if !m = 0 then
stop := true;
i := !i + 2
done;
if not (!m = 0) then
(print_int(!cnt);
print_newline());
if !cnt >= !sqm then
(sq := !sq+1; sqm := !sq * !sq);
cnt := !cnt + 2
done;;
let max = ref (int_of_string Sys.argv.(1)) in
if !max < 1 then (print_string("Invalid Max Num\n"); exit(1););
primes(max);

47
primes.pas Normal file
View File

@ -0,0 +1,47 @@
{ Pascal version of a Prime Number Finder
Built with fpc (Free Pascal) www.freepascal.org
haplo@mindstab.net }
program primes;
var isPrime : Integer;
var test : Real;
var MAX, cnt, i : LongInt;
begin
if ARGC <= 1 then begin
WriteLn('Useage: primespas [Max Num]');
exit;
end;
{MAX := Str2Int(ARGV[1]);}
Val(ARGV[1], MAX, cnt);
if MAX < 1 then begin
WriteLn('Invalid Max Num');
exit;
end;
cnt := 3;
while cnt <= MAX do
begin
test := sqrt(cnt);
isPrime := 1;
i := 3;
while i <= test do
begin
if (cnt mod i) = 0 then begin
isPrime := 0;
break;
end;
i := i + 2;
end;
if not (isPrime = 0) then
begin
WriteLn(cnt);
end;
cnt := cnt +2;
end;
end.

31
primes.perl.pl Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/perl
# PERL version of a Prime Number Finder
# haplo@mindstab.net
if(@ARGV > 0)
{
$max = $ARGV[0];
} else
{
print "primes.pl <MAX NUM>\n";
exit;
}
if($max < 1)
{
print "Invalid MAX NUM\n";
exit;
}
for($counter =3; $counter < $max; $counter +=2)
{
$max_test = sqrt($counter);
$isPrime = 1;
for($tester=3; $tester <= $max_test; $tester+=2) {
if(!($counter % $tester)) {
$isPrime = 0;
last;
}
}
if($isPrime == 1) {print "$counter\n";}
}

25
primes.php Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env php
<?php
# PHP version of a Prime Number Finder
# haplo@mindstab.net
$MAX = $argv[1];
if($MAX < 1)
{
print "primes.php <MAX NUM>\n";
exit;
}
for($num=3; $num<= $MAX; $num+=2)
{
$max = sqrt($num);
$isPrime = true;
for($test=3;$test <= $max; $test+=2)
{
if(! ($num % $test)) {
$isPrime= false;
break;
}
}
if($isPrime) print "$num\n";
}
?>

38
primes.pike Normal file
View File

@ -0,0 +1,38 @@
// Pike version of a Prime Number Finder
// haplo@mindstab.net
int main(int argc, array(string) argv)
{
int max;
if(argc >= 2) {
max = (int)argv[1];
} else {
write("USEAGE: primes.pike [ Max Num ]");
return 0;
}
if(max < 2)
{
write("Invalid Max Num\n");
return 0;
}
for(int cnt=3;cnt<=max; cnt+=2)
{
int sq = sqrt(cnt);
int isp = 1;
for(int i = 3; i<=sq;i+=2)
{
if(cnt % i == 0)
{
isp = 0;
break;
}
}
if(isp) {
write(cnt + "\n");
}
}
return 0;
}

34
primes.prolog.pl Normal file
View File

@ -0,0 +1,34 @@
% prolog
check(I,C,M,P) :-
P \= 0,
I > M,
write(C), nl.
check(I,C,M,P) :-
I =< M,
P \= 0,
NP is C mod I,
NI is I + 2,
check(NI,C,M,NP).
check(I,C,M,0).
primes(M) :-
nl, primes(3,M,2,4).
primes(C,M,S,T) :-
C =< M,
C > T,
NS is S + 1,
NT is NS * NS,
check(3,C,NS,1),
NC is C + 2,
primes(NC,M,NS,NT).
primes(C,M,S,T) :-
C =< M,
C =< T,
check(3,C,S,1),
NC is C + 2,
primes(NC,M,S,T).

27
primes.py Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env python
# Python version of a Prime Number Finder
# haplo@mindstab.net
import sys
import math
try: max = int(sys.argv[1]);
except:
print "primes.py <MAX NUM>"
sys.exit()
if max < 1:
print "Invalide MAX NUM"
sys.exit()
for cnt in range(3, max, 2) :
testMax = math.sqrt(cnt)+1
isPrime = 1
for test in range(3, testMax, 2):
isPrime = cnt % test
if isPrime == 0:
break
if isPrime != 0 :
print cnt

28
primes.r Normal file
View File

@ -0,0 +1,28 @@
REBOL [
Title: "Rebol Prime Number Finder"
Date: 02-Dec-2003
Author: haplo@mindstab.net
Version: 1.0
File: %primes.r
Home: http://www.mindstab.net/primes
]
; Command line argument getting code borrowed from
; http://www.melbpc.org.au/pcupdate/2305/2305article6.htm
cl_args: make block! system/script/args
max: either none? cl_args/1 [""] [to-integer cl_args/1]
for cnt 3 max +2 [
sq: to-integer(square-root(cnt))
isp: 1
for i 3 sq +2 [
isp: cnt // i
if isp = 0 [
break
]
]
if isp <> 0 [ print cnt ]
]

36
primes.rb Executable file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env ruby
# Ruby version of a Prime Number Finder
# haplo@mindstab.net
include Math
class Primes
def findPrimes(cmax)
3.step(cmax,2) { |i|
max = sqrt(i)
isPrime = 1
3.step(max, 2) { |x|
isPrime = i % x
if isPrime == 0
break
end
}
if isPrime != 0
print i, "\n"
end
}
end
end
MAX = ARGV[0].to_i;
if ! MAX
print "primes.rb <MAX NUM>\n"
exit(1)
elsif MAX < 1
print "Invalid MAX Input\n"
exit(1)
end
prime = Primes.new();
prime.findPrimes(MAX);

26
primes.rexx Normal file
View File

@ -0,0 +1,26 @@
/* REXX version of a Prime Number Finder
haplo@mindstab.net */
max=arg(1)
sq=2
sqm=4
do cnt = 3 to max by +2
isp = 1
i = 3
do while i <= sq & isp = 1
if cnt // i = 0 then
do
isp = 0
end
i = i + 2
end
if isp \= 0 then
say cnt
if cnt >= sqm then
do
sq = sq+1
sqm = sq*sq
end
end

26
primes.scm Normal file
View File

@ -0,0 +1,26 @@
;; Scheme version of a Prime Number Finder
;; haplo@mindstab.net
(define (check cnt i MAX)
(if (> i MAX)
1
(if (= (modulo cnt i) 0)
0
(check cnt (+ i 2) MAX)
)
)
)
(define (primes cnt MAX)
(if (<= cnt MAX)
(and
(if (not (= (check cnt 3 (sqrt cnt)) 0))
(and (display cnt) (display "\n"))
)
(primes (+ cnt 2) MAX)
)
)
)

42
primes.sh Executable file
View File

@ -0,0 +1,42 @@
#!/bin/sh
# Unix Shell version of a Prime Number Finder
# haplo@mindstab.net
odd=3
sqr=4
max=2
i=3
if [ ! $1 ] ; then
echo "primes.sh [Max number]"
exit
fi
MAX=$1
if [ $MAX -le 0 ] ; then
echo "Max must be a valid number"
exit
fi
while [ $i -lt $MAX ] ;
do
isP=1
x=3
while [ $x -le $max -a $isP != 0 ] ;
do
isP=`expr $i % $x`
x=`expr $x + 2`
done
if [ $isP != 0 ] ;
then
echo "$i"
fi
if [ $i -ge $sqr ] ;
then
max=`expr $max + 1`
odd=`expr $odd + 2`
sqr=`expr $odd + $sqr - 2`
fi
i=`expr $i + 2`;
done

72
primes.sieve.c Normal file
View File

@ -0,0 +1,72 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int *SieveArray;
void InitSieve(unsigned int Max)
{
unsigned int i;
for (i=0; i <= Max; i++)
{
SieveArray[i] = 1;
}
}
void Sieve(unsigned int Max)
{
unsigned int i,j;
unsigned int low_max = sqrt(Max);
for( i = 2; i <= low_max; i++)
{
for ( j = i*2; j <= Max; j += i)
{
SieveArray[j] = 0;
}
}
}
void ShowResults(unsigned int Max)
{
unsigned int i;
for (i =3; i <= Max; i++)
{
if (SieveArray[i])
{
printf("%d\n", i);
}
}
}
int main(int argc ,char **argv)
{
unsigned int Max;
if(argc < 2) {
printf("primes.sieve.c <Max>\n");
return 1;
} else {
Max = atoi(argv[1]);
}
if(Max < 1)
{
printf("Invalid Max specified\n");
return 1;
}
SieveArray = (int*) malloc(sizeof(int)*Max);
if(!SieveArray)
{
printf("Error: could not allocate SieveArray at Max:%d\n", Max);
return 1;
}
InitSieve(Max);
Sieve(Max);
ShowResults(Max);
free(SieveArray);
return 0;
}

30
primes.sml Normal file
View File

@ -0,0 +1,30 @@
(* Standard ML version of a Prime Number Finder
haplo@mindstab.net *)
fun check i cnt max = if i > max
then
1
else
if cnt mod i = 0 then
0
else
check (i+2) cnt max
fun ploop cnt max sq sqm =
if cnt > max then
print ""
else
if cnt >= sqm then
ploop cnt max (sq+1) ((sq+1)*(sq+1))
else
(if ((check 3 cnt sq) <> 0) then
(print (Int.toString(cnt));
print("\n") )
else
print ""
; ploop (cnt+2) max sq sqm)
fun primes max =
(print "\n"; ploop 3 max 2 4);
(* autotest call will be inserted here via sed *)

29
primes.st Normal file
View File

@ -0,0 +1,29 @@
" Smalltalk version of a prime number finder
<haplo@mindstab.net>"
| Max |
Smalltalk arguments size < 1
ifTrue: [
'Usage: primes.st [Max]' printNl.
]
ifFalse: [
Max := (Smalltalk arguments at: 1) asInteger.
3 to: Max by: 2 do: [ :number |
LowMax := (number sqrt) rounded.
IsPrime := true.
3 to: LowMax by: 2 do: [ :factor |
((number rem: factor) == 0)
ifTrue: [
IsPrime := false
]
].
IsPrime
ifTrue: [
number printNl
]
]
].
!

29
primes.tcl Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env tclsh
# TCL version of a Prime Number Finder
# haplo@mindstab.net
if { $argc == 0 } {
puts "Useage: primes.tcl \[Max Num\]"
exit
}
set MAX [lindex $argv 0]
if { $MAX < 1 } {
puts "Max Num must be greater than 1"
exit
}
for { set cnt 3} {$cnt <= $MAX} {incr cnt 2} {
set max_test [expr sqrt($cnt)]
set isPrime 1
for {set test 3} {$test <= $max_test} {incr test 2} {
if { [expr $cnt % $test] == 0 } {
set isPrime 0;
break
}
}
if { $isPrime == 1 } {
puts $cnt
}
}

31
primes.vb Normal file
View File

@ -0,0 +1,31 @@
' Visual Basic version of a Prime Number Finder
' haplo@mindstab.net
Imports System
Imports Math
Module Prime
Sub Main(ByVal CmdArgs() As String)
Dim cnt As Long, max As Long
Dim test As Double, isPrime As Byte, i as Long
If CmdArgs.Length < 1 then
Console.WriteLine("Usage: primesvb [Max Num]")
Return
Else
max = Convert.ToInt32(CmdArgs(0))
End If
For cnt = 3 to max Step +2
test = Math.Sqrt(cnt)
isPrime = 1
For i = 3 to test Step +2
If (cnt mod i) = 0 then
isPrime = 0
Exit For
End If
Next
If isPrime <> 0 Then
Console.WriteLine(cnt)
End If
Next
End Sub
End Module

109
primes.x86.s Normal file
View File

@ -0,0 +1,109 @@
/* x86 Assembly (AT&T style) version of a Prime Number Finder
haplo@mindstab.net */
.data
numstr: .asciz "%d\n"
max = 100000
scanformat : .asciz "%d"
int = 0
.text
.extern printf
.extern scanf
.extern exit
.global main
printnum:
pushl %ebx
pushl $numstr
call printf
pop %eax
pop %ecx
ret
main:
# scanf code from http://www.linuxgazette.com/issue71/joshi.html
pushl %ebp # save EBP on stack
movl %esp,%ebp # EBP = ESP
subl $4,%esp # create space for x on the stack
leal -4(%ebp),%eax
pushl %eax
pushl $scanformat
call scanf
addl $12, %esp
movl -4(%ebp),%eax
movl %eax, -12(%ebp)
# END scanf code
# The rest is all my code
pushl %eax
movl $3, %ebx
movl $2, %ecx
movl $4, %edx
toploop:
popl %eax
cmp %eax, %ebx
jg end
pushl %eax
pushl %edx
pushl %ecx
pushl %ebx
movl $3, %ecx
checkloop:
popl %ebx
popl %edx
cmp %edx, %ecx
pushl %edx
pushl %ebx
jge showprime
movl $0, %edx
movl %ebx, %eax
divl %ecx
addl $2, %ecx
cmp $0, %edx
je doneloop
jmp checkloop
showprime:
call printnum
doneloop:
popl %ebx
popl %ecx
popl %edx
addl $2, %ebx
cmp %edx, %ebx
jl toploop
addl $1, %ecx
movl %ecx, %eax
mull %eax
movl %eax, %edx
jmp toploop
end:
pushl $0
call exit

28
primes_prolog.pl Normal file
View File

@ -0,0 +1,28 @@
% Prolog version of a Prime Number Finder
% haplo@mindstab.net
check(I,C,M,P) :-
P \= 0,
I > M.
check(I,C,M,P) :-
P \= 0,
NP is C mod I,
NI is I + 2,
check(NI,C,M,NP).
primes(M) :-
nl, primes(3,M,2).
primes(C,M,S) :- C > M.
primes(C,M,S) :-
check(3,C,S,1),
write(C), nl,
NC is C + 2,
NS is sqrt(NC),
primes(NC,M,NS).
primes(C,M,S) :-
NC is C + 2,
NS is sqrt(NC),
primes(NC,M,NS).

BIN
primesoc Executable file

Binary file not shown.

45
src/Main.m3 Normal file
View File

@ -0,0 +1,45 @@
(* Modula-3 version of a Prime Number Finder
haplo@mindstab.net *)
MODULE Main;
IMPORT IO;
IMPORT Math;
IMPORT Params;
IMPORT Scan;
PROCEDURE Main() =
VAR i, cnt, max, sq, isp : INTEGER;
BEGIN
IF Params.Count < 2 THEN
IO.Put("Useage: primesm3 [Max Num]\n");
RETURN
END;
max := Scan.Int(Params.Get(Params.Count-1));
FOR cnt := 3 TO max BY +2 DO
sq := ROUND(Math.sqrt(FLOAT(cnt, LONGREAL)));
isp := 1;
FOR i := 3 TO sq BY +2 DO
IF cnt MOD i = 0 THEN
isp := 0;
EXIT;
END;
END;
IF isp = 1 THEN
IO.PutInt(cnt);
IO.Put("\n");
END;
END;
END Main;
BEGIN
Main();
END Main.

5
src/m3makefile Normal file
View File

@ -0,0 +1,5 @@
% Makefile for Modula-3 program 'primesm3'
import("libm3")
implementation("Main")
program("primesm3")