loading...

re: Daily Challenge #130 - GCD Sum VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Assume 0 < N <= M Sum = N + M GCD = G, so that N = x * G, M = y * G and x, y are relatively prime integers > 0 => Sum = x * G + y * G =...
 

Reiterating to this logic in Swift:

import UIKit

func solve(sum: Int, hcf: Int) -> (Int, Int)? {
    let x = 1
    var y: Int!

    y = sum - hcf

    let verificationInt: Float = Float((hcf + y)/hcf)
    let verificationSum = Int(verificationInt + 1)
    if verificationSum % hcf == 0 {
        return (hcf*x, y)
    }
    return nil
}

if let solution = solve(sum: 12, hcf: 5) {
    print(solution)
} else {
    print(-1)
}
code of conduct - report abuse