Skip to content
Advertisement

Using a for-loop and range function vs a while-loop

I’m looking for a function like range except that the step is a fraction of the previous number generated. So if the fraction was 99/100 the set of numbers might be something like this: 100, 99, 98.01… 0.001

Would this be more efficiently done with a for-loop and range-like function or with just a while-loop?

The code I have so far:

stop = .001
current = 100
while current > stop:
    #code
    current *= 0.99

Advertisement

Answer

You can use np.geomspace:

np.geomspace(100, 12.5, 4)

You can use np.arange with direct exponentiation:

12.5 * 2**np.arange(3, -1, -1)

np.logspace is also an option:

100 * np.logspace(0, -3, 4, base=2)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement