Sha256: 4e792ac36136f7eda3d3b770009ff657efd5f496611c71bb23d11cc20589e635

Contents?: true

Size: 899 Bytes

Versions: 71

Compression:

Stored size: 899 Bytes

Contents

import Darwin

struct Grains {

    enum GrainsError: Error {
        case inputTooLow(String)
        case inputTooHigh(String)
    }

    static func square(_ num: Int) throws -> UInt64 {
        guard num >= 1 else {
            let message = "Input[\(num)] invalid. Input should be between 1 and 64 (inclusive)"
            throw GrainsError.inputTooLow(message)
        }

        guard num <= 64 else {
            let message = "Input[\(num)] invalid. Input should be between 1 and 64 (inclusive)"
            throw GrainsError.inputTooHigh(message)
        }

        let one: UInt64 = 1
        let shift = UInt64(num - 1)
        return one << shift
    }

    static var total: UInt64 {
        let numbers = (1...64).map { $0 }

        return numbers.reduce(UInt64(0)) {
            guard let squared = try? square($1) else {return $0}
            return $0 + squared
        }
    }
}

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.0.0.0 tracks/swift/exercises/grains/GrainsExample.swift
trackler-1.0.4.1 tracks/swift/exercises/grains/GrainsExample.swift
trackler-1.0.4.0 tracks/swift/exercises/grains/GrainsExample.swift
trackler-1.0.3.0 tracks/swift/exercises/grains/GrainsExample.swift
trackler-1.0.2.1 tracks/swift/exercises/grains/GrainsExample.swift
trackler-1.0.2.0 tracks/swift/exercises/grains/GrainsExample.swift
trackler-1.0.1.2 tracks/swift/exercises/grains/GrainsExample.swift
trackler-1.0.1.1 tracks/swift/exercises/grains/GrainsExample.swift
trackler-1.0.1.0 tracks/swift/exercises/grains/GrainsExample.swift
trackler-1.0.0.1 tracks/swift/exercises/grains/GrainsExample.swift
trackler-1.0.0 tracks/swift/exercises/grains/GrainsExample.swift