Commits

Miki Tebeka committed c5d38f3

go1 and one

Comments (0)

Files changed (31)

 syntax: glob
 
-go/[0-9]+$
+go/src/euler/euler
 haskell/*.hi
 csharp/*.exe
 

go/src/euler/1.go

 
 import "fmt"
 
-func main() {
+func e1() {
 	sum := 0
 	for i := 1; i < 1000; i++ {
 		if (i%5 == 0) || (i%3 == 0) {
 
 	fmt.Println(sum)
 }
+
+func init() {
+    register("1", e1)
+}

go/src/euler/10.go

 	"math/big"
 )
 
-func main() {
+func e10() {
 	sum := int64(0)
 	for i := int64(0); i < 2000000; i++ {
-		if big.ProbablyPrime(big.NewInt(i), 10) {
+		if big.NewInt(i).ProbablyPrime(10) {
 			sum += i
 		}
 	}
 	fmt.Println(sum)
 }
+
+func init() {
+    register("10", e10)
+}
+
+func init() {
+    register("10", e10)
+}

go/src/euler/11.go

 	{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48},
 }
 
-func main() {
+func e11() {
 	max := 0
 	// horizontal
 	for row := 0; row < 20; row++ {
 
 	fmt.Println(max)
 }
+
+func init() {
+    register("11", e11)
+}

go/src/euler/12.go

 
 import "fmt"
 
-func triangles() chan uint {
+func triangleNums() chan uint {
 	out := make(chan uint)
 	go func() {
 		t, i := uint(0), uint(1)
 	NUM_WORKERS = 1000
 )
 
-func main() {
+func e12() {
 	out := make(chan uint)
-	in := triangles()
+	in := triangleNums()
 	for i := 0; i < NUM_WORKERS; i++ {
 		go worker(in, out)
 	}
 	t := <-out
 	fmt.Println(t)
 }
+
+func init() {
+    register("12", e12)
+}

go/src/euler/13.go

 package main
 
 import (
-	"big"
 	"bufio"
 	"fmt"
+	"io"
+	"math/big"
 	"os"
 )
 
 	go func() {
 		for {
 			line, _, err := reader.ReadLine()
-			if err == os.EOF {
+			if err == io.EOF {
 				close(out)
 				break
 			} else if err != nil {
 	return out
 }
 
-func main() {
+func e13() {
 	sum := big.NewInt(0)
 	for i := range numbers("13-digits.txt") {
 		sum.Add(sum, i)
 
 	fmt.Println(sum.String()[:10])
 }
+
+func init() {
+    register("13", e13)
+}

go/src/euler/14.go

 package main
 
 import (
-	"big"
 	"fmt"
+	"math/big"
 )
 
 var one *big.Int = big.NewInt(1)
 	return -1
 }
 
-func main() {
+func e14() {
 	n, value := 1, 1
 
 	for i := 2; i < 1000000; i++ {
 
 	fmt.Println(n)
 }
+
+func init() {
+    register("14", e14)
+}

go/src/euler/15.go

 	return np
 }
 
-func main() {
+func e15() {
 	_cache = make(map[string]uint64)
 	fmt.Println(numPaths(&Point{0, 0}))
 }
+
+func init() {
+    register("15", e15)
+}

go/src/euler/16.go

 package main
 
 import (
-	"big"
 	"fmt"
+	"math/big"
 )
 
 func sumDigits(s string) int {
 	sum := 0
 	for _, c := range s {
-		sum += c - '0'
+		sum += int(c) - '0'
 	}
 
 	return sum
 }
 
-func main() {
+func e16() {
 	i := big.NewInt(2)
 	i.Exp(big.NewInt(2), big.NewInt(1000), nil)
 	fmt.Println(sumDigits(i.String()))
 }
+
+func init() {
+    register("16", e16)
+}

go/src/euler/17.go

 
 import "fmt"
 
-var numbers = map[int]string{
+var num2eng = map[int]string{
 	1:  "one",
 	2:  "two",
 	3:  "three",
 
 func english(n int) string {
 	if n >= 100 {
-		hundreds := numbers[n/100] + "hundred"
+		hundreds := num2eng[n/100] + "hundred"
 		tens := english(n - n/100*100)
 		if len(tens) > 0 {
 			return hundreds + "and" + tens
 			return hundreds
 		}
 	}
-	if value, ok := numbers[n]; ok {
+	if value, ok := num2eng[n]; ok {
 		return value
 	}
 
 	if n >= 20 {
-		return numbers[n/10*10] + numbers[n-n/10*10]
+		return num2eng[n/10*10] + num2eng[n-n/10*10]
 	}
 
 	return ""
 }
 
-func main() {
+func e17() {
 	s := "onethousand"
 	for n := 1; n < 1000; n++ {
 		s += english(n)
 	}
 	fmt.Println(len(s))
 }
+
+func init() {
+    register("17", e17)
+}

go/src/euler/18.go

 
 import "fmt"
 
-var nums = [][]int{
+var triangle = [][]int{
 	{75},
 	{95, 64},
 	{17, 47, 82},
 }
 
 func maxPath(row, col int) int {
-	value := nums[row][col]
-	if row == len(nums)-1 {
+	value := triangle[row][col]
+	if row == len(triangle)-1 {
 		return value
 	}
 	return value + max(maxPath(row+1, col), maxPath(row+1, col+1))
 }
 
-func main() {
+func e18() {
 	fmt.Println(maxPath(0, 0))
 }
+
+func init() {
+    register("18", e18)
+}

go/src/euler/19.go

 	return 31
 }
 
-func main() {
+func e19() {
 	weekday := 1 // Monday
 	day, month, year := 1, 1, 1900
 	count := 0
 
 	fmt.Println(count)
 }
+
+func init() {
+    register("19", e19)
+}

go/src/euler/2.go

 	return results
 }
 
-func main() {
+func e2() {
 	sum := 0
 	for i := range fibs() {
 		if i >= 4000000 {
 
 	fmt.Println(sum)
 }
+
+func init() {
+    register("2", e2)
+}

go/src/euler/20.go

 package main
 
 import (
-	"big"
 	"fmt"
+	"math/big"
 )
 
-// taken from 16
-func sumDigits(s string) int {
-	sum := 0
-	for _, c := range s {
-		sum += c - '0'
-	}
+// sumDigits is defined in 16
 
-	return sum
-}
-
-func main() {
+func e20() {
 	fact := big.NewInt(1)
 	fact.MulRange(1, 100)
 
 	fmt.Println(sumDigits(fact.String()))
 }
+
+func init() {
+    register("20", e20)
+}

go/src/euler/21.go

 	return count
 }
 
-func main() {
+func e21() {
 	seen := map[int]bool{}
 	for i := 1; i < 10000; i++ {
 		sf := sumFactors(i)
 
 	fmt.Println(sum)
 }
+
+func init() {
+    register("21", e21)
+}

go/src/euler/22.go

 func score(name string, position int) int {
 	score := 0
 	for _, c := range name {
-		score += c - 'A' + 1
+		score += int(c) - 'A' + 1
 	}
 
 	return position * score
 	return strs
 }
 
-func main() {
+func e22() {
 	sum := 0
 	for i, name := range readNamesSorted() {
 		sum += score(name, i+1)
 	}
 	println(sum)
 }
+
+func init() {
+    register("22", e22)
+}

go/src/euler/23.go

 	return sum
 }
 
-var _cache = map[int]bool{}
+var _abudant_cache = map[int]bool{}
 
 func isAbudant(n int) bool {
-	if value, ok := _cache[n]; ok {
+	if value, ok := _abudant_cache[n]; ok {
 		return value
 	}
 
 	value := sumDivisors(n) > n
-	_cache[n] = value
+	_abudant_cache[n] = value
 	return value
 }
 
 	return false
 }
 
-func main() {
+func e23() {
 	sum := 0
 	for i := 1; i <= 28123; i++ {
 		if !isSumTwoAbudant(i) {
 
 	println(sum)
 }
+
+func init() {
+    register("23", e23)
+}

go/src/euler/24.go

 
 var items = []int{0, 1, 2}
 
-func main() {
+func e24() {
 	var i = []int{3, 1, 2}
 	fmt.Printf("%v\n", isLast(i))
 	sort.Sort(Reversed(i))
 	fmt.Printf("%v\n", i)
 	fmt.Printf("%v\n", isLast(i))
 }
+
+func init() {
+    register("24", e24)
+}

go/src/euler/25.go

 package main
 
 import (
-	"big"
 	"fmt"
+	"math/big"
 )
 
-func fibs() chan *big.Int {
+func bigFibs() chan *big.Int {
 	a, b := big.NewInt(1), big.NewInt(1)
 	out := make(chan *big.Int)
 	go func() {
 	return out
 }
 
-func main() {
+func e25() {
 	i := 0
-	for f := range fibs() {
+	for f := range bigFibs() {
 		i++
 		if len(f.String()) >= 1000 {
 			break
 	}
 	fmt.Println(i)
 }
+
+func init() {
+    register("25", e25)
+}

go/src/euler/27.go

 
 import (
 	"fmt"
-	"./primes"
 )
 
-func main() {
+func e27() {
 	best_a, best_b, best_n := 0, 0, 0
 	for a := -999; a < 1000; a++ {
 		for b := -999; b < 1000; b++ {
 				if v < 0 {
 					break
 				}
-				if !primes.IsPrime(uint64(v)) {
+				if !IsPrime(uint64(v)) {
 					break
 				}
 				n++
 
 	fmt.Println(best_a * best_b)
 }
+
+func init() {
+    register("27", e27)
+}

go/src/euler/3.go

 package main
 
 import (
-	"big"
 	"fmt"
 	"math"
+	"math/big"
 )
 
 const (
 	n = 600851475143
 )
 
-func main() {
+func e3() {
 	for start := int64(math.Sqrt(n)); start > 0; start-- {
-		if !big.ProbablyPrime(big.NewInt(start), 10) {
+		if !big.NewInt(start).ProbablyPrime(10) {
 			continue
 		}
 		if n%start == 0 {
 		}
 	}
 }
+
+func init() {
+    register("3", e3)
+}

go/src/euler/4.go

 	return true
 }
 
-func main() {
+func e4() {
 	max := 0
 	for i := 1; i < 1000; i++ {
 		for j := 1; j < 1000; j++ {
 
 	fmt.Println(max)
 }
+
+func init() {
+    register("4", e4)
+}

go/src/euler/42.go

 /*
-The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:
+The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so
+the first ten triangle numbers are:
 
 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
 
 	"regexp"
 )
 
-func triangle(n int) int {
+func triangleNum(n int) int {
 	f := float64(n)
 	return int((f / 2) * (f + 1))
 }
 func triangles(max int) map[int]bool {
 	ts := make(map[int]bool)
 	for i := 1; i <= max; i++ {
-		ts[triangle(i)] = true
+		ts[triangleNum(i)] = true
 	}
 
 	return ts
 }
 
-func main() {
+func e42() {
 	data, _ := ioutil.ReadFile("words.txt")
 	re := regexp.MustCompile("[A-Z]+")
 
 	fmt.Println(count)
 
 }
+
+func init() {
+    register("42", e42)
+}

go/src/euler/5.go

 	return true
 }
 
-func main() {
+func e5() {
 	for i := 20; true; i++ {
 		if isDivisible(i) {
 			fmt.Println(i)
 		}
 	}
 }
+
+func init() {
+    register("5", e5)
+}

go/src/euler/6.go

 	return sum * sum
 }
 
-func main() {
+func e6() {
 	fmt.Println(squareOfSums(100) - sumOfSquares(100))
 }
+
+func init() {
+    register("6", e6)
+}

go/src/euler/7.go

 
 import (
 	"fmt"
-
-	"./primes"
 )
 
-func main() {
+func e7() {
 	i := 0
-	for p := range primes.Primes() {
+	for p := range Primes() {
 		i++
 		if i > 10000 {
 			fmt.Println(p)
 	}
 
 }
+
+func init() {
+    register("7", e7)
+}

go/src/euler/8.go

 	return out
 }
 
-func main() {
+func e8() {
 	max_p := 0
 	for p := range products() {
 		if p > max_p {
 
 	fmt.Println(max_p)
 }
+
+func init() {
+    register("8", e8)
+}

go/src/euler/9.go

 	return a*a+b*b == c*c
 }
 
-func main() {
+func e9() {
 	for a := 1; a < 1000; a++ {
 		for b := 1; b < 1000; b++ {
 			for c := 1; c < 1000; c++ {
 	}
 	fmt.Println(isPythagorean(3, 4, 5))
 }
+
+func init() {
+    register("9", e9)
+}

go/src/euler/Makefile

-LIBS_SRC=$(wildcard [a-z]*.go)
-LIBS=$(LIBS_SRC:%.go=%.6)
+export GOPATH := $(shell dirname $(shell dirname $(PWD)))
 
-SRC = $(wildcard [0-9]*.go)
-PROGRAMS = $(SRC:%.go=%)
-
-all: $(LIBS) $(PROGRAMS)
-
-clean:
-	rm -f $(PROGRAMS)
-	rm -f $(LIBS)
-	rm -f $(PROGRAMS:%=%.6)
-
-fresh: clean all
-
-.PHONY: all clean fresh
+all:
+	go build euler

go/src/euler/euler.go

+package main
+
+import (
+	"flag"
+	"fmt"
+)
+
+type Solution struct {
+	name string
+	fn   func()
+}
+
+var solutions = []Solution{}
+
+func register(name string, fn func()) {
+	solutions = append(solutions, Solution{name, fn})
+}
+
+
+func main() {
+	list := flag.Bool("list", false, "list solutions")
+	flag.Parse()
+
+	if *list {
+		for _, sol := range solutions {
+			fmt.Println(sol.name)
+		}
+	}
+
+}

go/src/euler/primes.go

-package primes
+package main
 
 func integers() chan uint64 {
 	out := make(chan uint64)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.