Sunday, 28 May 2017

Error finding LCM in javascript with larger numbers

Currently trying to find LCM using js for a course. It seems to work no problem, but when I submit it I am getting a failed result for input 226553150 1023473145 and that is returning 46374212988031340 when it should be returning 46374212988031350 Thanks for anything you can do!

var readline = require('readline');

process.stdin.setEncoding('utf8');
var rl = readline.createInterface({
input: process.stdin,
terminal: false
});

function lcm(a,b) {
   return((a*b)/gcd(a,b))
}

function gcd(a,b) {
   if(b == 0){
    return a;
   }
   return gcd(b,(a%b));
}




rl.on('line', (line) => {
  var a = parseInt(line.toString().split(' ')[0]);
  var b = parseInt(line.toString().split(' ')[1]);
  console.log(lcm(a,b));
  rl.close();
})



via McKinley Forbes

No comments:

Post a Comment