Repetend
Problem
Repetend is the infinite repeating figures in a repeating decimal. Every repeating decimal can be expressed as a fraction. So as long as a fraction is given, one can calculate its repetend. Please develop a program to implement this algorithm.
Tip
General steps: Get the remainder of the dividend in division by the divisor, and judge whether the remainder appeared in the previous computation. If it has not appeared yet, then multiply the remainder by 10 and take the result as the dividend. Continue the loop and repeat the computation to calculate the remainder until a same remainder that has once appeared is found. The quotient between the position where the same remainder appears last time and the position where it appears this time is the repetend in the repeating decimal.
- Get the remainder by dividing the dividend by the divisor.
- Firstly judge in the loop body whether the remainder appeared in the preceding computation. If it did, then suspend the loop.
- If the remainder has not appeared, add the current remainder to historical remainder sequence, and then multiply the current remainder by 10 and take the result as the dividend to be divided by the divisor. Add the quotient into the historical quotient sequence, replace the current remainder with the resulting remainder, and enter the next round of loop.
- According to the digits from the position where the current remainder is found in the historical remainders to the position of the last remainder, find the quotients at the corresponding positions in the historical quotient sequence. This group of quotients is the repetend.
Code
A | B | C | ||
---|---|---|---|---|
1 | 99 | Dividend | ||
2 | 140 | Divisor | ||
3 | =A1%A2 | Obtain the remainder | ||
4 | for | =B6.pos(A3) | Define the infinite loop. If the remainder obtained in this loop has appeared in the remainders in previous loops, then end the loop, and B4 stores the position in which the remainder appears. | |
5 | if B4>0 | break | ||
6 | =B6|A3 | Store the remainder computed each time. | ||
7 | =B7|int(A3*10/A2) | Store the quotient computed each time. | ||
8 | >A3=A3*10%A2 | Refresh A3 to appear as the current remainder. | ||
9 | =B7.to(B4,).concat() | In B7, part from position B4 to the last number is the repetends. Take out the repetends and put them into string. |
Result
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL
Chinese version