Project Euler #48: Self powers

  • + 0 comments

    python3 100%

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    
    N = int(input())
    
    val, e = 0, 10**10
    
    def exponent_mod(a, n, e):
        p = 1
        while n > 1:
            if n % 2 == 1:
                p *= a % e
                n = (n-1)
            a = (a**2) % e
            n //= 2
        return((p*a) % e)
        
    for i in range(1, N+1):
        #print(exponent_mod(i, i , e), pow(i, i, e))
        val += exponent_mod(i, i, e)
        
    print(val%e)