Sha256: c82ad5fc737f94698aff187eed3511fd06358be1857290234b743fb2730564ae

Contents?: true

Size: 1.07 KB

Versions: 59

Compression:

Stored size: 1.07 KB

Contents

#if os(Linux)
    import Glibc
#elseif os(OSX)
    import Darwin
#endif

struct Triplet {

    var a: Int, b: Int, c: Int = 0

    init(_ a: Int, _ b: Int, _ c: Int) {
        self.a = a
        self.b = b
        self.c = c
    }

    var sum: Int {return a + b + c }

    var product: Int {return a * b * c }

    var isPythagorean: Bool {return pow(Double(a), 2) + pow(Double(b), 2) == pow(Double(c), 2)}

    static func Where(_ minFactor: Int = 1, maxFactor: Int, sum: Int = 0) -> [Triplet] {

        func shouldIncludeTriplet(_ sum: Int, triplet: Triplet) -> Bool {
            return triplet.isPythagorean && (sum == 0 || triplet.sum == sum)
        }

        var triplets = [Triplet]()
        for i in minFactor ..< maxFactor - 1 {
            for j in i + 1 ..< maxFactor {
                for k in j + 1 ... maxFactor {
                    let triplet = Triplet(i, j, k)
                    if shouldIncludeTriplet(sum, triplet: triplet) {
                        triplets.append(triplet)
                    }
                }
            }
        }
        return triplets
    }

}

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
trackler-2.0.8.30 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.29 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.28 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.27 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.26 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.24 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.23 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.22 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.21 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.20 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.19 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.18 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.17 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.16 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.15 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.14 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.13 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.12 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.11 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.10 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift