Arithmetic/Rational: Difference between revisions
Content added Content deleted
(Add Swift) |
(Make Swift solution generic for use in other tasks) |
||
Line 4,130: | Line 4,130: | ||
} |
} |
||
public struct Frac: Equatable { |
public struct Frac<NumType: BinaryInteger & SignedNumeric>: Equatable { |
||
@usableFromInline |
|||
fileprivate var _num: Int |
|||
var _num: NumType |
|||
@usableFromInline |
|||
⚫ | |||
var _dom: NumType |
|||
@usableFromInline |
|||
⚫ | |||
self._num = _num |
self._num = _num |
||
self._dom = _dom |
self._dom = _dom |
||
} |
} |
||
@inlinable |
|||
public init(numerator: |
public init(numerator: NumType, denominator: NumType) { |
||
let divisor = numerator.gcd(with: denominator) |
let divisor = numerator.gcd(with: denominator) |
||
Line 4,146: | Line 4,151: | ||
} |
} |
||
@inlinable |
|||
public static func + (lhs: Frac, rhs: Frac) -> Frac { |
public static func + (lhs: Frac, rhs: Frac) -> Frac { |
||
let multiplier = lhs._dom.lcm(with: rhs.denominator) |
let multiplier = lhs._dom.lcm(with: rhs.denominator) |
||
Line 4,155: | Line 4,161: | ||
} |
} |
||
@inlinable |
|||
public static func += (lhs: inout Frac, rhs: Frac) { |
public static func += (lhs: inout Frac, rhs: Frac) { |
||
lhs = lhs + rhs |
lhs = lhs + rhs |
||
} |
} |
||
@inlinable |
|||
public static func - (lhs: Frac, rhs: Frac) -> Frac { |
public static func - (lhs: Frac, rhs: Frac) -> Frac { |
||
return lhs + -rhs |
return lhs + -rhs |
||
} |
} |
||
@inlinable |
|||
public static func -= (lhs: inout Frac, rhs: Frac) { |
public static func -= (lhs: inout Frac, rhs: Frac) { |
||
lhs = lhs + -rhs |
lhs = lhs + -rhs |
||
} |
} |
||
@inlinable |
|||
public static func * (lhs: Frac, rhs: Frac) -> Frac { |
public static func * (lhs: Frac, rhs: Frac) -> Frac { |
||
return Frac(numerator: lhs._num * rhs._num, denominator: lhs._dom * rhs._dom) |
return Frac(numerator: lhs._num * rhs._num, denominator: lhs._dom * rhs._dom) |
||
} |
} |
||
@inlinable |
|||
public static func *= (lhs: inout Frac, rhs: Frac) { |
public static func *= (lhs: inout Frac, rhs: Frac) { |
||
lhs = lhs * rhs |
lhs = lhs * rhs |
||
} |
} |
||
@inlinable |
|||
public static func / (lhs: Frac, rhs: Frac) -> Frac { |
public static func / (lhs: Frac, rhs: Frac) -> Frac { |
||
return lhs * Frac(_num: rhs._dom, _dom: rhs._num) |
return lhs * Frac(_num: rhs._dom, _dom: rhs._num) |
||
} |
} |
||
@inlinable |
|||
public static func /= (lhs: inout Frac, rhs: Frac) { |
public static func /= (lhs: inout Frac, rhs: Frac) { |
||
lhs = lhs / rhs |
lhs = lhs / rhs |
||
} |
} |
||
@inlinable |
|||
prefix static func - (rhs: Frac) -> Frac { |
prefix static func - (rhs: Frac) -> Frac { |
||
return Frac(_num: -rhs._num, _dom: rhs._dom) |
return Frac(_num: -rhs._num, _dom: rhs._dom) |
||
Line 4,189: | Line 4,203: | ||
extension Frac { |
extension Frac { |
||
@inlinable |
|||
public var numerator: |
public var numerator: NumType { |
||
get { _num } |
get { _num } |
||
set { |
set { |
||
Line 4,199: | Line 4,214: | ||
} |
} |
||
@inlinable |
|||
public var denominator: |
public var denominator: NumType { |
||
get { _dom } |
get { _dom } |
||
set { |
set { |
||
Line 4,217: | Line 4,233: | ||
extension Frac: Comparable { |
extension Frac: Comparable { |
||
@inlinable |
|||
public static func <(lhs: Frac, rhs: Frac) -> Bool { |
public static func <(lhs: Frac, rhs: Frac) -> Bool { |
||
return lhs._num * rhs._dom < lhs._dom * rhs._num |
return lhs._num * rhs._dom < lhs._dom * rhs._num |
||
Line 4,224: | Line 4,241: | ||
extension Frac: ExpressibleByIntegerLiteral { |
extension Frac: ExpressibleByIntegerLiteral { |
||
public init(integerLiteral value: Int) { |
public init(integerLiteral value: Int) { |
||
self._num = value |
self._num = NumType(value) |
||
self._dom = 1 |
self._dom = 1 |
||
} |
} |
||
} |
} |
||
for candidate in 2..<1<<19 { |
for candidate in 2..<1<<19 { |