added scala support

This commit is contained in:
Dan Ballard 2013-02-23 09:36:15 -08:00
parent 06496457bb
commit f4b87f53ac
2 changed files with 35 additions and 1 deletions

View File

@ -15,7 +15,7 @@ $debug=0;
@langs = ("c", @langs = ("c",
# Uncomment for alternat C algorithms # Uncomment for alternat C algorithms
# "c2", "ca", "ca2", "ca3", # "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"); "objc", "cpp", "asm", "for", "pas", "ada", "hs", "e", "cbl", "oml", "java", "scala", "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; $llen = $#langs+1;
@ -308,6 +308,12 @@ $data{'java'}{'osreprompt'} = "OpenBSD";
$data{'java'}{'oswarn'} = "OpenBSD"; $data{'java'}{'oswarn'} = "OpenBSD";
$data{'java'}{'oswarnmsg'} = "This language has been flagged as unstable on this operating system"; $data{'java'}{'oswarnmsg'} = "This language has been flagged as unstable on this operating system";
$data{'scala'}{'name'} = "Scala";
$data{'scala'}{'src'} = "primes.scala";
$data{'scala'}{'compiler0'} = "scalac";
$data{'scala'}{'interp0'} = "scala";
$data{'scala'}{'prog'} = "Primes";
$data{'cs'}{'name'} = "C# (Mono)"; $data{'cs'}{'name'} = "C# (Mono)";
$data{'cs'}{'src'} = "primes.cs"; $data{'cs'}{'src'} = "primes.cs";
$data{'cs'}{'compiler0'} = "mcs"; $data{'cs'}{'compiler0'} = "mcs";

28
primes.scala Normal file
View File

@ -0,0 +1,28 @@
// Scala primes
// This to my mind may not be the fastest algorithm,
// I would think printing when finding would be better,
// especially on memory, then storing the thing in a list
// and then running mkString on it, but it already
// outperforms java for me, so here it is
object Primes {
def main(args: Array[String]) {
if (args.length < 1) {
println("Usage: scala Primes [MAX]")
} else print(primes(3, List(), args(0).toInt).mkString("\n") + "\n")
}
def is_prime(num: Int): Boolean = {
def prime_loop(i: Int, max: Int): Boolean =
if (i > max) true
else if (num % i == 0) false
else prime_loop(i+2, max)
prime_loop(3, scala.math.sqrt(num).ceil.toInt)
}
def primes(i: Int, acc: List[Int], max: Int): List[Int] =
if (i >= max) acc
else if (is_prime(i)) primes(i+2, acc ++ List(i), max)
else primes(i+2, acc, max)
}