I’ve been working on the Smallest Common Multiple challenge for the past few days, and I’ve finally made some progress. I was originally trying to implement a complex solution using an LCM algorithm and a recursive function. I put that on hold and decided I should try and get a solution working with a simple brute force program first.
I’m hitting a wall with my simple brute force solution.
Here’s my code: https://pastebin.com/547zHRK9
What I’m trying to do
- I’m creating an array with the entire range of numbers between the provided two digits.
- I’m then sorting the array from largest to smallest.
- I then have a while loop checking the smallest multiple of the first two numbers against the other numbers in the array. I’m getting this multiple from a custom function. If the multiple does not divide evenly with one of the range numbers, the custom function is called again (but this time it will exclude the previous smallest multiple), and the next smallest multiple of the two first numbers is checked against the range of numbers (this is supposed to be repeated until a multiple is found that divides evenly with all the range numbers).
This isn’t happening as planned.
The program works correctly with some number ranges (such as [1, 3], [1, 4], and [1, 6]), but not with others ([1,5] gets stuck in an infinite loop).
Can anyone give me a nudge in the right direction? Also, is there a better solution that using while (true) loops, like I have in the current code?
Update: The assistance provided by @honmanyau helped solve the infinite loop issue. My code still doesn’t pass all checks, but the issue I was asking about above has been solved.
Here’s my code with the fixed secondary function call (finding the smallest common multiple of two numbers, and excluding an included third argument): https://pastebin.com/JLgZXPEF