Project Euler #108: Diophantine reciprocals I

  • + 0 comments
    import math
    
    def num_square_divisors(n):
        result = 1
        reduce = n
        divisor = 2
        
        while divisor * divisor <= reduce:
            exponent = 0
            while reduce % divisor == 0:
                exponent += 1
                reduce //= divisor
            result *= 2 * exponent + 1
            divisor += 1 if divisor == 2 else 2
    
        if reduce > 1:
            result *= 3 
    
        return result
    
    def main():
        import sys
        input = sys.stdin.read
        data = input().split()
        tests = int(data[0])
        results = []
        
        for i in range(1, tests + 1):
            n = int(data[i])
            divisors = num_square_divisors(n)
            half = (divisors + 1) // 2
            results.append(half)
        
        for result in results:
            print(result)
    
    // What's wrong with my code, It passes 7 testcases only.. and throw Timeout error?? Anyone please help
    
    if __name__ == "__main__":
        main()