[Solved] Math Problem - Sequence/Series

Hi everyone !

I have a big problem with an equation.

In my project, the player have some cash, some element to buy and the possibility to buy these element by pack (x10, x100, xMax…)

The price of an element is calculated by this formula : “initialCost x coeff^itemLvl”
For example, an Iron Suit lvl45 cost : “11 x 1.15^45” so 5.926gold.

I want to make a function which allow the player to buy the max lvl of the item according to the cash he has (each item bought increases its itemLevel by 1)
For example, the player has 200gold. Actually, he’s at Iron Suit lvl 5.
The function show him how many item he can buy, so 5 Iron Suit.

I try to use a “while” iteration to calculating that, but this consume too much performance with many different item.
Also I thought about math Series/Sequence, but I can’t find a solution to match with my situation…

If you have any advice for me so I can find the solution, thanks in advance :slight_smile:

A pleasant day to everyone !

Arigasoft

What you want to do is integrate your cost function. This link looks like a good place to start.

http://www.math.com/tables/integrals/more/b^x.htm

Then you rearrange the formula to solve for the final level.

Edit: Looping through in a generic while loop will probably work just as well if your calculus is weak. It shouldn’t mess with performance much.

2 Likes

Dont do all that

its just level = Mathf.Log(gold/initialCost, Coefficient)
then round down

2 Likes

Little bit of explanation:

x*y^c = sum
y^c = sum/x
c * log y = log (sum / x)
c = log (sum / x) / log y

or

c = logy ( sum / x)

3 Likes

To clarify these are ways of working out the highest cost level that can be purchased. It doesn’t let you factor in the cost of purchasing each level on the way up. Depending on your design this may be a factor.

If I can purchase just level 45 then this formula is great.

If to purchase level 45 I need to buy level 44, 43, 42, 41 … then a solution based on integration is required.

2 Likes

Thanks everyone, I’ll looking this immediately :slight_smile:

And yes BoredMormon, to buy the item lvl45, the player has to purchase the precedent levels.
So the function will show him how many item he can purchase according to his money.

A good day to all,

Arigasoft

Okay, after many attempts I have solved my problem, my math lessons are far far away.
Thanks everyone for your advices :slight_smile:

For an item price as “initialCost x coeff^itemLvl”, and if any item bought increase its level by one, there is the formula to calculate the number of item purchasable :

nbrUpgrade = Mathf.Floor(Mathf.Log(-((cash * (1 - coeff)) / initialCost) + Mathf.Pow (coeff, itemLvl)) / (Mathf.Log (coeff))) - itemLvl;

If anyone is interested in the proof, don’t hesitate to ask me.

A nice day to everyone,

Arigasoft

2 Likes

Today I tried to get the equation for this type of problem, and for now I just got this equation ( using integrals ):
n^3/3 + n^2/2 + n/6 <= sum

I am interested to see how you got that equation.

Hi OneDragutin, sorry for my late answer.

To calculate an integral, you have to find the primitive of your function.

With an equation as “initialCost x coeff^iLvl” (so “C x a^x”) we have this :

1 Like

Blodorag you are a god, thanks so much bro

Glad to have been able to help you :slight_smile: