Advertisement
zvoulgaris

Coprimes Drill

Jan 17th, 2020
2,445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 0.70 KB | None | 0 0
  1. function gcd(x::Int64, y::Int64)
  2.     while (x > 0) && (y > 0)
  3.         if x < y
  4.             y = y % x
  5.         else
  6.             x = x % y
  7.         end
  8.     end
  9.  
  10.     return max(x, y)
  11. end
  12.  
  13. AreCoprime(x::Int64, y::Int64) = (gcd(x, y) == 1)
  14.  
  15. function main(lo::Int64, hi::Int64)
  16.     lo, hi = minmax(lo, hi)
  17.     if (lo > 1e18) || (hi > 1e18) || (hi - lo > 50); error("lo and hi parameters are invalid!"); end
  18.  
  19.     for x = lo:(hi-2)
  20.         for y = (x+1):(hi-1)
  21.  
  22.             if AreCoprime(x, y)
  23.                 for z = (y+1):hi
  24.                     if AreCoprime(y, z) && !AreCoprime(x, z); return (x, y, z); end
  25.                 end
  26.             end
  27.         end
  28.     end
  29.  
  30.     return (NaN, NaN, NaN)
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement