From 6e3185914e8190da0f85e9461a80a584a31e1d97 Mon Sep 17 00:00:00 2001 From: Angelica Hjelm Gardner Date: Mon, 9 Mar 2026 20:49:16 +0300 Subject: [PATCH] feat(euler): solve problem 001 multiples of 3 and 5 --- .github/workflows/go-test.yaml | 6 +++--- .github/workflows/lint.yaml | 14 +++++++------ .golangci.yaml | 26 ++++++++++++++++-------- README.md | 11 +--------- go.mod | 2 +- {ProjectEuler => projecteuler}/README.md | 0 projecteuler/problem_001.go | 14 +++++++++++++ projecteuler/problem_001_test.go | 25 +++++++++++++++++++++++ 8 files changed, 70 insertions(+), 28 deletions(-) rename {ProjectEuler => projecteuler}/README.md (100%) create mode 100644 projecteuler/problem_001.go create mode 100644 projecteuler/problem_001_test.go diff --git a/.github/workflows/go-test.yaml b/.github/workflows/go-test.yaml index 9cc1aae..aad9ebf 100644 --- a/.github/workflows/go-test.yaml +++ b/.github/workflows/go-test.yaml @@ -10,12 +10,12 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: - go-version: '1.21' + go-version: '1.24' - name: Install dependencies run: go mod tidy diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 1ad1345..bd2ea1f 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -12,16 +12,18 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: - go-version: '1.21' + go-version: '1.24' + cache: true - - name: Install golangci-lint - run: | - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.59.1 + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.64.5 - name: Print Go environment run: | diff --git a/.golangci.yaml b/.golangci.yaml index bbd5216..5670271 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,7 +1,7 @@ linters: + disable-all: false enable: - errorlint - #- godot - goimports - misspell - nolintlint @@ -9,21 +9,31 @@ linters: - testifylint - unconvert - durationcheck - - exportloopref - nilerr - whitespace - unparam - tagalign - stylecheck - gosec - #- gofumpt - perfsprint - nestif - noctx - prealloc - depguard - # - revive - # - gocritic - # - wastedassign - # - goconst - # - gomnd \ No newline at end of file + - gocritic + - bodyclose + +linters-settings: + depguard: + rules: + main: + deny: + - pkg: "io/ioutil" + desc: "ioutil is deprecated, use os or io instead" + + nestif: + min-complexity: 5 + +run: + timeout: 5m + tests: true \ No newline at end of file diff --git a/README.md b/README.md index beb14a1..2b107dc 100644 --- a/README.md +++ b/README.md @@ -30,16 +30,7 @@ The test cases should test both solutions. ### Solution Template ```go -/* -[Issue title] - -// Time complexity: O(n) -// Space complexity: O(n) -func ImprovedSolution() { - -*/ - -package challenge +package challengeName // Time complexity: O(n) // Space complexity: O(n) diff --git a/go.mod b/go.mod index cc6e73a..c5e9d49 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/angelicagardner/coding-challenges -go 1.21.1 +go 1.24 diff --git a/ProjectEuler/README.md b/projecteuler/README.md similarity index 100% rename from ProjectEuler/README.md rename to projecteuler/README.md diff --git a/projecteuler/problem_001.go b/projecteuler/problem_001.go new file mode 100644 index 0000000..7eae9d3 --- /dev/null +++ b/projecteuler/problem_001.go @@ -0,0 +1,14 @@ +package projecteuler + +// Time complexity: O(1) +// Space complexity: O(1) +func Solution(limit int) int { + n := limit - 1 + + sumMultiples := func(k int) int { + p := n / k + return k * (p * (p + 1)) / 2 + } + + return sumMultiples(3) + sumMultiples(5) - sumMultiples(15) +} diff --git a/projecteuler/problem_001_test.go b/projecteuler/problem_001_test.go new file mode 100644 index 0000000..519447f --- /dev/null +++ b/projecteuler/problem_001_test.go @@ -0,0 +1,25 @@ +package projecteuler + +import "testing" + +func TestSolution(t *testing.T) { + t.Run("limit 10", func(t *testing.T) { + limit := 10 + expected := 23 + got := Solution(limit) + + if got != expected { + t.Errorf("Solution(%d) = %d; want %d", limit, got, expected) + } + }) + + t.Run("limit 1000", func(t *testing.T) { + limit := 1000 + expected := 233168 + got := Solution(limit) + + if got != expected { + t.Errorf("Solution(%d) = %d; want %d", limit, got, expected) + } + }) +}