Browse Source

add rust!; make go not 'cheat' with array based method and print each number (save array base impl tho); update autotest wrt Ruby, now all common ruby is 2+

master
Dan Ballard 2 years ago
parent
commit
83a6f8573f
  1. 2
      Makefile
  2. 20
      autoTest.pl
  3. 27
      primes.go
  4. 36
      primes.go.array
  5. 28
      primes.rs

2
Makefile

@ -3,4 +3,4 @@ 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 primesgo
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 primesgo primesrs

20
autoTest.pl

@ -15,7 +15,7 @@ $debug=0;
@langs = ("c",
# Uncomment for alternat C algorithms
# "c2", "ca", "ca2", "ca3",
"objc", "cpp", "asm", "for", "pas", "go", "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");
"objc", "cpp", "asm", "for", "pas", "go", "rs", "ada", "hs", "e", "cbl", "oml", "java", "scala", "cs", "vb", "erl", "awk", "pl", "php", "py", "tcl", "rb", "cl", "scm", "st", "sml", "4th", "pro", "m3", "pike", "lua", "rexx", "r", "fe", "5c", "m4", "sh");
$llen = $#langs+1;
@ -123,6 +123,12 @@ $data{'go'}{'compiler0'} = 'go';
$data{'go'}{'cflags0'} = 'build -o primesgo ';
$data{'go'}{'prog'} = 'primesgo';
$data{'rs'}{'name'} = "Rust";
$data{'rs'}{'src'} = "primes.rs";
$data{'rs'}{'compiler0'} = 'rustc';
$data{'rs'}{'cflags0'} = ' -o primesrs ';
$data{'rs'}{'prog'} = 'primesrs';
$data{'sml'}{'name'} = "SML";
$data{'sml'}{'src'} = "primes.sml";
#$data{'sml'}{'iflagspre0'} = "echo primes $max | ";
@ -296,13 +302,13 @@ $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.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{'rb'}{'name'} = "Ruby";
$data{'rb'}{'prog'} = "primes.rb";
$data{'rb'}{'interp0'} = "ruby";
$data{'java'}{'name'} = "Java";
$data{'java'}{'src'} = "primes.java";

27
primes.go

@ -2,14 +2,14 @@ package main
// Go version of a prime number finder
import(
import (
"fmt"
"math"
"os"
"strconv"
"math"
)
// In stead of in place printing of each prime, it seems
// In stead of in place printing of each prime, it seems
// fmt.Println is costly, and causes the program to take
// 7x more time, so we gain speed at the expense of RAM
// by storing them all in a slice and printing once
@ -18,19 +18,20 @@ func main() {
fmt.Println("Usage: go run primes.go [MAX]")
return
}
primes := make([]int, 0)
max,_ := strconv.Atoi(os.Args[1])
for i:= 3; i < max; i+=2 {
is_prime := true
for j := 3; j <= int(math.Ceil(math.Sqrt(float64(i)))); j+=2 {
if i % j == 0 {
is_prime = false
//primes := make([]int, 0)
max, _ := strconv.Atoi(os.Args[1])
for i := 3; i < max; i += 2 {
isPrime := true
for j := 3; j <= int(math.Ceil(math.Sqrt(float64(i)))); j += 2 {
if i%j == 0 {
isPrime = false
break
}
}
if is_prime {
primes = append(primes, i)
if isPrime {
//primes = append(primes, i)
fmt.Println(i)
}
}
fmt.Println(primes)
//fmt.Println(primes)
}

36
primes.go.array

@ -0,0 +1,36 @@
package main
// Go version of a prime number finder
import (
"fmt"
"math"
"os"
"strconv"
)
// In stead of in place printing of each prime, it seems
// fmt.Println is costly, and causes the program to take
// 7x more time, so we gain speed at the expense of RAM
// by storing them all in a slice and printing once
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run primes.go [MAX]")
return
}
primes := make([]int, 0)
max, _ := strconv.Atoi(os.Args[1])
for i := 3; i < max; i += 2 {
isPrime := true
for j := 3; j <= int(math.Ceil(math.Sqrt(float64(i)))); j += 2 {
if i%j == 0 {
isPrime = false
break
}
}
if isPrime {
primes = append(primes, i)
}
}
fmt.Println(primes)
}

28
primes.rs

@ -0,0 +1,28 @@
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
println!("Usage: ./primes $MAX");
return;
}
let max: u64 = args[1].parse().expect("MAX must be an integer value");
let mut i: u64 = 3;
while i <= max {
let loop_max: u64 = (i as f64).sqrt() as u64;
let mut x: u64 = 2;
let mut prime = true;
while x <= loop_max {
if i % x == 0 {
prime = false;
break;
}
x += 1;
}
if prime {
println!("{}", i)
}
i += 2;
}
}
Loading…
Cancel
Save