Skip to content

Commit

Permalink
go
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Strelau committed Oct 22, 2013
1 parent c7c4f37 commit de153f8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions data/022

Large diffs are not rendered by default.

72 changes: 68 additions & 4 deletions euler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"fmt"
"io/ioutil"
"math"
"math/big"
"os"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -140,16 +142,16 @@ func numInWords(lookupTable map[int]string, n int) string {
return strings.Join(result, " ")
}

func readFileLines(filename string) []string {
func readFile(filename string) string {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
return strings.Split(strings.Trim(string(data), "\n"), "\n")
return strings.Trim(string(data), "\n")
}

func readIntMatrix(filename string) [][]int {
lines := readFileLines(filename)
lines := strings.Split(readFile(filename), "\n")
matrix := make([][]int, len(lines))
for i := 0; i < len(lines); i++ {
line := strings.Split(lines[i], " ")
Expand Down Expand Up @@ -190,6 +192,28 @@ func daysIn(year int, month int) int {
}
}

func sumSlice(s []int) int {
sum := 0
for _, n := range s {
sum += n
}
return sum
}

func divisors(n int) []int {
r := []int{1}
root := int(math.Sqrt(float64(n)))
for i := 2; i <= root; i++ {
if n%i == 0 {
r = append(r, i)
if i != n/i {
r = append(r, n/i)
}
}
}
return r
}

/////////////////////////////////////////////////////////////////////////////

func defineSolutions() map[int]solution {
Expand Down Expand Up @@ -246,7 +270,6 @@ func defineSolutions() map[int]solution {
}

// 005: Find the smallest positive number evenly divisible by all of (1..20)

// 006: Find the difference of sum of squares and square of the sum of (1..100)
// 007: What is the 10001st prime number?
// 008: Find the greatest product of five consecutive digits in a 1000-digit number.
Expand Down Expand Up @@ -311,5 +334,46 @@ func defineSolutions() map[int]solution {
return sum
}

// 021: Evaluate the sum of all the amicable numbers under 10000.
solutions[21] = func() int {
result := 0
toSum := make(map[int]struct{})
sumOfDivisors := make(map[int]int)
for i := 0; i <= 10000; i++ {
sumOfDivisors[i] = sumSlice(divisors(i))
}
for a := 0; a <= 10000; a++ {
b := sumOfDivisors[a]
if sumOfDivisors[b] == a && a != b {
toSum[a] = struct{}{}
toSum[b] = struct{}{}
}
}
for n, _ := range toSum {
result += n
}
return result
}

// 022: Compute the sum of character-position scores for a word list
solutions[22] = func() int {
a := int('A')
names := strings.Split(readFile("data/022"), ",")
for i, n := range names {
names[i] = strings.Trim(n, "\"")
}
sort.Strings(names)

result := 0
for i, n := range names {
score := 0
for _, c := range n {
score += int(c) - a + 1
}
result += score * (i + 1)
}
return result
}

return solutions
}
13 changes: 13 additions & 0 deletions problems.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,16 @@ which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a
and b are an amicable pair and each of a and b are called amicable numbers.

Evaluate the sum of all the amicable numbers under 10000.

== 022
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file
containing over five-thousand first names, begin by sorting it into
alphabetical order. Then working out the alphabetical value for each name,
multiply this value by its alphabetical position in the list to obtain a name
score.

For example, when the list is sorted into alphabetical order, COLIN, which is
worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN
would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

0 comments on commit de153f8

Please sign in to comment.