// // IntExtensions.swift // EZSwiftExtensions // // Created by Goktug Yilmaz on 16/07/15. // Copyright (c) 2015 Goktug Yilmaz. All rights reserved. // import Foundation extension Int { /// EZSE: Checks if the integer is even. public var isEven: Bool { return (self % 2 == 0) } /// EZSE: Checks if the integer is odd. public var isOdd: Bool { return (self % 2 != 0) } /// EZSE: Checks if the integer is positive. public var isPositive: Bool { return (self > 0) } /// EZSE: Checks if the integer is negative. public var isNegative: Bool { return (self < 0) } /// EZSE: Converts integer value to Double. public var toDouble: Double { return Double(self) } /// EZSE: Converts integer value to Float. public var toFloat: Float { return Float(self) } /// EZSE: Converts integer value to CGFloat. public var toCGFloat: CGFloat { return CGFloat(self) } /// EZSE: Converts integer value to String. public var toString: String { return String(self) } /// EZSE: Converts integer value to UInt. public var toUInt: UInt { return UInt(self) } /// EZSE: Converts integer value to Int32. public var toInt32: Int32 { return Int32(self) } /// EZSE: Converts integer value to a 0.. { return 0..) -> Int { let delta = within.upperBound - within.lowerBound return within.lowerBound + Int(arc4random_uniform(UInt32(delta))) } } extension UInt { /// EZSE: Convert UInt to Int public var toInt: Int { return Int(self) } /// EZSE: Greatest common divisor of two integers using the Euclid's algorithm. /// Time complexity of this in O(log(n)) public static func gcd(_ firstNum: UInt, _ secondNum: UInt) -> UInt { let remainder = firstNum % secondNum if remainder != 0 { return gcd(secondNum, remainder) } else { return secondNum } } /// EZSE: Least common multiple of two numbers. LCM = n * m / gcd(n, m) public static func lcm(_ firstNum: UInt, _ secondNum: UInt) -> UInt { return firstNum * secondNum / UInt.gcd(firstNum, secondNum) } }