From 6dc292bc10d077ddf7f739f046c675877c7d70a8 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 15 Jan 2014 10:53:35 -0500 Subject: [PATCH] adding go --- Makefile | 2 +- README | 4 +++- autoTest.pl | 8 +++++++- primes.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 primes.go diff --git a/Makefile b/Makefile index 1ac776f..765f9c0 100644 --- a/Makefile +++ b/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 + 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 diff --git a/README b/README index 709b02b..cdc3624 100644 --- a/README +++ b/README @@ -2,9 +2,11 @@ http://www.mindstab.net/tag/primes/ haplo@mindstab.net Mindstab.net Multi language prime number project: - Prime number finders in ~39 languages + Prime number finders in ~40 languages and a test driver +2014: added go + 2013: Cleanedup and small additions Cleaned up code to work with newer versions of compilers and interpreters and added a few new languages diff --git a/autoTest.pl b/autoTest.pl index c377e11..15dadc2 100755 --- a/autoTest.pl +++ b/autoTest.pl @@ -15,7 +15,7 @@ $debug=0; @langs = ("c", # Uncomment for alternat C algorithms # "c2", "ca", "ca2", "ca3", - "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"); + "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"); $llen = $#langs+1; @@ -117,6 +117,12 @@ $data{'awk'}{'pre0'} = "echo $max | "; $data{'awk'}{'interp0'} = "awk"; $data{'awk'}{'iflags0'} = "-f " . $data{'awk'}{'src'}; +$data{'go'}{'name'} = "Go"; +$data{'go'}{'src'} = "primes.go"; +$data{'go'}{'compiler0'} = 'go'; +$data{'go'}{'cflags0'} = 'build -o primesgo '; +$data{'go'}{'prog'} = 'primesgo'; + $data{'sml'}{'name'} = "SML"; $data{'sml'}{'src'} = "primes.sml"; #$data{'sml'}{'iflagspre0'} = "echo primes $max | "; diff --git a/primes.go b/primes.go new file mode 100644 index 0000000..cc56849 --- /dev/null +++ b/primes.go @@ -0,0 +1,30 @@ +package main + +// Go version of a prime number finder + +import( + "fmt" + "os" + "strconv" + "math" +) + +func main() { + if len(os.Args) < 2 { + fmt.Println("Usage: go run primes.go [MAX]") + return + } + 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 + break + } + } + if is_prime { + fmt.Println(i) + } + } +}