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.9 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.8 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.7 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.6 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.5 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.4 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.3 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.2 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.8.1 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.7.0 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.6.44 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.6.43 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.6.42 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.6.41 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.6.40 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.6.39 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.6.38 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.6.37 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.6.36 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift
trackler-2.0.6.35 tracks/swift/exercises/pythagorean-triplet/Sources/PythagoreanTripletExample.swift