Avatar
@swift-5.4.3 import Foundation func original(n: Int, x: Int, y: Int) -> Int { var (n, x, y) = (n, x, y) for _ in 2 ..< n { let t = (x + y) % 100 x = y y = t } return y } struct Mat { var m11: Double var m12: Double var m21: Double var m22: Double init(_ m11: Double, _ m12: Double, _ m21: Double, _ m22: Double) { self.m11 = m11 self.m12 = m12 self.m21 = m21 self.m22 = m22 } static func * (lhs: Mat, rhs: Mat) -> Mat { Mat( lhs.m11*rhs.m11 + lhs.m12*rhs.m21, lhs.m11*rhs.m12 + lhs.m12*rhs.m22, lhs.m21*rhs.m11 + lhs.m22*rhs.m21, lhs.m21*rhs.m12 + lhs.m22*rhs.m22 ) } } func mat(n: Int, x: Int, y: Int) -> Double { let S = Mat( -(1+sqrt(5))/2, -(1-sqrt(5))/2, 1, 1 ) let Jn = Mat( pow((1-sqrt(5))/2, Double(n-2)), 0, 0, pow((1+sqrt(5))/2, Double(n-2)) ) let St = Mat( -1 / sqrt(5), (5-sqrt(5)) / 10, 1 / sqrt(5), (5+sqrt(5)) / 10 ) let m = S * Jn * St let num = Double(x) * m.m12 + Double(y) * m.m22 return num - floor(num/100)*100 } print(original(n: 60, x: 2, y: 3), mat(n: 60, x: 2, y: 3)) print(original(n: 80, x: 2, y: 3), mat(n: 80, x: 2, y: 3))