Given the sum and gcd of two numbers, return those two numbers in ascending order. If the numbers do not exist, return -1, (or return NULL in C).
...
For further actions, you may consider blocking this person and/or reporting abuse
Haskell.
A bit ineffective because it generates all possible solutions instead of stopping when it finds the first solution.
Correct me if I'm wrong, but it doesn't generate all possible solutions. Haskell is lazy, and because the function only returns the head of the list, only the head can be forced into a value. So the evaluation of the list will stop as soon as a value for the head is found, meaning it does stop when the first solution is found.
You might be right, I had forgotten about that tiny detail.
Update: I tried with an infinite sequence to generate the solutions and the program didn't get stuck in an infinite loop, so you are right, only the first element of the sequence is generated.
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 = G * (x + y)
Sum / G should also be integer, otherwise no answer
x + y = Sum / G
We need to take smallest x, so that x, y are relatively prime, so x = 1
=> x = 1 => N = G => M = Sum - G
Answer: GCD, Sum - GCD
Test:
1) Solve(12, 4) = 4, 8; GCD(4, 8) = 4; 4 + 8 = 12; OK
2) Solve(12, 5): 12 % 5 > 0 => No Answer; OK
3) Solve(10, 2) = 2, 8; GCD(2, 8) = 2; 2 + 8 = 10; OK
Reiterating to this logic in Swift:
JavaScript ES6
const solve = (sum,gcd) => {
if(sum%gcd) return -1;
return [gcd,sum - gcd]
}
I like to share the blog very much it is the amazing way play the amazing free solitaire really glad for the share me this hope you like it very much.