# Thread: Create whole numbers within array

1. ## Create whole numbers within array

Hi friends,

I have an array of numbers, each number of which represents quantities of a financial instrument I must either buy or sell to create a statically hedged portfolio. Some of the numbers are not whole numbers, but rather contain values extending beyond the decimal point.

E.g.:

1) 102.56
2) -79.35
3) 34.1

The decimal values create a problem, as one cannot buy and sell fractions of shares or contracts. Is there an equation for generating the lowest possible number by which an array could be multiplied whereby all the numbers of the array would be whole numbers? I say "lowest possible number" because it may be impractical to multiply the array by a large value, as the resulting outputs may call for higher performance bond requirements (margin). Thus, a lower multiple is preferable, all else being equal.

Thank you very much for your consideration of this challenge. I appreciate any insights you may have to share.

Steven

2. I'm assuming, by the phrasing "array could be multiplied", that you must multiply all numbers in the array by the same factor in order to achieve integers everywhere.

Here's one algorithm:

Code:
For each entry in the array:
count the number of decimal places, $\displaystyle n$, that are present
Let $\displaystyle x$ be the numbers beyond the decimal place, expressed as an integer
let $\displaystyle y_{n} = 10^n / \gcd(10^n, x)$
Let $\displaystyle y = \text{lcm}(y_1, y_2, ... , y_n)$
Then $\displaystyle y$ is the number you're after.

Here's your three numbers as an example:

For $\displaystyle 102.56$, $\displaystyle n=2$. Let $\displaystyle x=56.$ Then $\displaystyle y_{1}=10^{2}/\gcd(10^{2},56)=100/4=25.$ (Check: $\displaystyle 102.56\times 25=2564,$ an integer.)

For $\displaystyle -79.35$, $\displaystyle n=2$. Let $\displaystyle x=35.$ Then $\displaystyle y_{2}=10^{2}/\gcd(10^{2},35)=100/5=20.$ (Check: $\displaystyle -79.35\times 20=-1587,$ an integer.)

For $\displaystyle 34.1$, $\displaystyle n=1$. Let $\displaystyle x=1.$ Then $\displaystyle y_{3}=10^{1}/\gcd(10^{1},1)=10/1=10.$ (Check: $\displaystyle 34.1\times 10=341,$ an integer.)

Finally, let $\displaystyle y=\text{lcm}(y_{1},y_{2},y_{3})=\text{lcm}(25,20,1 0)=100,$ which certainly works for your situation. You could dream up an example where the final factor is less than that, but your numbers beyond the decimals would have to have more factors in common than your example.

Does this make sense?

3. Thank you so much, Dr. Keister! Your solution makes perfect sense. I'm so impressed that you were able to dream this up. I wish I knew enough about math to think of such ideas.

I think I could extend the numbers a few more decimal places out, which would perhaps create higher GCDs, resulting in a lower LCM.

Thanks again,

Steven

4. You're welcome. Have a good one!

5. Hi again Ackbeet,

I had meant to ask what should be done if the first number(s) beyond the decimal point are zeros. Should I just omit the zero(s) in expressing the fractions as integers? If I do omit the zero(s), does n correspond to the number of decimal places with or without the zero? For example, if I were using the number 102.056, and assuming I should just omit the zero, would I solve for y_1 by: = 10^3/GCD(10^3, 56) , or would it be: =10^2/GCD(10^2, 56) ?

Thanks again for your philanthropy in being of assistance to others in need in this forum.

Steven

6. For example, if I were using the number 102.056, and assuming I should just omit the zero, would I solve for y_1 by: = 10^3/GCD(10^3, 56) , or would it be: =10^2/GCD(10^2, 56) ?
You should do the first one.

You're very welcome for any help I can provide.

7. OK, great, Dr. Keister. I now know just how to proceed, thanks to you.

Steven