Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

gcd() ... wrong answer, lcm() hangs

May
12,834
163
This is correct.
Code:
v:\> echo %@eval[gcd(2546068882500000000 99999900000000000)]
357142500000000

If I multiply both numbers by 10, their GCD should be multiplied by 10. But this is not correct.
Code:
v:\> echo %@eval[gcd(25460688825000000000 999999000000000000)]
3584

And this puts TCC in an uninterruptible, 100% CPU usage state.
Code:
v:\> echo %@eval[lcm(25460688825000000000 999999000000000000)]
 
And here's a peculiar one. The correct answer is 10**18, but I get 2**18.
Code:
v:\> echo %@eval[gcd(%@eval[10**18] %@eval[10**19])]
262144
 
Note that @EVAL was necessary in my last example. The arguments to gcd() are not expanded, and, as mentioned in another thread, variable names without a leading '%' are not expanded.
 
WAD - you're overflowing the 64-bit integer arguments & result. I've added an overflow check to lcm in build 47 to prevent it from looping.

Those operators could (with substantial effort) be rewritten to use BCD instead, but I doubt you actually need it.
Actually, I needed it. How do you think I discovered it?
 
LCM's overflow check apparently works. GCD can also hang TCC and would seem to need a similar check. THis will do it.
Code:
echo %@eval[gcd(1111111111118077966350999 1111111111118077966350888)]
 

Similar threads

G
Replies
8
Views
3K
Back
Top