Created
March 17, 2020 13:09
-
-
Save mnrn/472501df9e4fac642c886b351d117f5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import unittest | |
| def cycle_finding(n): | |
| m, p = 1, 1 # m.. カメの計算のあまり p.. ウサギの計算の余り | |
| s, t = 0, 0 # s.. 循環節の先頭小数桁位置 t.. 循環節の末尾小数桁位置 | |
| while True: | |
| m = (m * 10) % n | |
| p = (p * 10) % n | |
| p = (p * 10) % n | |
| if m == p: | |
| break | |
| if p != 0: | |
| # 循環節の先頭の検知 | |
| p = 1 | |
| s = 1 | |
| while m != p: | |
| s += 1 | |
| m = (m * 10) % n | |
| p = (p * 10) % n | |
| # 循環節の末尾の検知 | |
| p = (p * 10) % n | |
| t = s | |
| while m != p: | |
| t += 1 | |
| p = (p * 10) % n | |
| if s == 0 and t == 0: | |
| return 0 | |
| else: | |
| return t - s + 1 | |
| class TestCycleFinding(unittest.TestCase): | |
| def test_1st_decimal_place(self): | |
| # n = 3 1 / 3 = 0.33333... -> 0.(3) | |
| self.assertEqual(cycle_finding(3), 1) | |
| def test_2nd_decimal_place(self): | |
| # n = 6 1 / 6 = 0.16666... -> 0.1(6) | |
| self.assertEqual(cycle_finding(6), 1) | |
| def test_max_digits(self): | |
| # n = 7 1 / 7 = 0.14285714285714... -> 0.(142857) | |
| self.assertEqual(cycle_finding(7), 6) | |
| def test_two_digit_number(self): | |
| # n = 56 1 / 56 = 0.01785714285714285... -> 0.017(857142) | |
| self.assertEqual(cycle_finding(56), 6) | |
| if __name__ == '__main__': | |
| unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment