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.6.15 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.14 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.13 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.12 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.11 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.10 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.9 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.8 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.7 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.6 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.5 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.4 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.3 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.2 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.1 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.6.0 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.5.18 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.5.17 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.5.16 tracks/swift/exercises/grains/GrainsExample.swift
trackler-2.0.5.15 tracks/swift/exercises/grains/GrainsExample.swift