0

Right now we have a java method calculating a number, it is called prestige, the prestige cost increases each level, the code is a loop, we're trying to find the max prestige an user can reach with all their current balance.

The loop takes a long time, i believe we could somehow generate an formula which would be way way faster than the loop.

The actual calculation of an prestige cost is: cost + cost/(100 * prestige)

We have the variable balance, which is the actual balance of the user, and we need the balance = sum of prestige costs, until a level where the sum of costs is higher than the balance,

at first I thought about something related to An? do you guys have an idea?

        double balance = EnchantedPrison.getEcon().getBalance(player);
        while (true){
            final double prestigeCost = PrestigeUtils.getPrestigeCost(cost, prestige + prestiges.getFirst() + 1) + prestiges.getSecond();
            //Bukkit.getLogger().log(Level.INFO,"PrestigeCost -> " + prestigeCost);
            if (!EnchantedPrison.getEcon().has(player, prestigeCost)){
                break;
            }
            prestiges.setSecond(prestigeCost);
            prestiges.setFirst(prestiges.getFirst() + 1);
        }
        return prestiges;

I've gotten to the point where I achieved a "formula", for it, but I'm having a hard time getting it from summation to an actual pack of variables that I could use on my program. here it is:

https://i.stack.imgur.com/Nm9FI.png

1 Answers1

0

Based on the image you posted, the following is the formula you're trying to simplify:

$C = \sum_{m = p}^t \frac{x}{100m} = \frac{x}{100p} + \frac{x}{100(p+1)} + \ldots + \frac{x}{100t}$

We can take out a common factor, giving us:

$C = \frac{x}{100}\left(\frac{1}{m} + \ldots + \frac{1}{t} \right)$

And what's in the brackets relates to something called the harmonic numbers, which are given as $H_n = 1 + \frac{1}{2} + \ldots + \frac{1}{n}$. In fact, $C = \frac{x}{100}(H_t - H_{m-1})$.

Unfortunately, there's no nice closed formula for the harmonic numbers, and so there isn't one for your cost function either. You can approximate it with some integrals, or as $H_n \sim \ln n + \gamma + \frac{1}{2n}$ where $\ln$ is the natural logarithm and $\gamma \approx 0.577$ is the Euler-Mascheroni constant, and that might be good enough for your purposes.

ConMan
  • 24,300