Reputation: 203
So I have recently been creating Cookie Clicker in Scratch. While I have been adding buildings to the game and a buy 10 function for those buildings, I honestly also want to add a Buy 100 function to the game but I don't want to have the code as x+(x*1.15)+(x*1.15^2)+...+(x*1.15^99)
, where x
is defined as the number of that building that the player currently has, and I honestly don't want to have to keep having to add 200+ blocks of code to the "Buy 10 of y" codes. (y in this case what building is being bought 10 times)
4 difficulties that are keeping me from solving this myself:
As mentioned above (I've just been copying and pasting code at this point), I have to write it out as
if z (z being the number of the cookies the player has attained) = x+(x*1.15)+(x*1.15*1.15)+(x*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15) or if z > x+(x*1.15)+(x*1.15*1.15)+(x*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)
then set z to z - x+(x*1.15)+(x*1.15*1.15)+(x*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)+(x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)
Change cps (cookies per second) by 10 * a
(the amount of cookies per second that is being changed by multiplied by 10. I usually just add an extra 0 to the end of the number for simplicity)
Set b(y) (the cost of the building that is being bought) by (x*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15*1.15)
Change y by 10
else
broadcast Can't Buy!
However, I want to write as a cumulative price, similar to the formula used to determine how many cookies you would need to buy x buildings when you have y amount of the building such as bΣ(b=a+1) ((c)(1.15^b))/1.15
where c
is the base cost of the type building you want to buy, b
is the number of the type of building you want to have, and a
is the number of the type of building you have currently.
To clear up any confusion, if you want to take a look at the editor, here is the link.
Upvotes: 0
Views: 766
Reputation: 163
The sum you are trying to solve is a special case of something known as a finite geometric series and it's formula is:
latex:\sum_{k=0}^n xy^k=\frac{xy^{n+1}-x}{y-1}
plugging the values in we get:
latex:\sum_{k=0}^{100} x(1.15)^k=x\frac{(1.15)^{101}-1}{0.15}=20x\frac{(1.15)^{101}-1}{3}
to compute (1.15)^{101} we can use the fact that the exponential function and logarithm are inverses of each other to get that
latex:(1.15)^{101}=e^{101\log(1.15)}
writing this in scratch we get
Wolframalpha gives an approximation of 1350460.46830533172615525572988868491474851495179481437632719
more generally, if x is the initial number of buildings and n is the number of buildings to be bought, then the sum in scratch can be computed using
Upvotes: 1
Reputation: 6857
I used that C program link I sent you, for how to compute powers, to write an equivalent function in Scratch. Works with both positive and negative exponents. I'm rusty with Scratch so there might be a better way to return from the function, but it seems like it works great.
This should be all you need in order to get going, but if you still run into issues I can try to help out:
Result:
Proof:
Upvotes: 3