Project Euler #29: Distinct powers

  • + 0 comments
    # My Third attempt was succeded
    # My First Attempt
    
    L = []
    n = int(input(""))
    if n >= 2 and n <= 10**5:
        for i in range(2, n+1):
            for j in range(2, n+1):
                k = i**j
                if k not in L:
                    L.append(k)
                else:
                    pass
        print(len(L))
    else:
        pass
    # Failed
    
    # My Second Attempt
    
    N = int(input())
    L = [True] * (N+1)
    res = 0
    for i in range(2, N+1):
        if L[i]:
            power = 2
            res = []
            while i**power <= N:
                L[i**power] = False
                res += [n for n in range(2*power, N * power+1, power) if n>N]
                power+=1
            res += len(set(res))+(N-1)
            
    print(res)
    # Failed
    
    # My Final Attempt
    
    def dist_term(n: int) -> int:
        count = 0
        test = [0]*(n+1)
        for i in range(2, n+1):
            if test[i]: continue;
            combined = set()
            po = 2
            while (i**po <= n):
                test[i**po] = True
                s = set([j*po for j in range(2, n+1) if j*po > n])
                combined.update(s)
                po += 1
            count += len(combined) + n-1
        return count
                
    print(dist_term(int(input())))