9 ms·
Analogous code in J, /:~ s -. p [ p =: s*/s [ s=: 2+i.48 An exercise for numpy, test that GCD(x,y) * LCM(x,y) = x*y using 1000 random numbers in the range 0
by bruturis 2y ago
Analogous code in J,
/:~ s -. p [ p =: s*/s [ s=: 2+i.48
An exercise for numpy, test that GCD(x,y) * LCM(x,y) = x*y using 1000 random numbers in the range 0..99 for x e y.
test =: (* = *. * +.) & ?
*./ test~ 1000 # 100
- cl3misch 2y agoThanks, that was fun. def d(x): N = arange(1, x + 1); return N[x % N == 0] def m(x, n): return x * arange(1, n + 1) def gcd(x, y): return max(set(d(x)) & set(d(y))) def lcm(x, y): return min(set(m(x, y)) & set(m(y, x))) def test(x, y): return gcd(x, y) * lcm(x, y) == x * y all([test(x, y) for (x, y) in randint(1, 100, (1000, 2))]) # True I am not a good golfer. Now I want to look at the codegolf stackexchange for this...
- deleted 2y ago[deleted]
- bear8642 2y agoEquivalently in APL: ⎕io←0 ⍝ to match J test ← (× = ∨ × ∧) ∘ ? ∧⌿ test⍨ 1000⍴100