Project Euler Problem#26

import unittest

def max_reciprocal_cycle(num):
    if type(num)==float:
        num=int(num)
    if type(num)!=int or num<1:
        return None    
    max_length=0
    max_num=1
    for i in range(2,num+1):
        numerator=10
        remainders=set()
        fraction=''
        a=True
        while a:
            while numerator<i:
                fraction+='0'
                numerator*=10
            if numerator%i == 0:
                break
            if (numerator%i) not in remainders:
                fraction+=str(numerator//i)
                remainders.add(numerator%i)
                numerator= 10*(numerator%i)
            else:
                a=False
        if len(fraction)>max_length:
            max_length=len(fraction)
            max_num=i
    return(max_num)

print(max_reciprocal_cycle(10.8))

class test_cases(unittest.TestCase):
    def test_max_reciprocal_cycle(self):
        self.assertEqual(max_reciprocal_cycle(10),7)
        self.assertEqual(max_reciprocal_cycle(-10),None)
        self.assertEqual(max_reciprocal_cycle('10'),None)
        self.assertEqual(max_reciprocal_cycle(10.8),7)
        self.assertEqual(max_reciprocal_cycle(-10.8),None)

if __name__ == "__main__":
    unittest.main()
0
Subscribe to my newsletter

Read articles from Varjinth subramaniyan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Varjinth subramaniyan
Varjinth subramaniyan