7

I don't know if I have an odd question, or if this has been asked before (research has not provided me with an answer or question about this). But I have a large amount of summation I need to do, which would be fine if I didn't intend for this to be part of game, which needs to operate at a decent frame rate.

If I were to turn this equation into code, due to the size number may reach in my game this will quickly turn very computationally heavy, and wont function quickly for many users.

In this example below, a is the current amount the user owns, b is the target amount to reach after purchase, c is the cost of the items, making $c_t$ the total cost, and finally beta is the base cost of the items (the price when a = 0)

For example, if a user owns 3 items, and wants to purchase 2 more, their target total would then be 3+2 = 5. The cost for these items due to a scaling price would be 4^4 + 5^5 or a total cost of 3381. Express mathematically, the equation is: $$\sum_{x=a}^b{{\beta}x^x}=c_{a+(a+1)...b}=c_t$$

Re-arranging for the part I actually care about and need to approximate (Beta is static, it never changes once set, so I believe it should be able to be moved around):

$$\sum_{x=a}^b{x^x}=c_{a+(a+1)...b}/{\beta}=c_t/\beta$$

Is it possible to write this summation (or very closely approximate it) in a method that uses less repeated actions, as I intend to use this for a game of mine, but I can't have a for loop run thousands of times per frame and maintain decent frame rates for players, so being able to approximate or rewrite without repeated sum or product would massive free the frame rates of my users, as there would be much less useless usage of computer resources.

The only part I care about is the sum (below) and the possibility of expressing it in such a way that it would be far less computationally repetitive at larger amounts of b.

$$\sum_{x=a}^b{x^x}$$

EDIT: Minor typos, hopefully I've got them all.

  • 7
    How large are $a$ and $b$ typically? I imagine they have to be pretty small to avoid overflow. If they are small enough, you could try precomputing a lookup table. – whpowell96 Apr 10 '24 at 16:15
  • Thats the thing, they can vary alot as this is an incremental game, so anywhere from 0 to thousands. I have a library so that my game can handle larger numbers than the 32 and 64 bit limits thankfully. But because it could range in thousands, I feel it would be quicker to have an approximation than a lookup table. If I cant find an answer I will look into doing that – DaisyLlife Apr 10 '24 at 16:21
  • 1
  • omg tysm, they even have a table of the first 100 numbers in the sequence, all i need to do is adjust by the extra 1 – DaisyLlife Apr 10 '24 at 16:31
  • 1
    ah looks like some beat me to it, OEIS sequence A001923 – DaisyLlife Apr 10 '24 at 16:34
  • 4
    @DaisyLlife I'm curious though, why do you have such a rapidly increasing price scaling in your game? – ioveri Apr 10 '24 at 17:29
  • @ioveri Because the 'item' the user is buying is a powerful upgrade in large amounts, so I wanted it to start very small but escalate very quickly to make it challenging to get higer amounts. I still expect my players to reach these high amounts, and theyll also be losing them quite often with feature i plan to add soon, which is why I need to sum them, so the user can buy in bulk amounts. – DaisyLlife Apr 11 '24 at 00:46
  • I hope your game has a ludicrously high inflation rate tucked into it somehow... – Lee Mosher Apr 11 '24 at 02:08
  • @DaisyLlife ok butwhat if they decide not to buy in bulk instead? I'm pretty sure someone has enough patience to buy a single item several hundred times – ioveri Apr 11 '24 at 10:14
  • 2
    Do you need exact answers? The sum is so heavily tilted towards its final entries that for even moderate $n$, it should be quite fast to approximate it if you can compute $n^n$. – stochasticboy321 Apr 11 '24 at 19:44
  • This doesn't answer your question so I'll leave it as a comment. If you want costs to grow quickly and be easy to compute, you might instead want an exponential cost function like $c \cdot r^{k - 1}$ for the $k$-th item (starting at $1$). This has the nice property that sums are really easy to compute: the total cost of items $a + 1$ through $b$ is $\sum_{k = a + 1}^b c \cdot r^{k - 1} = c \cdot \frac{r^b - r^a}{r - 1}$. If we choose a rate of growth like $r = 2$, then this becomes the very simple $c (2^b - 2^a)$. – crb233 Apr 12 '24 at 00:40

4 Answers4

13

Note that this number grows pretty fast, and already $200^{200}$ doesn't fit into 64-bit floating number.

If you have $0 \leq a, b \leq 200$, then you can just precompute all $40000$ variants and do "calculation" with single lookup.

Standard more efficient (although not necessary in this case) method is to precompute all $\sum\limits_{0}^n$, and use that $\sum\limits_{a}^b = \sum\limits_{0}^b - \sum\limits_{0}^{a - 1}$ - for this you need to precompute linear in $\max(b)$ number of values, and calulation of actual sum will be one subtraction.

mihaild
  • 17,674
  • 2
    oh sweet I forgot thats possible, thanks alot, this means id only need to check two things lol – DaisyLlife Apr 10 '24 at 16:24
  • 3
    Note that if you are using floating point numbers, this subtraction can lead to cancellation errors if the two terms have very different orders of magnitude. – whpowell96 Apr 10 '24 at 16:25
  • im not sure if I should mark this as the solution, as while it does solve why I asked this, it doesnt actually solve the question itself. Am I supposed to mark it as solved? – DaisyLlife Apr 10 '24 at 16:30
  • @DaisyLlife: No, it's generally considered a good idea on Stack Exchange to wait at least a couple of days before accepting an answer, to give people an opportunity to post better answers. – Dan Apr 10 '24 at 16:41
  • alright then, thanks for letting me know – DaisyLlife Apr 10 '24 at 16:43
  • 2
    @whpowell96 I'm pretty sure we don't have to worry about cancellation errors in this case. They matter when the result $A-B$ would be much smaller than either $A$ or $B$; however, in this case, in the calculation $\sum_a^b= \sum_0^b - \sum_0^{a-1}$, the order of magnitude of $\sum_a^b$ will be about the same as $\sum_0^b$, even if $a=b$. – Misha Lavrov Apr 11 '24 at 13:04
8

We have $$\frac{(n-k)^{n-k}}{n^n} = n^{-k}\left(1-\frac{k}{n}\right)^{n-k} \approx n^{-k} e^{-k}$$

As you can see, the magnitude $(n-k)^{n-k}$ decreases extremely fast, and the vast majority terms in the sum are just going to be indiscernible. So you can modify the sum in this way

$$ \begin{align} c_t/\beta &= \sum_{k= 0}^{L}(b-k)^{b-k} + \sum_{k= L+1}^{a}(b-k)^{b-k}\\ &\approx \sum_{k= 0}^{L}(b-k)^{b-k} + \sum_{k= 0}^{a-L-1}(b-L-1)^{b-L-1-k}e^{-k}\\ &\approx \sum_{k= 0}^{L}(b-k)^{b-k} + \frac{e(b-L-1)^{b-L}}{e(b-L-1)-1}\\ \end{align} $$

when $a > L$, where $L$ is a cut-off limit that depends on how big $b$ is. Usually $L = \frac{\text{desired bit precision}}{n \log_2 n}$.If you are not very keen on precision then $$\sum_{k= 0}^{L}(b-k)^{b-k}$$ is enough for $a > L$

EDIT

FYI here's a relative error graph for the simple cut-off method enter image description here

ioveri
  • 2,157
4

AFAIK, there's no closed-form solution to this.

But, defining $f(n) = \sum_{k=0}^n k^k$ (with $0^0 = 1$), doing a brute-force calculation for $ n = 0\dots 143$, and transforming the data until the scatterplot resembles a straight line, I get:

$$\frac{\ln(f(n))}{\ln(n)} \approx 0.999965448338396n + 0.00472477202524318$$ $$f(n) \approx e^{(0.999965448338396n + 0.00472477202524318)\ln(n)}$$

which is reasonably accurate ($<1\%$ relative error) for $n \ge 18$. For smaller $n$, you can use a precomputed lookup table.

Dan
  • 18,262
  • does this have an accurarcy to 5 sf above 100? Im asking the players only ever the first 5 digits, so any error below there will be unnoticable to them unless they look very closely – DaisyLlife Apr 11 '24 at 00:50
  • 2
    Testing myself, im not sure if that has the amount of accuracy I need, I think I'll follow the other suggestions of precomputing these values. Thank you for answering anyways, it is a decent approximation. – DaisyLlife Apr 11 '24 at 00:57
  • 3
    I believe that you don't really need accuracy, but just repeatibility. Are you telling the players the formula behind the price? If the player only knows "each aditional item is more expensive", so it is not relevant if the cost is 35000 or 35001, as long as it is always one or the other. – vals Apr 11 '24 at 10:49
  • players have the option to noy buy in bulk amoints, if they were to not do so then they would could be impacted by significant inaccuracy. If possible id like any inaccurarcy to not be visible to what my players see, which means I need an accuracy of 10^-6 or less, an amount unlikely to effect the first 5 sf which the player is shown when they have large amounts – DaisyLlife Apr 12 '24 at 09:04
2

If you are ok with 99% accuracy, just use $b^b$ for $b\ge 100$, use $(b-1)^{b-1}+b^b$ for $10 \le b < 100$ and for $b<10$, calculate exact.

drahnoel
  • 121