# How to turn this into a formula

I have this loop, that calculates a multiplier based on level.

``````local LastMulti = 1
for i = 1, 100 do
local Multiplier = 1.5
if i <= 10 then
Multiplier = 2
end

local Multi = math.ceil(LastMulti * Multiplier)
LastMulti = Multi
end
``````

And spits out the intended number I want. However, if I wanna get the multiplier for some at say level 55, I’d have to loop 55 times to get the answer. So is there a way to just plugin a level in and get the multiplier, without needing to loop like this?

2 Likes

I’m not sure if you could do this without loops as you check for the “for loop’s” index and multiply your old values with a multiplier which also changes based on the index. So here’s some shorter code:

``````local LastMulti = 1

for i = 1, 100 do
LastMulti = math.ceil(LastMulti * if i <= 10 then 2 else 1.5)
end
``````

Exponents. To replicate the code you have:

``````local LastMulti = 2^10 * 1.5^90
``````

Or to make it more dynamic:

``````local Level = 12
local Multi = 2^math.min(Level, 10) * 1.5^math.max(0, Level-10)
print(Multi) --> 2,304
``````

This actually does the same thing as what you’re doing because exponentiation is just multiplication in a loop, but it looks cleaner and it should run faster than using your own loop. When you use exponentiation, the computer’s CPU is doing most of the work natively in its own high-performance loop of sorts, and it’s built for this sort of thing.

2 Likes

Exponents is the way to go! Good thinking!!This is why you gotta be good at math to be good at programming (as in using C, C++, C#, etc for more complicated/or for an AP in computer science. Etc)

1 Like