Skip to content
Advertisement

Efficient way for checking number 3^x * 5^y

I want to check if number, based on lower and upper bound, has prime divisors only 3 and 5 and number should be multiplication of power of 3 and power of 5. My current solution is this. I want to optimize it, since checking powers with for loops isn’t good way in my opinion. Thanks in advance.

JavaScript

Based on comments I think this is the best way:

JavaScript

Advertisement

Answer

I want to optimize it, since checking powers with for loops isn’t good way in my opinion.

Over a range of random numbers, we improve its speed by doing:

JavaScript

Or we can use a nested loop and combine both divisions into one:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement