Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Summation equation in Java?

Summation Equation

I would like to know how I would go about writing this summation equation in java. But, the trick is, I need the summation to be equal to an amount.

x= Total Loss Streak amount
sb= Starting Bet
m= multiplier

The whole equation will equal to the current amount of currency in one's account. The amount of times the summation can complete itself while adding up needs to be less than or equal to the amount of currency in ones account.

Fyi, this is for a dicebot that work's on peerbet.org and I want to be able to show the user how many times he can loose in a row without wasting all his money.

If this question is bad, please do not answer it and let me delete it. Also, it thought the middle part was code, so I had to put it as such or it wouldn't let me post.

like image 965
michaeladair Avatar asked Sep 24 '26 21:09

michaeladair


1 Answers

Renaming sb to just b. This is just a sum of a geometric progression

sum = b * (m<sup>2</sup> - m<sup>x + 1</sup>) / (1 - m)

In Java, you can write:

return b * (m * m - Math.pow(m, x + 1)) / (1 - m);

This will be considerably faster than using a loop, although you must check that m is not 1.

If you want to solve for x given a sum S then a rearrangement of the formula gives the following Java code:

double x = Math.log(m * m - S * (1 - m) / b) / log(m) - 1;

and truncate this result to get the integral value of x where the next integer bankrupts the player.

like image 101
Bathsheba Avatar answered Sep 27 '26 12:09

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!