본문 바로가기

프로그래밍/오일러프로젝트

[EP 048] ∑ i^i (i=1 ~ 1000) 의 마지막 10자리수 구하기

728x90

#!/usr/bin/python

############################################################################
# 
# Problem 48
# 18 July 2003
#              1    2    3           10
# The series, 1  + 2  + 3  + ... + 10   = 10405071317.
# 
# Find the last ten digits of the series, 
#  1    2    3             1000
# 1  + 2  + 3  + ... + 1000     .
############################################################################
# Author : DwYoon
# Date   : 2007 04 12


def pow_mod(b, pw, md):
    ret = 1
    b = b % md
    for i in range(pw):
        ret = (ret*b) % md
    return ret

def sum_mod(a, b, md):
    return (a+b) % md

N = 1000
MOD = 10000000000 # last ten digit

res = 0
for i in range(1, N + 1):
    res = sum_mod(res, pow_mod(i, i, MOD), MOD)
print(res)