Rebirths math problem

Hello, I would like to add a rebirth system to my game :
https://www.roblox.com/games/5329202758/BEACH-Hatching-Simulator-X?refPageId=aba80429-91e7-46e0-a33a-c3feb8046590#!/game-instances

But I don’t know the exact math equation without using a for loop to make for multiple rebirths. The price would go up depending on the actual rebirths.

E.G. ;

local rebirthsToDo = 20
local actualRebirths = 3

for i = 0, rebirthsToDo do
    local price = (actualRebirths * 10000)
    rebirth(Price) -- The actual rebirths would increase so, the price would go higher.
end

This would definitely work but I want to be able to do it with high numbers, like Millions and I can’t make a for loop with 1Mil.

Thank you!

Note : I may not be able to answer you rn, I’m going to sleep. I’ll see all messages tomorrow, thank you for your understanding!

All that code sample does is call the rebirth function with the argument 30,000 three times.

Could you elaborate on what the goal is? From my understanding, you want to convert some recursive mathematical function into a simple function, but it isn’t clear what the original recursive function is.

I wrote something similar for levels and experience. You can accomplish this without loops or recursive functions with arithmetic series and the quadratic formula.

1 Like

I want to see the next rebirth price and add it to the current price, meaning that if I want to rebirth x10, I’ll calculate the price of first rebirth + price of second rebirth until 10, not one price x10.

E.G: 2 rebirths would do 30k, not 20k.

That’s a little confusing.
So first rebirth is 10,000
2nd is 30,000
3rd is 30,000 + 20,000?
4th is 50,000 + 30,000?
5th is 80,000 + 50,000?

Actually, yea, its confusing. Here’s correct one:
1 - yourActualRebirths x 10k
2 - yourActualRebirths x 10k
Etc.

But since you rebirthed 1 time, yourActualRebirths would be + 1 so the 2nd rebirth would cost more.

My wording here is fairly confusing so I solved it for you. All you have to do is plug in the values to the script in my post above. start and increment would both be 10000. a and b defined below.

a1 = 10000
d = 10000
s(n) = n / 2 [2a1 + (n - 1) d]
s(n) = n / 2 [20000 + (n - 1) 10000]

s(n) = n / 2 [20000 + 10000n - 10000]
= 10000n + 5000n^2 - 5000n	Divide all by two and multiply by n (n^2 in the case already multiplied by n)
= 5000n^2 + 5000n

Apply quadratic equation.

5000n^2 + 5000n - s(n) = 0
^^^^      ^^^^
a         b

Let me re-explain. Okay, so, you have 1 rebirth. Each rebirth costs your actual rebirth * 10k in this case. So, if you had 2 rebirths, it would cost 20k, 3 rebirths-30k etc.

Now, what I want is to make massive rebirths directly, since rebirthing makes you lose everything you have. So like, I want into the process of finding price, if player chose to rebrith 5 times, it’ll count like that :

local RebirthsIHave = 1

local HowManyRebirths = 5
local price = 0

for i = 1, HowManyRebirths do
    price = price + RebirthsIHave * 10000 -- The price will increase as we rebirth
    RebirthsIHave = RebirthsIHave + 1
end

This would work but I don’t want to use a for loop, I want to get it done instantly, calculating the price only 1 time, meaning it will also calculate the price of the next rebirth until 5 in this case.

For one to the next
cost = (currentRebirths+1)*10,000]

Cost series

1 => 10k
2 => 20k

50 = 500k
51 = 510k

For multiple
cR = currentRebirth
nR = numberOfRebirths
cost = (cR*nR + nR(nR+1)/2)*10000
or = nR/2 * (2*cR + nR +1) *10000

complements of @Astr0Derp 10000/2
cost = 5000*nR*(2cR + nR + 1)

can’t make it much prettier
this is say, you have 6
and want to buy 5 (7,8,9,10,11)
the cost is 450,000

1 Like

Sorry for all the edits… I didn’t understand what you were trying to do with the RebirthsIHave at first.
The formula is basically this. Will see about simplifying it.

local scale = 10000
local final_price = HowManyRebirths * (HowManyRebirths+1) * scale/2 + scale * ((RebirthsIHave-1)*(HowManyRebirths-1)+ RebirthsIHave-1)

Edit: Here it is simplified

scale = 10000
final_cost = 0.5*scale * HowManyRebirths * (HowManyRebirths + 2*RebirthsIHave - 1)

So pretty much
First Rebirth is 10,000 = 10,000
Second Rebirth is 20,000 + First Rebirth = 30,000
Third Rebirth is 30,000 + second rebirth = 60,000
then 100,000
etc

This is really simple :stuck_out_tongue:

local rebirthsToDo = 20
local startCost = 10000
local rebirthCost = 10000

for i=1, rebirthsToDo do
   rebirthCost = rebirthCost + startCost*i
end

What I would do is make an algorithm for the price to go up. I don’t see a reason for you to use a for loop, just make an algorithm that handles it.

Demonstration:

local PRICE_INCREASE_PER_REBIRTH = player.leaderstats.Rebirths.Value + 1000 / ( player.leaderstats.Rebirths.Value * 5)

This add’s the player’s rebirth value by a thousand and then divides it by the amount of rebirths the player multiplied by 6. ( It doesn’t increase the rebirths, just an algorithm )

Yes, that’s simple but what if player wants to do over a million rebirths? The script would crash.

A million rebirths at once?!?!

Yea, like tapping simulator? That’s why I dont want to use a for loop.

Well I highly doubt that anyone would do that many rebirths at once, however under that case, you could use that script to log the price of each rebirth
Create a table,
Put something like this into the loop

table.insert(rebirthCostTable, rebirthCost)

Then you could call it, otherwise I would look into the algorithms that others have posted here :stuck_out_tongue:

Why are you guys making it this complicated, just use a exponential scale. Any equation of the form y=n^x will work fine, where n is the steepness of the function and x would be the amount of rebirths which you can track by your own means. Using a for loop for any sort of math that isn’t integration(but even integration in most cases) is a massive sign you’re doing something terribly wrong.

he wants the total price of multiple iterations of buying a rebirth not just the price of a specific rebirth

Yes, exactly, I want to be able to calculate the next price and add it to the current one.

Then just integrate whatever formula (exponential formula in this case) for calculating the price and do the exact same thing.