Multiply a number so that it's always in the thousand billions

50 Views Asked by At

Excuse my lack of mathematical notation, hopefully this makes sense.

Say I have a number which is x

10,000,000,000,000 > x > 999,999,999

I always want to multiply this number by some other number y, so that the result z in the thousand billions, so for example:

x = 1,230,000,000      
x * y = z
z = 1,230,000,000,000
y = 1000

x = 55,123,000,000      
x * y = z
z = 5,512,300,000,000
y = 100

x = 456,123,000,000      
x * y = z
z = 4,561,230,000,000
y = 10

x = 7,894,560,000,000      
x * y = z
z = 7,894,560,000,000
y = 1

All I have is x and the I know that x is be between 1,000,000,000 and 9,999,999,999,999.

How can I find the value of y so that I can calculate z? I'm currently doing this manually (in code) with if statements,

if x < 10 => x * 1000
if x < 100 => x * 100
if x < 1000 => x * 10
if x < 1000 => x

But I'm looking for a single mathematical function that can do this for me.

Solved with Arnaud Mortier's answer

Using the marked answer I created the function (in Javascript)

const fn = x => {
    let k = 12 - Math.floor(Math.log10(x));
    let y = Math.pow(10, k);
    let z = x * y;

    console.log(`x = ${x}, k = ${k}, y = ${y}, z = ${z}`);

    return z;
};

Even more than I hoped for, this works for any positive integer, not just those in my range.

3

There are 3 best solutions below

4
On BEST ANSWER

Set $y=10^{k}$. \begin{align}10^{12}\leq z<10^{13}&\Longleftrightarrow 12\leq {\log_{10}(x\cdot 10^k)}<13\\&\Longleftrightarrow \left\lfloor \log_{10}{(x\cdot 10^k)}\right\rfloor=12\\&\Longleftrightarrow \left\lfloor \log_{10}{(x)+k}\right\rfloor=12\\&\Longleftrightarrow k=12-\left\lfloor \log_{10}{(x)}\right\rfloor\\\end{align}

If you don't have $\log_{10}$ implemented in your computer, use $\log_{10}(x)=\frac{\ln(x)}{\ln(10)}$.

The function $\left\lfloor x\right\rfloor$ is usually implemented as $\operatorname{floor}(x)$.

1
On

Pick a number $k$ between one thousand billion and ten thousand billion and take $y = \frac{k}{x}$ ?

0
On

Writing $x$ in scientific notation we get something like $k\times10^n$ where $1<k<9$. For example $x = 1,230,000,000 = 1.23\times10^9$ Now you want the $z = 1,230,000,000,000=1.23\times10^{12}$. So you need to multiply $1000$ or $10^3$. Just find the value $n$ when $x$ is written in scientific notation and if see how much you are short of $12$. And multiply that much.