# Are the Advanced and Intermediate solution actually faulty

**URL:** <https://forum.freecodecamp.org/t/are-the-advanced-and-intermediate-solution-actually-faulty/146150>\
**Category:** General\
**Created:** [September 5, 2017, 6:18pm UTC](https://forum.freecodecamp.org/t/are-the-advanced-and-intermediate-solution-actually-faulty/146150 "2017-09-05T18:18:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnL3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johnl3/32/16459_2.png) [@JohnL3](https://forum.freecodecamp.org/u/JohnL3)\
**Post date:** [September 5, 2017, 6:18pm UTC](https://forum.freecodecamp.org/t/are-the-advanced-and-intermediate-solution-actually-faulty/146150/1 "2017-09-05T18:18:55Z")

</div>

> [@freeCodeCamp Challenge Guide: Smallest Common Multiple](https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-smallest-common-multiple/16075):
>
> [Smallest Common Multiple](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple)Problem Explanation The smallest common multiple between two numbers is the smallest number that both numbers can divide into evenly. This concept can be extended to more than two numbers as well. We can first start with finding the smallest common multiple between two numbers. Naively, we can start writing out multiple of each number until we write a multiple that exists from both numbers. An example would be the numbers 3 and 4. The multiples of 3 are 3, 6, 9, 12…

Ok why do i say this when they pass the tests …  
While refactoring some of my code and looking at other ways of doing the smallest common multiple problem and comparing results i realized both of these solutions will give wrong results with certain groups of numbers  
eg if you do smallestCommons([24,43]) it gives 1228641933756837400  
… i worked it out to be 409547311252279200  
in fact you get the same result for range [25,43] … 409547311252279200 in which it dose do correctly.  
Also tried another combination smallestCommons(100,110) 2954551107310803000 (55 times bigger than should be) … i worked it out to be 590910221462160600 …  
dose range [100,109] correctly.  
I checked my results for what i believe to be correct from two versions of smallest common programs i wrote and by also separately on excel.  
Now im presuming there are more ranges that these will fail on … which brings me to another question.

**How to adequately test algorithms like this** that could end up using ridiculously big numbers and could end up being used in the real world doing important tasks.

Now I know its not the end of the world that i think these are in my eyes faulty solutions (pity as the are short clear and fast) … The main thing is how to test a algorithm like this so you can be guaranteed it works for all conditions … and is it possible for something like this.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [September 5, 2017, 7:03pm UTC](https://forum.freecodecamp.org/t/are-the-advanced-and-intermediate-solution-actually-faulty/146150/2 "2017-09-05T19:03:21Z")

</div>

Well, one thing to consider is that there are limits to the precision of numbers on a computer. To make matters worse, Javascript doesn’t have different number types, but lumps them all together. Consider this code snippet:

```auto
var x = Number.MAX_SAFE_INTEGER;
var y = x + 1000;

console.log("x = " + x);
// x = 9007199254740991

console.log("y = " + y);
// y = 9007199254741992

console.log("difference = " + (y-x));
// difference = 1001

```

This is what I get when I run it in codepen on Windows 10 with Chrome. Above a certain number, integers can’t be trusted. That’s in the high 16 digits. Your number 1228641933756837400 is 19 digits.

Any integer calculations that high in JS can’t be trusted. JS is not made for advanced mathematical calculations that high. If you want something that exact, you’ve either got to use a library, design your own solution, or use a language that allows for more precision in large numbers.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:40pm UTC](https://forum.freecodecamp.org/t/are-the-advanced-and-intermediate-solution-actually-faulty/146150/3 "2021-01-17T20:40:24Z")

</div>

Closed on January 17, 2021, 8:40pm UTC


