Apologies for what may be a simple question, but I don't have a background in mathematics beyond high school.
I'm trying to work out a formula, that when given a number, say 2, to treat this as "number of digits" and calculate the largest possible integer.
For example :
- If
2, then largest number possible is99 - If
3, then largest number possible is999 - If
4, then largest number possible is9999
My quick and dirty solution (programmers hat on here), is to just count upwards from 0 to the given number, appending 9, and returning the result, however there must be a mathematical formula that can do this in a cleaner and more efficient manner.
Thanks
In base $10$, the number you want is $10^n - 1$.
If you are passing strings around, your method will be fast enough.
If you are passing ints around, you can compute it using the above, but might have to worry about overflows etc. Of course, computing $10^n$ might be slower than just appending nines to your string...
So codewise it might be cleaner, but it need not be more efficient and might open up the overflow can of worms.