Skip to content

Commit 0236af6

Browse files
committed
fix getGCD return wrong answer when max(a, b) exceeds magnitude of precision determined by p
1 parent 0e7addc commit 0236af6

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/utils/Utils.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,16 +390,20 @@ class Utils {
390390
// Find the Greatest Common Divisor of two numbers
391391
//
392392
static getGCD(a, b, p = 7) {
393-
let big = Math.pow(10, p - Math.floor(Math.log10(Math.max(a, b))))
394-
a = Math.round(Math.abs(a) * big)
395-
b = Math.round(Math.abs(b) * big)
393+
let factor = Math.pow(10, p - Math.floor(Math.log10(Math.max(a, b))))
394+
if (factor > 1) {
395+
a = Math.round(Math.abs(a) * factor)
396+
b = Math.round(Math.abs(b) * factor)
397+
} else {
398+
factor = 1
399+
}
396400

397401
while (b) {
398402
let t = b
399403
b = a % b
400404
a = t
401405
}
402-
return a / big
406+
return a / factor
403407
}
404408

405409
static getPrimeFactors(n) {

0 commit comments

Comments
 (0)