Skip to content

Commit 1c1a101

Browse files
committed
16 to 20
1 parent 3e02f1e commit 1c1a101

File tree

16 files changed

+905
-0
lines changed

16 files changed

+905
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Problem 16 - Power digit sum
3+
* @see {@link https://projecteuler.net/problem=16}
4+
*
5+
* 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
6+
*
7+
* What is the sum of the digits of the number 2^1000?
8+
*
9+
* @author ddaniel27
10+
*/
11+
package problem16
12+
13+
import (
14+
"math/big"
15+
)
16+
17+
func Problem16(exponent int64) int64 {
18+
var result big.Int
19+
20+
bigTwo := big.NewInt(2)
21+
bigExponent := big.NewInt(exponent)
22+
23+
result.Exp(bigTwo, bigExponent, nil)
24+
25+
resultStr := result.String()
26+
27+
var sum int64
28+
for _, digit := range resultStr {
29+
sum += int64(digit - '0')
30+
}
31+
32+
return sum
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problem16
2+
3+
import "testing"
4+
5+
// Tests
6+
func TestProblem16_Func(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
exponent int64
10+
want int64
11+
}{
12+
{"2^15", 15, 26},
13+
{"2^1000", 1000, 1366},
14+
}
15+
16+
for _, tt := range tests {
17+
t.Run(tt.name, func(t *testing.T) {
18+
if got := Problem16(tt.exponent); got != tt.want {
19+
t.Errorf("Problem16() = %v, want %v", got, tt.want)
20+
}
21+
})
22+
}
23+
}
24+
25+
// Benchmark
26+
func BenchmarkProblem16_Func(b *testing.B) {
27+
for i := 0; i < b.N; i++ {
28+
Problem16(1000)
29+
}
30+
}

project_euler/problem_17/input.go

Lines changed: 8 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Problem 17 - Number letter counts
3+
* @see {@link https://projecteuler.net/problem=17}
4+
*
5+
* If the numbers 1 to 5 are written out in words: one, two, three, four, five,
6+
* then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
7+
*
8+
* If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words,
9+
* how many letters would be used?
10+
*
11+
* NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two)
12+
* contains 23 letters and 115 (one hundred and fifteen) contains 20 letters.
13+
* The use of "and" when writing out numbers is in compliance with British usage.
14+
*
15+
* @author ddaniel27
16+
*/
17+
package problem17
18+
19+
import "strings"
20+
21+
func Problem17(input string) int {
22+
var sum int
23+
24+
parsed := strings.Split(input, " ")
25+
26+
for _, word := range parsed {
27+
sum += len(word)
28+
}
29+
30+
return sum
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problem17
2+
3+
import "testing"
4+
5+
// Tests
6+
func TestProblem17_Func(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
input string
10+
want int
11+
}{
12+
{"1 to 5", "one two three four five", 19},
13+
{"1 to 1000", INPUT, 21124},
14+
}
15+
16+
for _, tt := range tests {
17+
t.Run(tt.name, func(t *testing.T) {
18+
if got := Problem17(tt.input); got != tt.want {
19+
t.Errorf("Problem17() = %v, want %v", got, tt.want)
20+
}
21+
})
22+
}
23+
}
24+
25+
// Benchmark
26+
func BenchmarkProblem17_Func(b *testing.B) {
27+
for i := 0; i < b.N; i++ {
28+
Problem17(INPUT)
29+
}
30+
}

project_euler/problem_18/edge.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package problem18
2+
3+
type Edge struct {
4+
ID int
5+
NodeValue NodeValue
6+
NodeLeft Node
7+
NodeRight Node
8+
Parent Node
9+
}
10+
11+
func (n *Edge) Value() NodeValue {
12+
return n.NodeValue
13+
}
14+
15+
func (n *Edge) Left() Node {
16+
return n.NodeLeft
17+
}
18+
19+
func (n *Edge) Right() Node {
20+
return n.NodeRight
21+
}
22+
23+
func (n *Edge) Kind() string {
24+
return "edge"
25+
}
26+
27+
func (n *Edge) CreateChild(value NodeValue, id int) Node {
28+
// When the left child is nil, it's a left edge
29+
if n.NodeLeft == nil {
30+
return &Edge{
31+
ID: id,
32+
NodeValue: value,
33+
Parent: n,
34+
NodeLeft: nil,
35+
NodeRight: nil,
36+
}
37+
}
38+
39+
// When the left child is a leaf, it's a right edge
40+
if n.NodeLeft.Kind() == "leaf" {
41+
return &Edge{
42+
ID: id,
43+
NodeValue: value,
44+
Parent: n,
45+
NodeLeft: nil,
46+
NodeRight: nil,
47+
}
48+
}
49+
50+
return &Leaf{
51+
ID: id,
52+
NodeValue: value,
53+
Parent: n,
54+
NodeLeft: nil,
55+
NodeRight: nil,
56+
}
57+
}
58+
59+
func (n *Edge) GetID() int {
60+
return n.ID
61+
}
62+
63+
func (n *Edge) Insert(node Node) {
64+
// If Left is nil, always simply insert the node
65+
if n.NodeLeft == nil {
66+
node.SetParent(n)
67+
n.NodeLeft = node
68+
69+
return
70+
}
71+
72+
// If Right is nil, insert the node
73+
n.NodeRight = node
74+
75+
// If the node to insert is an edge, set the parent
76+
if node.Kind() == "edge" {
77+
node.SetParent(n)
78+
79+
return
80+
}
81+
82+
// If the node to insert is a leaf, send it to the sibling right
83+
n.Parent.Right().Insert(node)
84+
}
85+
86+
func (n *Edge) HasSpace() bool {
87+
return n.NodeLeft == nil || n.NodeRight == nil
88+
}
89+
90+
func (n *Edge) LeftIsNil() bool {
91+
return n.NodeLeft == nil
92+
}
93+
94+
func (n *Edge) RightIsNil() bool {
95+
return n.NodeRight == nil
96+
}
97+
98+
func (n *Edge) SetParent(node Node) {
99+
n.Parent = node
100+
}

project_euler/problem_18/input.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package problem18
2+
3+
import "strings"
4+
5+
const problem18_input_string = `
6+
75
7+
95 64
8+
17 47 82
9+
18 35 87 10
10+
20 04 82 47 65
11+
19 01 23 75 03 34
12+
88 02 77 73 07 63 67
13+
99 65 04 28 06 16 70 92
14+
41 41 26 56 83 40 80 70 33
15+
41 48 72 33 47 32 37 16 94 29
16+
53 71 44 65 25 43 91 52 97 51 14
17+
70 11 33 28 77 73 17 78 39 68 17 57
18+
91 71 52 38 17 14 91 43 58 50 27 29 48
19+
63 66 04 68 89 53 67 30 73 16 69 87 40 31
20+
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
21+
`
22+
23+
var problem18_input_parsed_string []string = strings.Split(
24+
strings.Trim(
25+
strings.ReplaceAll(problem18_input_string, "\n", " "),
26+
" ",
27+
),
28+
" ")
29+
30+
const problem18_test_string = `
31+
3
32+
7 4
33+
2 4 6
34+
8 5 9 3
35+
`
36+
37+
var problem18_test_parsed_string []string = strings.Split(
38+
strings.Trim(
39+
strings.ReplaceAll(problem18_test_string, "\n", " "),
40+
" ",
41+
),
42+
" ")

project_euler/problem_18/leaf.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package problem18
2+
3+
type Leaf struct {
4+
ID int
5+
NodeValue NodeValue
6+
NodeLeft *Leaf
7+
NodeRight *Leaf
8+
Parent Node
9+
}
10+
11+
func (n *Leaf) Value() NodeValue {
12+
return n.NodeValue
13+
}
14+
15+
func (n *Leaf) Left() Node {
16+
if n.NodeLeft != nil {
17+
n.NodeLeft.Parent = n // Leaf is the parent of its left child always
18+
}
19+
20+
return n.NodeLeft
21+
}
22+
23+
func (n *Leaf) Right() Node {
24+
return n.NodeRight
25+
}
26+
27+
func (n *Leaf) Kind() string {
28+
return "leaf"
29+
}
30+
31+
func (n *Leaf) CreateChild(value NodeValue, id int) Node {
32+
// Leafs only have leaf children
33+
return &Leaf{
34+
ID: id,
35+
NodeValue: value,
36+
Parent: n,
37+
NodeLeft: nil,
38+
NodeRight: nil,
39+
}
40+
}
41+
42+
func (n *Leaf) GetID() int {
43+
return n.ID
44+
}
45+
46+
func (n *Leaf) Insert(node Node) {
47+
// If Left is nil, always simply insert the node
48+
if n.NodeLeft == nil {
49+
node.SetParent(n)
50+
n.NodeLeft = node.(*Leaf)
51+
52+
return
53+
}
54+
55+
// If Right is nil, insert the node
56+
n.NodeRight = node.(*Leaf)
57+
// Send it to the sibling right
58+
n.Parent.Right().Insert(node)
59+
}
60+
61+
func (n *Leaf) HasSpace() bool {
62+
return n.NodeLeft == nil || n.NodeRight == nil
63+
}
64+
65+
func (n *Leaf) LeftIsNil() bool {
66+
return n.NodeLeft == nil
67+
}
68+
69+
func (n *Leaf) RightIsNil() bool {
70+
return n.NodeRight == nil
71+
}
72+
73+
func (n *Leaf) SetParent(node Node) {
74+
n.Parent = node
75+
}

0 commit comments

Comments
 (0)