Project Euler #183: Maximum product of parts

  • + 0 comments

    For n=100, I get -738. Not sure what is wrong. Here is my code:

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    # None-terminating decimals that have a period are rational numbers that have (in their # most reduced from) a prime factor other than  2  or  5  in the denominator
    from fractions import Fraction
    
    q = int(input().rstrip())
    cached_d = [0]*(10**6)
    primes = set([2,3])
    max_s_prime = 3
    
    def prime(X):
        global primes
        global max_s_prime
        
        if max_s_prime >= X:
            if X in primes:
                return True
            return False
        else:
            for p in primes:
                if X % p == 0:
                    return False
                if p > X**0.5:
                    break
            while max_s_prime < X**0.5:
                max_s_prime +=1
                cur_is_prime = prime(max_s_prime)
                if cur_is_prime:
                    primes.add(max_s_prime)
                    if X % max_s_prime == 0:
                        return False
            primes.add(X)
            return True
    
    def max_k(NN):
        left_n = 1
        right_n = NN
        
        while left_n < right_n:
            first_border = int((right_n-left_n)/3) + left_n
            second_border = int((right_n-left_n)*2/3) + left_n
        
            first_border_f = (NN/first_border)**first_border
            if first_border_f == left_n:
                first_border_f = left_n + 1
            
            second_border_f = (NN/second_border)**second_border
            if second_border_f < left_n:
                second_border_f = left_n
        
            if first_border_f < second_border_f:
                left_n = first_border
            else:
                right_n = second_border
                
            if first_border >= (second_border - 1):
                maxs = []
                maxs.append((first_border_f, first_border))
                maxs.append((second_border_f, second_border))
                maxs.append(((NN/left_n)**left_n, left_n))
                maxs.append(((NN/right_n)**right_n, right_n)) 
                r = max(maxs)
                return r[1]
                #if first_border_f < second_border_f:
                #    return second_border
                #else:
                #    return first_border
     
        return left_n
            
    def term(N):
        global cached_d
        if cached_d[N] != 0:
            if cache_d[N] == 1:
                return True
            else:
                return False
            
        res = False
        k = max_k(N)
        f = Fraction(N, k)
        d = f.denominator
        if d == 2 or d == 5:
            res = True
        else:
            res = not prime(d)
        if res:
            cached_d[N] = 1
        else:
            cached_d[N] = 2
            
        return res
        
    def DD(N):
        if term(N):
            return -N
        return N
        
       
    for i in range(0, q):
        n = int(input().rstrip())
        #print("cur n={}".format(n))
        sum1 = 0
        for j in range(5, n+1):
            mk = max_k(j)
            #print("j={} DD={} maxk={} k/maxk={}".format(j, DD(j), mk, (j/mk)**mk))
            sum1 += DD(j)
        print("{}".format(sum1))