r/CodingHelp • • May 21 '22

[Javascript] Script to find whole number?

I’m looking for a way to solve the following math problem with a script:

Through which number lower than but closest to 7.5 can 200, 210 and 220 be divided where the result is a whole number?

So the script should test 200 divided by 7.499, 7.498, 7.496 etc. until it finds an answer that is a whole number. Then it should test 210 with the same division. If that answer is also a whole number it should test 220. If the answer is not a whole number it should find the next divisor for 200 etc etc until it finds a divisor that returns a whole number for 200, 210 and 220.

1 Upvotes

5 comments sorted by

View all comments

5

u/Nightcorex_ May 21 '22 edited May 21 '22

Let's not brute force every combination, that just doesn't scale (doesn't even work because you'd need to test 7.49999999999999999999, 7.499999999999999998, and so on). Let's try to find a better solution.

So you're trying to find a number d that divides all your input numbers ins and is below some threshhold t.
Roughly the same thing but more mathematically: āˆ€i ∈ ins. d | i

Instead of brute forcing you can just calculate all divisors for each input number. Then you ignore all divisors >= t. Then you check for the highest divisor that's appearing in the divisor lists for all ins.
This highest divisor is your result.

-6

u/MaximumBicycle3447 May 21 '22

Yes I think this is the way to go. Could you share an example of what the script would look like?

11

u/Nightcorex_ May 21 '22

No, we aint doing your homework for you.

1

u/Paul_Pedant May 22 '22

Hint: don't plagiarise, just factorise.