diff --git a/.github/workflows/swiftlint.yml b/.github/workflows/swiftlint.yml new file mode 100644 index 0000000..7dbbe8d --- /dev/null +++ b/.github/workflows/swiftlint.yml @@ -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 \ No newline at end of file diff --git a/.swiftlint.yml b/.swiftlint.yml new file mode 100644 index 0000000..cf66318 --- /dev/null +++ b/.swiftlint.yml @@ -0,0 +1,6 @@ + +included: + - Sources +excluded: + - .build + \ No newline at end of file diff --git a/Package.swift b/Package.swift index 3a101fb..ea36a27 100644 --- a/Package.swift +++ b/Package.swift @@ -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. @@ -19,7 +19,7 @@ let package = Package( .testTarget( name: "ProjectEulerTests", dependencies: ["ProjectEuler"] - ), + ) ], - swiftLanguageModes: [ .v6 ] + swiftLanguageModes: [ .v6 ] ) diff --git a/Sources/ProjectEuler/Problem001.swift b/Sources/ProjectEuler/Problem001.swift index 8dbe63f..dceb1e0 100644 --- a/Sources/ProjectEuler/Problem001.swift +++ b/Sources/ProjectEuler/Problem001.swift @@ -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.. 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) } @@ -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() } } diff --git a/Sources/ProjectEuler/utils/FibonacciGenerator.swift b/Sources/ProjectEuler/utils/FibonacciGenerator.swift index f02158c..8140150 100644 --- a/Sources/ProjectEuler/utils/FibonacciGenerator.swift +++ b/Sources/ProjectEuler/utils/FibonacciGenerator.swift @@ -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 diff --git a/Tests/ProjectEulerTests/Problem001Tests.swift b/Tests/ProjectEulerTests/Problem001Tests.swift index 8ccc515..3094366 100644 --- a/Tests/ProjectEulerTests/Problem001Tests.swift +++ b/Tests/ProjectEulerTests/Problem001Tests.swift @@ -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) } } diff --git a/Tests/ProjectEulerTests/Problem002Tests.swift b/Tests/ProjectEulerTests/Problem002Tests.swift index a965330..b7e383c 100644 --- a/Tests/ProjectEulerTests/Problem002Tests.swift +++ b/Tests/ProjectEulerTests/Problem002Tests.swift @@ -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) } }