Project Euler #48: Self powers

Sort by

recency

|

66 Discussions

|

  • + 1 comment

    def Square_sum(n): MOD = 10**10

    total_sum = 0
    
    
    for i in range(1, n + 1):
    
        total_sum = (total_sum + pow(i, i, MOD)) % MOD
    
    return total_sum
    

    if name == 'main':

    N = int(input())
    
    
    print(Square_sum(N))
    
  • + 1 comment

    Python 3 100% 1-liner

    print(int(str(sum(pow(i, i, 10**10) for i in range(1, int(input())+1)))[-10:]))
    
  • + 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)
    
  • + 0 comments

    JAva code

    import java.io.*;
    import java.util.*;
    import java.math.BigInteger;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            long t = in.nextLong();
            System.out.println(count(t));
        }
    
    
        public static String count(long num) {
            BigInteger modulus = BigInteger.TEN.pow(10);
            BigInteger sum = BigInteger.ZERO;
            for (int i = 1; i <= num; i++)
                sum = sum.add(BigInteger.valueOf(i).modPow(BigInteger.valueOf(i), modulus));
            return sum.mod(modulus).toString();
        }
    }
    
  • + 0 comments

    I was pretty much surprised that my solution has been accepted 100 % while it took 3.5 seconds on my (modest though) laptop. I thought the correct solutions should take 2 sec at most.