2

I need a function such that if input is $n$ it outputs an $n$-digit number. I need a mathematical function.

For example:

$$\begin{align}\mathrm{ANY}(2) &= 22 \\ \mathrm{ANY}(3) &= 333 \\ \mathrm{ANY}(n) &= nnn\ldots n \quad (n \text{ times})\end{align}$$

I think $\mathrm{ANY}(n)$ is something like:

$$\begin{align}\mathrm{ANY}(2) &= (10^1 + 10^2) * 2 \\ \mathrm{ANY}(3) &= (10^1 + 10^2 + 10^3) * 3 \\ \mathrm{ANY}(n) &= (10^1 + 10^2 + \ldots + 10^n) * n\end{align}$$

My question is can we further simplify the equation: $10^1 + 10^2 + \ldots + 10^n$? Or is it not possible?

Additionally, I need just any $n$-digit number on $n$ input, not necessary like $22$, $333$, $4444$, $\ldots$. In case simplification is not possible then suggest me any other function? I have no much idea if its possible.
Also domain to my $\mathrm{ANY}$ function can be $1$ to $100$.

I need to solve some problem in computer science.

TMM
  • 9,976
Grijesh Chauhan
  • 325
  • 4
  • 18

3 Answers3

4

I suppose simpler functions would be $\mathrm{any}(n) = 10^{n-1}$, e.g. outputting $\mathrm{any}(3) = 100$, or $\mathrm{any}(n) = 10^n - 1$, e.g. outputting $\mathrm{any}(3) = 999$.

To get the exact function you specified, you could use the formula for geometric series to obtain $\mathrm{any}(3) = (\frac{10^{n} - 1}{9}) \cdot n$. Note that this does not give you an $n$-digit number for $n \geq 10$.

TMM
  • 9,976
  • thanks :) sorry it was so simple I mean your first solution. – Grijesh Chauhan Mar 28 '13 at 19:43
  • @FixedPoint: Yes, that formula works, but the formula suggested by the OP leads to $\mathrm{any}(n)=(10^n-1)/9 \cdot n$ (note the extra $n$) which gets an unwanted extra digit once $n \geq 10$. – TMM Mar 29 '13 at 00:23
3

This one gives you only till $n\leq 9$. You could take the geometric series which says that $$\sum_{k=0}^n q^k = \frac{q^{n+1}-1}{q-1}$$ which is in your case $$\frac{1}{10}\cdot \left( \frac{10^{n+1}-1}{9}-1\right)\cdot n$$ the $-1$ is there because you don't have the naught term.

If you only want an arbitrary $n$ digit number take $$10^{n-1}$$ if you want alle the same digit take $$\frac{10^n-1}{9} \cdot \text{your favorite digit}$$

To get any number from 10 till 99 with your exact formula take $$\frac{1}{100} \cdot \sum_{k=1}^\frac{n}{2} 100^k \cdot n=\frac{1}{99} \cdot (10^n-1)\cdot n$$ for $n$ even and $$\frac{1}{10} \cdot \sum_{k=1}^\frac{n-1}{2} 100^k \cdot n + \left\lfloor\frac{n}{10}\right\rfloor=\frac{1}{99} (10^{n}-10)\cdot n +\left\lfloor\frac{n}{10}\right\rfloor$$

0

so if you are okay with

any(1)=1

any(2)=11

any(3)=111

and so on then $$any(N)=\frac{10^N-1}{9}$$ will work for any number of digits so the domain is all natural numbers. You can get a number with whatever number of digits you need.

Fixed Point
  • 7,909
  • 3
  • 31
  • 48
  • Why the downvote? This any(N) does give you a N digit number for any natural number N. any(20)=11111111111111111111 – Fixed Point Mar 29 '13 at 00:16