Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This workflow will run Swiftlint on a project
# For more information see: https://github.com/marketplace/actions/github-action-for-swiftlint

name: SwiftLint

on:
pull_request:
paths:
- '.github/workflows/swiftlint.yml'
- '.swiftlint.yml'
- '**/*.swift'

jobs:
SwiftLint:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: GitHub Action for SwiftLint
uses: norio-nomura/action-swiftlint@master
6 changes: 6 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

included:
- Sources
excluded:
- .build

6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let package = Package(
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "ProjectEuler",
targets: ["ProjectEuler"]),
targets: ["ProjectEuler"])
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand All @@ -19,7 +19,7 @@ let package = Package(
.testTarget(
name: "ProjectEulerTests",
dependencies: ["ProjectEuler"]
),
)
],
swiftLanguageModes: [ .v6 ]
swiftLanguageModes: [ .v6 ]
)
7 changes: 3 additions & 4 deletions Sources/ProjectEuler/Problem001.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/// Project Euler [Problem 1](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.
/// > 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.
struct Problem001 {

init() { }


func sum(of multiples: [Int], max: Int) -> Int {
(0..<max)
.filter { number in
Expand Down
16 changes: 8 additions & 8 deletions Sources/ProjectEuler/Problem002.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/// Project Euler [Problem 2](https://projecteuler.net/problem=2)
/// > Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
/// > Each new term in the Fibonacci sequence is generated by adding the previous two terms.
/// > By starting with 1 and 2, the first 10 terms will be:
/// >
/// > 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
/// >
/// > By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
/// > By considering the terms in the Fibonacci sequence whose values do not exceed four million,
/// > find the sum of the even-valued terms.
struct Problem002 {

init() { }


func sumEvenFibonacci(_ max: Int) -> Int {
generateFibonacciNumbersUpTo(max)
.filter { $0.isMultiple(of: 2) }
Expand All @@ -19,15 +19,15 @@ struct Problem002 {
var numbers: [Int: Int] = [:]
var index: Int = 0
var fibonacci = 1

while fibonacci <= max {
defer { index += 1 }
fibonacci = generator.fibonacci(at: index)

guard fibonacci <= max else { break }
numbers[index] = fibonacci
}

return numbers.values.sorted()
}
}
6 changes: 3 additions & 3 deletions Sources/ProjectEuler/utils/FibonacciGenerator.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
final class FibonacciGenerator {
private var memoized: [Int: Int] = [:]

func fibonacci(at index: Int) -> Int {
if let fibonacci = memoized[index] {
return fibonacci
}

guard index >= 2 else {
memoized[index] = index
return index
}

let fibonacci = fibonacci(at: index - 1) + fibonacci(at: index - 2)
memoized[index] = fibonacci
return fibonacci
Expand Down
8 changes: 4 additions & 4 deletions Tests/ProjectEulerTests/Problem001Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import Testing
@Suite("Problem 001")
struct Problem001Tests {
var subject: Problem001 = .init()

@Test("Known sum of all the multiples of 3 or 5 below 10.")
func test_knownValue() {
let test = subject.sum(of: [3, 5], max: 10)

#expect(test == 23)
}

@Test("Test sum of all the multiples of 3 or 5 below 1000.")
func test_testValue() {
let test = subject.sum(of: [3, 5], max: 1000)

#expect(test == 233168)
}
}
8 changes: 4 additions & 4 deletions Tests/ProjectEulerTests/Problem002Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import Testing
@Suite("Problem 002")
struct Problem002Tests {
var subject: Problem002 = .init()

@Test("Known even Fibonacci sum below 89.")
func test_knownValue() {
let test = subject.sumEvenFibonacci(89)

#expect(test == 44)
}

@Test("Test even Fibonacci sum below 4,000,000.")
func test_testValue() {
let test = subject.sumEvenFibonacci(4_000_000)

#expect(test == 4613732)
}
}