Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find K first digits of the decimal representation of 1/N

Tags:

algorithm

math

This is an interview question I came across: find K first digits of the decimal representation of 1/N. It looks like we need just calculate 10^K/N to solve the problem. Does it make sense ? It looks like I am missing something because the solution is too easy.

like image 296
Michael Avatar asked Jan 14 '11 18:01

Michael


People also ask

How do you find the 1st digit?

To find first digit of a number we divide the given number by 10 until number is greater than 10. At the end we are left with the first digit.

What is K digit?

k is a digit in the decimal 1.3k5, and 1.3k5 is less than 1.33. Quantity A : k Quantity B : 1 Quantity A is greater., Quantity B is greater., The two quantities are equal., The relationship cannot be determined from the information given.

How do you find the decimal representation?

A rational number can be represented as a decimal number with the help of the long division method. We divide the given rational number in the long division form and the quotient which we get is the decimal representation of the rational number.

How do you find the digits of a number?

The formula will be integer of (log10(number) + 1). For an example, if the number is 1245, then it is above 1000, and below 10000, so the log value will be in range 3 < log10(1245) < 4. Now taking the integer, it will be 3. Then add 1 with it to get number of digits.


3 Answers

Just implement grade-school long division:

int value = 1;
bool outputDecimalSeparator = false;
int digitsOutput = 1;
while(digitsOutput <= k) {
    if (value == 0) {
        Console.Write(0);
    }
    else {
        if (value < n) {
            Console.Write(0);
            value *= 10;
        }
        else {
            Console.Write(value / n);  
            value %= n;
        }
   }
   if (outputDecimalSeparator == false) {
       outputDecimalSeparator = true;
       Console.Write('.');
   }
   digitsOutput++;
}
Console.WriteLine();

The branch on value == 0 is to detect when 1 / n has a terminating representation of less than k digits.

Here, n is the denominator in 1 / n and k is the number of digits to print in the decimal representation of 1 / n.

Note that by changing value *= 10 to value *= b you can print the b-ary representation of 1 / n as well.

like image 170
jason Avatar answered Sep 21 '22 05:09

jason


Calculating 10^K/N can be extremely costly with large K's and small N's.

This is probably closer to a good solution: long division. It's how we used to divide numbers before calculators. :)

Obviously, you should only perform this algorithm until it yields K digits.

like image 25
biziclop Avatar answered Sep 23 '22 05:09

biziclop


If it is first k digits, isn't it very straight forward to multiply the numerator by 10^k and so it becomes easier to divide by N? And if we need the answer, the decimal representation that is, then we will end up the dividing the result by 10^K again so that previous multiplication nullifies.

like image 28
Senthil Kumaran Avatar answered Sep 22 '22 05:09

Senthil Kumaran