#math problem for game dev

79 messages · Page 1 of 1 (latest)

languid fossil
#

i’m developing an incremental game and I need help with something

Given a non negative number N and a base(>1 but can be noninteger) b

how would I find what the largest number in the form b^0+b^1+b^2+b^3…
that’s not greater than N

cursive stratusBOT
#
  1. Wait patiently for a helper to come along.
  2. Once someone helps you, say thank you and close the thread with:
+close
  1. Feel free to nominate the person for helper of the week in #helper-nominations
languid fossil
#

for example if N is 17 and B is 2
then the largest number would be 2^0+2^1+2^2+2^3

languid fossil
#

was able to solve it for b >=2

#

i think..

#

haven’t tested decimals

sturdy holly
#

Let (S = 1 + b + \ldots + b^k).
Then
\begin{align*}
b S &= b + \ldots + b^k + b^{k + 1} \
S - 1 + b^{k + 1} &= b^1 + \ldots + b^k + b^{k + 1}
\end{align*}
so (b S = S - 1 + b^{k + 1} ). Solving for (S) yields
[S = \frac{b^{k + 1} - 1}{b - 1} .]

So we want the largest (k) such that
[\frac{b^{k + 1} - 1}{b - 1} \leq N.]

languid fossil
#

yep decimals also work for b>=2

vocal bluffBOT
#

invariance.

sturdy holly
#

given (N) and (b), you can solve (\frac{b^{k + 1} - 1}{b - 1} = N) (using a logrithm at the end)

languid fossil
#

lol i actually got that formula too (the end formula)

vocal bluffBOT
#

invariance.

languid fossil
#

maybe I was mistaken that it didn’t work for b<2

sturdy holly
#

then you can floor to get your result

sturdy holly
languid fossil
#

hm

#

k

#

i actually only know algebra 2 so some of this stuff is beyond me but surprisingly my knowledge on different number bases helped me in this case

#

cause numbers are essentially the same idea

sturdy holly
#

you want the biggest number of the form 111...111 in base b, you mean?

languid fossil
#

no no

sturdy holly
#

wdym then?

languid fossil
#

like for b = 3 and n = 26 i would want
3^0+3^1+3^2

#

something i don’t get is
(1.1^4-1)/(0.1) is not equal too 1.1^0+1.1^1+1.1^2+1.1^3?

#

shouldn’t it be equal

sturdy holly
#

,w 1.1^0+1.1^1+1.1^2+1.1^3

sturdy holly
#

,w (1.1^4-1)/(0.1)

sturdy holly
#

they are equal

#

(idk why you're saying they're not, but remember that === is very unreliable with floating point numbers)

languid fossil
#

oh i’m stupid

#

i forgot to add 1.1^3

#

anyways, why not I might use it, how would this problem be solved for 0<b<1

#

would it work the same?

sturdy holly
vocal bluffBOT
#

invariance.

languid fossil
#

ohh

#

definitely not a good idea to implement lmao

languid fossil
#

yea what about different fractions?

sturdy holly
#

so the series sums to 2

sturdy holly
languid fossil
#

i see

#

let me try to get some code working for it once done I’ll close this

#

btw let’s say it starts at b^2+b^3…

it would be the equivalent to the answer of the same problem starting at b^0
but the answer is divided by b^2

#

right?

sturdy holly
#

(b^2 + b^3 + \ldots + b^k = b^2 (1 + \ldots + b^{k - 2}))

languid fossil
#

yea

#

what I said

sturdy holly
#

you do it for k-2 terms

#

then multiply by b^2

#

if that's what you meant, then yeah

languid fossil
#

yea

vocal bluffBOT
#

invariance.

languid fossil
#

btw the whole idea of this is that a price of an upgrade in my game scales multiplicatively but the game will eventually reach a point where that upgrade must be bought really fast

#

and this just calculates that in bulk

sturdy holly
#

I had guessed that

#

it's neat that you're making games and finding an application for this stuff at the middle-school level

languid fossil
#

thanks!

#

honestly it’s nice that I covered logarithms in school

#

since i have been finding lots of uses

#

ok so this works but for technical reasons I also need to find k and return that too

#

how would I do that

#

noooo it doesn’t work ):

#

yea honestly not sure how to approach this

languid fossil
#

now I just gotta make it work but starting at b^x

#

rather than b^0

#

it works!

#

thanks for your help

#

(lol following guidelines)

sturdy holly
#

nice job!

#

what language is this?