Sha256: d2f1a2d9972838209f440c437c3f82c42f5135b576e6f57fbe1cba48e3c86b5b
Contents?: true
Size: 905 Bytes
Versions: 266
Compression:
Stored size: 905 Bytes
Contents
import Foundation 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
266 entries across 266 versions & 1 rubygems