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+

This commit is contained in:
Dan Ballard 2020-06-28 11:11:30 -07:00
parent dc033b5bd3
commit 83a6f8573f
5 changed files with 92 additions and 21 deletions

View File

@ -3,4 +3,4 @@ default:
./autoTest.pl ./autoTest.pl
clean: 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

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", "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; $llen = $#langs+1;
@ -123,6 +123,12 @@ $data{'go'}{'compiler0'} = 'go';
$data{'go'}{'cflags0'} = 'build -o primesgo '; $data{'go'}{'cflags0'} = 'build -o primesgo ';
$data{'go'}{'prog'} = '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'}{'name'} = "SML";
$data{'sml'}{'src'} = "primes.sml"; $data{'sml'}{'src'} = "primes.sml";
#$data{'sml'}{'iflagspre0'} = "echo primes $max | "; #$data{'sml'}{'iflagspre0'} = "echo primes $max | ";
@ -296,13 +302,13 @@ $data{'fe'}{'prog'} = "primes.fe";
$data{'fe'}{'interp0'} = "ferite"; $data{'fe'}{'interp0'} = "ferite";
$data{'fe'}{'pre0'} = "echo $max | "; $data{'fe'}{'pre0'} = "echo $max | ";
$data{'rb1.8'}{'name'} = "Ruby 1.8"; #$data{'rb1.8'}{'name'} = "Ruby 1.8";
$data{'rb1.8'}{'prog'} = "primes.rb"; #$data{'rb1.8'}{'prog'} = "primes.rb";
$data{'rb1.8'}{'interp0'} = "ruby1.8"; #$data{'rb1.8'}{'interp0'} = "ruby1.8";
$data{'rb1.9'}{'name'} = "Ruby 1.9.1"; $data{'rb'}{'name'} = "Ruby";
$data{'rb1.9'}{'prog'} = "primes.rb"; $data{'rb'}{'prog'} = "primes.rb";
$data{'rb1.9'}{'interp0'} = "ruby1.9.1"; $data{'rb'}{'interp0'} = "ruby";
$data{'java'}{'name'} = "Java"; $data{'java'}{'name'} = "Java";
$data{'java'}{'src'} = "primes.java"; $data{'java'}{'src'} = "primes.java";

View File

@ -2,11 +2,11 @@ package main
// Go version of a prime number finder // Go version of a prime number finder
import( import (
"fmt" "fmt"
"math"
"os" "os"
"strconv" "strconv"
"math"
) )
// In stead of in place printing of each prime, it seems // In stead of in place printing of each prime, it seems
@ -18,19 +18,20 @@ func main() {
fmt.Println("Usage: go run primes.go [MAX]") fmt.Println("Usage: go run primes.go [MAX]")
return return
} }
primes := make([]int, 0) //primes := make([]int, 0)
max,_ := strconv.Atoi(os.Args[1]) max, _ := strconv.Atoi(os.Args[1])
for i:= 3; i < max; i+=2 { for i := 3; i < max; i += 2 {
is_prime := true isPrime := true
for j := 3; j <= int(math.Ceil(math.Sqrt(float64(i)))); j+=2 { for j := 3; j <= int(math.Ceil(math.Sqrt(float64(i)))); j += 2 {
if i % j == 0 { if i%j == 0 {
is_prime = false isPrime = false
break break
} }
} }
if is_prime { if isPrime {
primes = append(primes, i) //primes = append(primes, i)
fmt.Println(i)
} }
} }
fmt.Println(primes) //fmt.Println(primes)
} }

36
primes.go.array Normal file
View File

@ -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 Normal file
View File

@ -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;
}
}