How to find the maximum value for a quadratic sequence?

Hello,
I’m using a quadratic sequence to apply diminishing returns to the progression system (essentially it means that as you keep progressing, it gets more and more difficult at an unfixed, non-linear rate).
The formula for the quadratic sequence is this:
image
where n is “reqage” in my code. You read newspapers to become older, and age is a datastore value.
Basically, you need to “age up” using news (a currency).
I want two things to happen.

  • The player already has an age. They want to reach another age. Thus, I want to find Tn (the cost in news) for them to age up to this new age.
  • A “max” function - finds the maximum amount the user can age up by that they can afford.
    Here’s the code for the function that gives you the cost at a specific reqage.
local function calculate(reqage)
		local quad = (12.5 * (reqage ^ 2))
		local linear = (-12.5 * (reqage))
		local constant = 25
		return (quad + linear + constant)
	end

Any help would be greatly appreciated.

bump bump bump (pls im so bad at maths ): )

Soooo your function isn’t strictly increasing for n >= 0 and it also doesn’t cross either (0, 0) or (1, 0) which seem like weird choices to me.

It’d seems more natural to have it cross (1, 0) to mean “news for level 1 is 0”, or generally “f(x)=age(news)”. I mean it’s your game and the math works out either way, so w/e. But here’s a function with those properties:

You’d implement it like so:

function ageToNews(age)
    return 12.5 * (age - 1)^2 + 12.5 * (age - 1)
end

… which is perfectly equivalent to yours (except it’s a different equation).

You don’t need their current age, just their current news. The news remaining to each some other age is then

local targetAge = currentAge + 1
local newsToTargetAge = newsToAge(targetAge) - newsToAge(currentAge)

You’ll need to inverse function, newsToAge. WolframAlpha can compute that for you: Inverse of (25/2)(x - 1)^2 (25/2)(x - 1) - Wolfram|Alpha

It gives the result 1/2 ± 1/10 sqrt(8 x + 25), which can be implemented as

function newsToAge(age)
    return 0.5 + 0.1 * math.sqrt(8 * age + 25)
end

Although you’ll want to round down to get a whole number for age.

You can test these two functions like so:

for age = 1, 5 do
    print(ageToNews(age), age == newsToAge(ageToNews(age)))
end

which should print true for all ages.

1 Like

ok wow
that is literally so cool!
I am gonna take some time and try and figure out what to do using all this awesome stuff!
super cool
yes
thank

1 Like

So i was looking through it, and I’m sort of confused.
I’d like it so that if you are 1 year old, then it’d cost less to age up by 1, than if you were 4 years old. Not sure if this is what’s going on here (but then again im stupid so idrk)

To find the cost to gain 1 age, you take the cost of the next age and subtract from that the cost of the current age.

So

function getAgeUpCost(currentAge, years)
    local agedUpAge = currentAge + years
    local agedUpCost = ageToNews(agedUpAge)
    local currentAgeCost = ageToNews(currentAge)
    return agedUpCost - currentAgeCost
end

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