How to write this math formula in Lua? - Gideros Forum

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Top Posters

• ar2rsawseen 6991
• SinisterSoft 3857
• hgy29 2574
• keszegh 2243
• antix 2058
• OZApps 1983
• totebo 1764
• oleg 1617
• hgvyas123 1412
• techdojo 1321
• phongtt 1029
• Mells 1024
• MikeHart 1020
• john26 995
• GregBUG 962
• pie 962
• talis 912
• Scouser 898
• MoKaLux 887
• Apollo14 881

# How to write this math formula in Lua?

edited August 7
Hi guys!
Do you know how to express these 2 math formulas in Lua?
(They're from this old gamasutra article on clicker games: https://www.gamasutra.com/blogs/AnthonyPecorella/20161013/282422/The_Math_of_Idle_Games_Part_I.php )
While putting all this together, I derived two very useful formulas that will save you from brute-forcing with some lengthy for-loops. The first will calculate the cost of bulk-buying generators, the second will calculate the max generators you can buy with your current funds. These will only work for simple exponential growth that doesn't have shifting costs or exponents, so make sure your particular application works for it. For both of these, the variables are:

$n$ = the number of generators to buy
$b$ = the base price
$r$ = the price growth rate exponent
$k$ = the number of generators currently owned
$c$ = the amount of currency owned

Formula 1:
$cost = b * \frac{r^k(r^n-1)}{r-1}$

Formula 2:
$max = floor(log_r(\frac{c(r-1)}{b(r^k)}+1))$
Many thx! > Newcomers roadmap: from where to start learning Gideros
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)﻿

• edited August 7 Accepted Answer
My guess
 local log = math.log   local function myLog(v, base) return log(v) / log(base) end   cost = b * (r ^ k * (r ^ n - 1)) / (r - 1) max = ( myLog( (c * (r - 1)) / (b * (r ^ k)) + 1, r ) ) // 1
\frac: And log_r is a regular log with "r" base.

Likes: Apollo14

+1 -1 (+1 / -0 ) Share on Facebook
• edited August 8 Thx a lot @rrraptor ! I guess it's what I need!

p. s. I've also found out that original ugly expression can be made more readable with https://www.mathjax.org/#demo
(they've mentioned it in the article, but I didn't notice at first)
> Newcomers roadmap: from where to start learning Gideros
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)﻿