Go Euclid!
This commit is contained in:
parent
d231791a19
commit
f3712555d5
20
algs4/Makefile
Normal file
20
algs4/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
CLEAN_GOPATH := $(shell echo $(GOPATH) | tr ":" "\n" | grep -v '^$$' | grep -v $(PWD) | tr "\n" ":")
|
||||
GOPATH := $(PWD):$(CLEAN_GOPATH)
|
||||
PACKAGES := meatballhat.com/algs4-gcd
|
||||
|
||||
test: build
|
||||
go test $(PACKAGES)
|
||||
|
||||
build: deps
|
||||
go install $(PACKAGES)
|
||||
|
||||
fmt:
|
||||
go fmt $(PACKAGES)
|
||||
|
||||
deps:
|
||||
go get $(PACKAGES)
|
||||
|
||||
clean:
|
||||
rm -v bin/*
|
||||
|
||||
.PHONY: test build clean fmt
|
38
algs4/src/meatballhat.com/algs4-gcd/main.go
Normal file
38
algs4/src/meatballhat.com/algs4-gcd/main.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const USAGE string = `Usage: algs4-gcd <int> <int>`
|
||||
|
||||
func gcd(p, q uint64) uint64 {
|
||||
if q == 0 {
|
||||
return p
|
||||
}
|
||||
r := p % q
|
||||
return gcd(q, r)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 3 {
|
||||
fmt.Println(USAGE)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
p, err := strconv.ParseUint(os.Args[1], 10, 0)
|
||||
if err != nil {
|
||||
fmt.Println("Wherps: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
q, err := strconv.ParseUint(os.Args[2], 10, 0)
|
||||
if err != nil {
|
||||
fmt.Println("Wherps: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(gcd(p, q))
|
||||
}
|
Loading…
Reference in New Issue
Block a user