Repeating Decimal or Expanding Fraction

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Repeating Decimal or Expanding Fraction

Post by Roby »

Can someone explain to me how to find the repeating decimal or expanding fraction (202 and 275) from 2 integers? The algorithm only is OK. I've no idea with this problem. Thanx in advance :)
misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof »

Implement division the way it's taught in primary school. Keep track of all the remainders you have seen so far. Once a remainder repeats, you are in a cycle.
Timo
Learning poster
Posts: 70
Joined: Tue Oct 11, 2005 2:44 am
Location: Indonesia

Post by Timo »

Hi Roby,

I try to help you.

The number in decimal is repeated if when you divide them with the divisor they have same remainders appear twice.

I try explain you with sample input from problem 275.

input :
3 7

output :
.428571
The last 6 digits repeat forever.

output '.'

N

1. multiply 3 by 10
30 / 7 = 4 , output 4
30 % 7 = 2 (remember this value)

2. multiply 2 by 10
20 / 7 = 2, output 2
20 % 7 = 6 (remember this value)

3. multiply 6 by 10
60 / 7 = 8, output 8
60 % 7 = 4 (remember this value)

4. multiply 4 by 10
40 / 7 = 5, output 5
40 % 7 = 5 (remember this value)

5. multiply 5 by 10
50 / 7 = 7, output 7
50 % 7 = 1 (remember this value)

6. multiply 1 by 10
10 / 7 = 1, output 1
10 % 7 = 3 (remember this value)

7. multiply 3 by 10
30 / 7 = 4, output 4
30 % 7 = 2 (2 is appear twice)

to know how many digits repeat just output when :
(second time appear) - (first time appear)

in the example above first time '2' appear when N=1;
second time '2' appear when N=7;

so output 7-1 = 6.
6 digits repeat forever.

I hope you understand what I mean and can get AC.
Sorry for my poor English.

:D
"Life is more beautiful with algorithm"
Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Post by Roby »

thanx for the reply, I'll try it.
:lol: hehehe, forgot all that basics... :oops:
Post Reply

Return to “Algorithms”