Integer Y with N Repeating Digits of X?

41 Views Asked by At

I have a single Base 10 digit X. I want to return number Y where Y is digit X repeated N times.

For example:

X = 2
N = 4
Y = 2222

X = 9
N = 7
Y = 9999999

X = 1
N = 1
Y = 1

Is there a simple mathematical method whereby I can return this Y given X and N? Thanks!

Note: If tags are bad, please suggest--I don't know enough about math to even properly frame and categorize the question.

1

There are 1 best solutions below

0
On BEST ANSWER

If the goal is to get a simple mathematical representation for the number in question:

The key observation is that $$10^n-1=\overbrace{9\dots 9}^{n\,times} $$

It follows that $$\frac {10^n-1}9=\overbrace{1\dots 1}^{n\,times} $$

And hence that $$x \times \frac {10^n-1}9=\overbrace{x\dots x}^{n\,times} $$

For $x\in\{1,2,3,4,5,6,7,8,9\}$

As remarked in the comments, this is impractical from a coding point of view (as exponentiation to large powers is a very costly operation).