**My Solution in Ruby: **

To check if n has any divisors, you only need to check up to the square root of n. For example, if n = 24, then its divisors are:

1, 2, 3, 4

24, 12, 8, 6

This means if you’re checking 1, 2, 3, 4, you have also already checked their counterparts, which are 24, 12, 8, 6. Thus, you can set the upper limit of the range to be the square root of 24 (4.899) to shorten the running time.

Advertisements