LCM/GCM Javascript


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function gcm(a, b) { return ( b == 0 )?(a):( gcm(b, a % b) ); }function lcm(a, b) { return ( a / gcm(a,b) ) * b; }function lcm_nums(ar) {
  2. if (ar.length > 1) {
  3. ar.push( lcm( ar.shift() , ar.shift() ) );
  4. return lcm_nums( ar );
  5. } else {
  6. return ar[0];
  7. }
  8. }

URL: http://www.ideashower.com/our_solutions/leastgreatest-common-mulitple-lcmgcm-in-php-and-javascript/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.