Posted By

rwczippy on 06/18/08


Tagged

javascript lcm gcm


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

rwczippy


LCM/GCM Javascript


 / Published in: JavaScript
 

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

  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. }

Report this snippet  

You need to login to post a comment.