Problem: 1/n cycle | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Problem: 1/n cycle

1/7 = 0.142857 142857 142857 .... We see that the decimal part has a cycle: 142857. The length of this cycle is 6. In the same way: 1/11 = 0.09 09 09 .... Cycle length is 2. Task Given an integer n (n > 1) the function cycle(n) returns the length of the cycle if there is one otherwise (n and 10 not coprimes) return -1. How do find out the length of the repetiting part after the dot. I dont know the condition statement in for loop for iteration

1st Aug 2023, 4:03 AM
TeaserCode
4 Answers
+ 5
I would like to choose a solution based on recursive algorithm. You take first two digits and check with the next two. If they not equal you will take one more digit etc.
1st Aug 2023, 6:27 AM
JaScript
JaScript - avatar
1st Aug 2023, 9:20 AM
Bob_Li
Bob_Li - avatar
0
Are you given the numbers as a string or just n and its 1/n every time?
7th Aug 2023, 4:27 AM
Jeremy Ed
Jeremy Ed - avatar
0
If you're printing to high precision then you could use something like this so you don't have precision loss. https://code.sololearn.com/c0f6WDjCv0tz/?ref=app
10th Aug 2023, 4:50 AM
Jeremy Ed
Jeremy Ed - avatar