
Self Powers
The series, $1^1 + 2^2 + 3^3 + \cdots + 10^{10} = 10405071317$.
Find the last ten digits of the series, $1^1 + 2^2 + 3^3 + \cdots + 1000^{1000}$.

The series, $1^1 + 2^2 + 3^3 + \cdots + 10^{10} = 10405071317$.
Find the last ten digits of the series, $1^1 + 2^2 + 3^3 + \cdots + 1000^{1000}$.
Ancora un semplice esercizio che si appoggia alla comoda funzione powermod(a,b,m), che calcola \(a^b \mod m\). Volendo ce la si fa anche in una sola linea:
@time mod(sum([powermod(i,i,10^10) for i in 1:1000]),10^10) 0.070772 seconds (87.78 k allocations: 4.265 MiB, 99.59% compilation time)
9110846700 che però è più dispendiosa perché alloca materialmente il vettore delle potenze \(i^i\). Altrimenti qui segue la sua forma estesa, più classica, che calcola una valore per volta e quindi non alloca memoria inutile:
sol = 0
@time for i in 1:1000
global sol += powermod(i,i,10^10)
end
@show mod(sol,10^10) 0.000854 seconds (4.88 k allocations: 92.172 KiB)
mod(sol, 10 ^ 10) = 9110846700