Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Euler - Problem 1 solution #721

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions project_euler/problem_1/problem1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Problem 1 - Multiples of 3 and 5
*
* @see {@link https://projecteuler.net/problem=1}
*
* If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
* Find the sum of all the multiples of 3 or 5 below 1000.
*
* @author ddaniel27
*/
package problem_1

func Problem1(n uint) uint {
sum := uint(0)

for i := uint(1); i < n; i++ {
if i%3 == 0 || i%5 == 0 {
sum += i
}
}

return sum
}
49 changes: 49 additions & 0 deletions project_euler/problem_1/problem1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package problem_1_test

import (
"testing"

"github.com/TheAlgorithms/Go/project_euler/problem_1"
)

// Tests
func TestProblem1_Func(t *testing.T) {
tests := []struct {
name string
threshold uint
want uint
}{
{
name: "Testcase 1 - threshold 10",
threshold: 10,
want: 23,
},
{
name: "Testcase 2 - threshold 100",
threshold: 100,
want: 2318,
},
{
name: "Testcase 3 - threshold 1000",
threshold: 1000,
want: 233168,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := problem_1.Problem1(tt.threshold)

if n != tt.want {
t.Errorf("Problem1() = %v, want %v", n, tt.want)
}
})
}
}

// Benchmarks
func BenchmarkProblem1(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = problem_1.Problem1(1000)
}
}