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.
This is an interview question I came across: find K first digits of the
Share
Just implement grade-school long division:
The branch on
value == 0is to detect when1 / nhas a terminating representation of less thankdigits.Here,
nis the denominator in1 / nandkis the number of digits to print in the decimal representation of1 / n.Note that by changing
value *= 10tovalue *= byou can print the b-ary representation of1 / nas well.