Calculating how many ranks a user can afford

Yeah it was midnight when I came up with that solution and just as I turned my PC to go to sleep I realized that they would need to be able to change the first index.

2 Likes

This doesn’t seem to work unless I have the x wrong.

With a = 10, d = 4, price (x) = 100 it returns 8. Which that part of the sequence is 68 and if you add the sum of the 1-8th sequences it is 432. Not < 100

1 Like

Here is a new function that allows you to set the first index as well.

function solveSeries(total,delta,firstIndex)
	local sqrtPart = math.sqrt(4*firstIndex*firstIndex - 4*firstIndex*delta + delta*delta + 8*delta*total)
	return (sqrtPart - 2*firstIndex + delta) / (2*delta)
end

For example,

1, 4, 7, 10, 13 = 35

a1 = 1, delta = 3, total = 35

solveSeries(35,3,1) = 5, the amount of terms in that sequence that totals to 35.

Also you should floor the result of the function encase the user can’t perfectly afford a whole number amount of rebirths.

I would like to respond to this for anyone with the future problem. I also will rename the topic for a better naming to help search terms for people with a similar problem.

The Solution:

The solution isn’t basic and I learned to always sleep and give some time and thought as this could have all been avoided for better performance.

@AC_Starmarine’s solution will work if you have a = d (base = distance)

You can directly solve the roots with quadratic formula, even in cubic case there exists a formula, for anything more than that you need some polynomial root solver which likely more expensive.

0.109 microseconds for one check
0.141 microseconds using the quadratric formula as of for numerical root solvers they do converge rapidly so they may be faster if there need to be a lot of checks

Decided solution

I decided on summing them AS there currency increases. Meaning the first time is O(n) and most likely from then O(1) in most cases in my game.

I just compare and add to an nIndex and after client validations for the server to validate it just has to calculate the nIndex-th of the sequence

I am confused because my new function can have the base value and the delta be different, and both use the quadratic formula.

My function also runs on my PC around 0.45 microseconds per iteration average over 10 million runs.

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