Whats the fastest way I can do this:

I’m trying to find the maximum amount I can take from a number with it still being a multiple of X.

Heres my code:

local Multiplier = 0
local TakeAway = math.floor(Player.Upgrades.CoinRate.Value / (Player.Upgrades.TerminalVelocity.Value * 3))
local MoneyToGain = (1 * (Player.leaderstats.Rebirths.Value + 1))
local TakeAwayBase = TakeAway
local MoneyGainBase = MoneyToGain

repeat
	wait()
	Multiplier += 1
	TakeAway = math.floor(TakeAwayBase * Multiplier)
	MoneyToGain = math.floor(MoneyGainBase * Multiplier)
	
	if TakeAway > Player.leaderstats.Distance.Value then
		Multiplier -= 1
		TakeAway = math.floor(TakeAwayBase * Multiplier)
		MoneyToGain = math.floor(MoneyGainBase * Multiplier)
		break
	end
until false

(Yes I’m aware that code is bad but I’m not a math genius qwq)

And in case this helps,

  • TerminalVelocity is 100
  • CoinRate is 750
2 Likes

remove the wait() to make the loop repeat faster

1 Like

(Probably very dumb but…)
Couldn’t you multiply X and when you get a number taht equals the number you are substracting from you’ll end everything, if it’s higher take multiply it by 1 number less, and if it’s lower multiply it by one number up?

It’s there so it doesn’t crash

If only thats how that worked

You can literally just do

math.floor(amount / base)

aka floor division
image

1 Like

Can you explain what is your objective with this code? What are you trying to achieve?

Essentially, I have a number and I have to multiply it by X until the number is greater than what the player has, then I revert the multiplier by 1 to then get the maximum number I can remove it from with it still being a multiple of Y.

Did you try my solution yet? Floor division returns the largest integer that when multiplied by the divisor still fits within the dividend

I’m going to try applying it tomorrow, its 3:11AM for me at the moment.
Will return with results.
(Also, when I will try implementing your solution into my code, what do I put as amount and base?)

Also, I have been trying to solve this myself for 2 hours…

Division would Actually be slower than Multiplication, not by a lot, but it is still slower, because I did both for Multiplication and Division:

math.floor(750*.01) -- Faster 
math.floor(750/100) -- Slower

Use math.clamp() for that.Can be found here.

And how will this help? In order to use multiplication in that manner for this case, you will need to get the reciprocal of the divisor, which still involves division. Also, the difference in performance disputed; it could be nanoseconds of difference which is practically meaningless and not worth it at all to make the change.

1 Like

Its actually higher than that, but sure.

Based on your code sample, it would be structured like this:

local Multiplier = math.floor(Player.leaderstats.Distance.Value / TakeAwayBase)

Basically, how many times does TakeAwayBase fit into Distance.

It’s actually less than 1 nanosecond, but sure.

task.wait(4)

local s1 = os.clock()
for i = 1, 1e9 do
	local v = i / 100
end
local e1 = os.clock()

task.wait(4)

local s2 = os.clock()
for i = 1, 1e9 do
	local v = i * 0.01
end
local e2 = os.clock()

print("division", (e1 - s1), "ns per") -- division 5.019934599753469 ns per
print("multiplication", (e2 - s2), "ns per") -- multiplication 4.967759200371802 ns per

That’s a difference of 0.047 milliseconds, or 47 microseconds. Each run does the operation a million times (the loop is from 1 to 1E+6), so 47/1000000 = 0.000047 microseconds, or 47 picoseconds. So yes, it actually is nanoseconds of difference. This may matter to you, but I don’t think the OP intends on running his code millions of times.

Edit: goofed up the units

Doesnt matter, I can still understand it.

Even if it is slightly faster, it wont really matter like you said.
He was asking for the fastest way, and Multiplication would slightly faster (Usually), but there are a few cases where Division is faster, so it isnt always perfect.

Worked perfectly, had to remove some math from the TakeAway though, not a big deal though.
Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.