### Solved Problem 26

parent 34a31c23
Showing with 65 additions and 0 deletions
26/README 0 → 100644
 Question: A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: 1/2 = 0.5 1/3 = 0.(3) 1/4 = 0.25 1/5 = 0.2 1/6 = 0.1(6) 1/7 = 0.(142857) 1/8 = 0.125 1/9 = 0.(1) 1/10 = 0.1 Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. Find the value of d 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. Answer: 983
26/main.cpp 0 → 100644
 #include #include using namespace std; const int MAX_INTEGER = 1000; // Think of this as using simple long division. // After we figure out the remainder, we add a zero to it and try again // If we ever return to that remainder, it's looping! int find_recurring_pattern(int numerator, int denominator) { vector stack; int i = 0; while(true) { numerator = numerator * 10; numerator = numerator % denominator; for(int it = 0; it < stack.size(); it++) { if(stack[it] == numerator) { return (i - it); } } stack.push_back(numerator); i++; } return 0; } int main() { int largest = 0; int denominator = 2; int temp = 0; for(int i = denominator; i <= MAX_INTEGER; i++) { temp = find_recurring_pattern(1,i); if(temp > largest) { largest = temp; denominator = i; } } cout << denominator << endl; return 0; }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!