Skip to content

Commit fb570ff

Browse files
committed
Project Euler: First Ten solutions
1 parent 4263dac commit fb570ff

File tree

20 files changed

+690
-0
lines changed

20 files changed

+690
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Problem 1 - Multiples of 3 and 5
3+
*
4+
* @see {@link https://projecteuler.net/problem=1}
5+
*
6+
* If we list all the natural numbers below 10 that are multiples of 3 or 5,
7+
* we get 3, 5, 6 and 9. The sum of these multiples is 23.
8+
* Find the sum of all the multiples of 3 or 5 below 1000.
9+
*
10+
* @author ddaniel27
11+
*/
12+
package problem1
13+
14+
func Problem1(n uint) uint {
15+
sum := uint(0)
16+
17+
for i := uint(1); i < n; i++ {
18+
if i%3 == 0 || i%5 == 0 {
19+
sum += i
20+
}
21+
}
22+
23+
return sum
24+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package problem1
2+
3+
import "testing"
4+
5+
// Tests
6+
func TestProblem1_Func(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
threshold uint
10+
want uint
11+
}{
12+
{
13+
name: "Testcase 1 - threshold 10",
14+
threshold: 10,
15+
want: 23,
16+
},
17+
{
18+
name: "Testcase 2 - threshold 100",
19+
threshold: 100,
20+
want: 2318,
21+
},
22+
{
23+
name: "Testcase 3 - threshold 1000",
24+
threshold: 1000,
25+
want: 233168,
26+
},
27+
}
28+
29+
for _, tt := range tests {
30+
t.Run(tt.name, func(t *testing.T) {
31+
n := Problem1(tt.threshold)
32+
33+
if n != tt.want {
34+
t.Errorf("Problem1() = %v, want %v", n, tt.want)
35+
}
36+
})
37+
}
38+
}
39+
40+
// Benchmarks
41+
func BenchmarkProblem1(b *testing.B) {
42+
for i := 0; i < b.N; i++ {
43+
_ = Problem1(1000)
44+
}
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Problem 10 - Summation of primes
3+
* @see {@link https://projecteuler.net/problem=10}
4+
*
5+
* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
6+
* Find the sum of all the primes below two million.
7+
*
8+
* @author ddaniel27
9+
*/
10+
package problem10
11+
12+
import "github.com/TheAlgorithms/Go/math/prime"
13+
14+
func Problem10(n int) uint {
15+
sum := uint(0)
16+
sieve := prime.SieveEratosthenes(n)
17+
18+
for _, v := range sieve {
19+
sum += uint(v)
20+
}
21+
22+
return sum
23+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package problem10
2+
3+
import "testing"
4+
5+
// Tests
6+
func TestProblem10_Func(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
input int
10+
want uint
11+
}{
12+
{
13+
name: "Testcase 1 - input 10",
14+
input: 10,
15+
want: 17,
16+
},
17+
{
18+
name: "Testcase 2 - input 2000000",
19+
input: 2000000,
20+
want: 142913828922,
21+
},
22+
}
23+
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
n := Problem10(tt.input)
27+
28+
if n != tt.want {
29+
t.Errorf("Problem10() = %v, want %v", n, tt.want)
30+
}
31+
})
32+
}
33+
}
34+
35+
// Benchmarks
36+
func BenchmarkProblem10(b *testing.B) {
37+
for i := 0; i < b.N; i++ {
38+
_ = Problem10(2000000)
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Problem 2 - Even Fibonacci numbers
3+
* @see {@link https://projecteuler.net/problem=2}
4+
*
5+
* Each new term in the Fibonacci sequence is generated by adding the previous two terms.
6+
* By starting with 1 and 2, the first 10 terms will be:
7+
*
8+
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
9+
*
10+
* By considering the terms in the Fibonacci sequence whose values do not exceed four million,
11+
* find the sum of the even-valued terms.
12+
*
13+
* @author ddaniel27
14+
*/
15+
package problem2
16+
17+
func Problem2(n uint) uint {
18+
sum := uint(0)
19+
a, b := uint(1), uint(2)
20+
21+
for b < n {
22+
if b%2 == 0 {
23+
sum += b
24+
}
25+
26+
a, b = b, a+b
27+
}
28+
29+
return sum
30+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package problem2
2+
3+
import "testing"
4+
5+
// Tests
6+
func TestProblem2_Func(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
input uint
10+
want uint
11+
}{
12+
{
13+
name: "Testcase 1 - input 10",
14+
input: 10,
15+
want: 10,
16+
},
17+
{
18+
name: "Testcase 2 - input 100",
19+
input: 100,
20+
want: 44,
21+
},
22+
{
23+
name: "Testcase 3 - input 4e6",
24+
input: 4e6,
25+
want: 4613732,
26+
},
27+
}
28+
29+
for _, tt := range tests {
30+
t.Run(tt.name, func(t *testing.T) {
31+
n := Problem2(tt.input)
32+
33+
if n != tt.want {
34+
t.Errorf("Problem2() = %v, want %v", n, tt.want)
35+
}
36+
})
37+
}
38+
}
39+
40+
// Benchmarks
41+
func BenchmarkProblem2(b *testing.B) {
42+
for i := 0; i < b.N; i++ {
43+
_ = Problem2(4e6)
44+
}
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Problem 3 - Largest prime factor
3+
* @see {@link https://projecteuler.net/problem=3}
4+
*
5+
* The prime factors of 13195 are 5, 7, 13 and 29.
6+
* What is the largest prime factor of the number 600851475143 ?
7+
*
8+
* @author ddaniel27
9+
*/
10+
package problem3
11+
12+
func Problem3(n uint) uint {
13+
i := uint(2)
14+
15+
for n > 1 {
16+
if n%i == 0 {
17+
n /= i
18+
} else {
19+
i++
20+
}
21+
}
22+
23+
return i
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package problem3
2+
3+
import "testing"
4+
5+
// Tests
6+
func TestProblem3_Func(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
input uint
10+
want uint
11+
}{
12+
{
13+
name: "Testcase 1 - input 13195",
14+
input: 13195,
15+
want: 29,
16+
},
17+
{
18+
name: "Testcase 2 - input 600851475143",
19+
input: 600851475143,
20+
want: 6857,
21+
},
22+
}
23+
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
n := Problem3(tt.input)
27+
28+
if n != tt.want {
29+
t.Errorf("Problem3() = %v, want %v", n, tt.want)
30+
}
31+
})
32+
}
33+
}
34+
35+
// Benchmarks
36+
func BenchmarkProblem3(b *testing.B) {
37+
for i := 0; i < b.N; i++ {
38+
_ = Problem3(600851475143)
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Problem 4 - Largest palindrome product
3+
* @see {@link https://projecteuler.net/problem=4}
4+
*
5+
* A palindromic number reads the same both ways.
6+
* The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
7+
* Find the largest palindrome made from the product of two 3-digit numbers.
8+
*
9+
* @author ddaniel27
10+
*/
11+
package problem4
12+
13+
import (
14+
"fmt"
15+
16+
"github.com/TheAlgorithms/Go/strings/palindrome"
17+
)
18+
19+
func Problem4() uint {
20+
max := uint(0)
21+
22+
for i := 999; i >= 100; i-- {
23+
for j := 999; j >= 100; j-- {
24+
n := uint(i * j)
25+
26+
if palindrome.IsPalindrome(fmt.Sprintf("%d", n)) && n > max {
27+
max = n
28+
}
29+
}
30+
}
31+
32+
return max
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package problem4
2+
3+
import "testing"
4+
5+
// Tests
6+
func TestProblem4_Func(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
want uint
10+
}{
11+
{
12+
name: "Testcase 1",
13+
want: 906609,
14+
},
15+
}
16+
17+
for _, tt := range tests {
18+
t.Run(tt.name, func(t *testing.T) {
19+
n := Problem4()
20+
21+
if n != tt.want {
22+
t.Errorf("Problem4() = %v, want %v", n, tt.want)
23+
}
24+
})
25+
}
26+
}
27+
28+
// Benchmarks
29+
func BenchmarkProblem4(b *testing.B) {
30+
for i := 0; i < b.N; i++ {
31+
_ = Problem4()
32+
}
33+
}

0 commit comments

Comments
 (0)