From the 1996 UF ACM High School Programming Competition

Repeating Expansions

Ed Sessive scribbles wildly in his notebook. He's just realized that every rational number's decimal expansion must repeat. He's also just realized that his pen is almost out of ink. He doesn't want to stop playing with his new discovery. And there's someone with a computer sitting just a few feet away.

The first thing you notice is a loud rustling of paper. You worry, knowing Ed's tendency to become obsessive. You look over and see him walking towards you, his hands stretched towards your laptop. You state firmly, "Um, no." He looks as if you've stabbed him with a nasty knife and he's going to die very slowly and miserably at your feet. You give up and ask what he wants. It sounds simple enough...

Problem Statement

Write a program which takes a numerator and a denominator, both positive integers less than 32000, and prints the repeating group of the decimal expansion.

Notes

The repeating digits displayed must begin with the first digit that appears after any non-repeating portion of the expansion. For instance, 11/42 = .2619047619047..., so 619047 is displayed.

Each fraction will repeat within the first 100 digits of its expansion.

And no, you don't have to have the pluralization correct.

Examples

Example 1:
Enter a numerator and a denominator: 1 5
Repeating digit: 0

Example 2:
Enter a numerator and a denominator: 12 13
Repeating digits: 923076

Example 3:
Enter a numerator and a denominator: 11 42
Repeating digits: 619047


Grader's Test Data

Example 4:
Enter a numerator and a denominator: 2 3
Repeating digit: 6

Example 5:
Enter a numerator and a denominator: 11 16
Repeating digit: 0

Example 6:
Enter a numerator and a denominator: 17 19
Repeating digits: 894736842105263157

Example 7:
Enter a numerator and a denominator: 1 103
Repeating digits: 0097087378640776699029126213592233

Example 8:
Enter a numerator and a denominator: 96 97
Repeating digits: 989690721649484536082474226804123711340206185567010309278350515463917525773195876288659793814432


Solutions

C solution by Jason Riedy:
repeat.c