Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. function gcd(a, b) { //greatest common divisor
  2. if(isNaN(a) || isNaN(b)){
  3. //throw new Error("a or b should be a number");
  4. //console.log('a or b should be a number');
  5. return false;
  6. }
  7. if(Math.floor(a) !== a || Math.floor(b) !== b){
  8. //throw new Error("a or b should be an integer");
  9. //console.log('a or b should be an integer');
  10. return false;
  11. }
  12.  
  13. while(b !== 0){
  14. let r = a % b;
  15. a = b;
  16. b = r;
  17. }
  18. return (a<0) ? -a : a;
  19. }
  20.  
  21. function lcm(a, b) { //least common multiple
  22. return Math.abs(a)*Math.abs(b)/ gcd(a, b);
  23. }
  24.  
  25. function smallestMult(n) {
  26.  
  27. let myLcm = 1;
  28. let i;
  29. for(i=2; i<=n; i++){
  30. myLcm = lcm(myLcm, i);
  31. }
  32.  
  33. console.log(myLcm);
  34. return myLcm;
  35. }
  36.  
  37. smallestMult(20);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement