Clojure: Running time = 2.08s
+-;pow
(defn pow [a b]
(loop [res 1 mult a shift b]
(if (= 0 shift)
res
(if (= 1 (rem shift 2))
(recur (* res mult) (* mult mult) (bit-shift-right shift 1))
(recur res (* mult mult) (bit-shift-right shift 1))
)
)
)
)
+-;pow-mod
(defn pow-mod [a b m]
(loop [res 1 mult a shift b]
(if (= 0 shift)
(rem res m)
(if (= 1 (rem shift 2))
(recur (rem (* res mult) m) (rem (* mult mult) m) (bit-shift-right shift 1))
(recur res (rem (* mult mult) m) (bit-shift-right shift 1))
)
)
)
)
(defn p48 []
(println
(let [mod (pow 10 10)]
(rem (apply + (map #(pow-mod % % mod) (range 1 1001))) mod)
)
)
)
Erlang: Running time = 0.13s
+-%pow
pow(_,0)->1;
pow(1,_)->1;
pow(A,B)->pow(A,B,1).
pow(_,0,Res)->Res;
pow(Mult,B,Res) when B band 1 == 1 ->
pow(Mult*Mult,B bsr 1, Res*Mult);
pow(Mult,B,Res)->pow(Mult*Mult,B bsr 1, Res).
+-%pow_mod
pow_mod(_,_,1)->0;
pow_mod(_,0,_)->1;
pow_mod(1,_,_)->1;
pow_mod(A,B,M)->pow_mod(A,B,M,1).
pow_mod(_,0,_,Res)->Res;
pow_mod(Mult,B,M,Res) when B band 1 ==1 ->
pow_mod((Mult*Mult) rem M, B bsr 1, M, (Res*Mult) rem M);
pow_mod(Mult,B,M,Res)->pow_mod((Mult*Mult) rem M, B bsr 1, M, Res).
p48()->
M=pow(10,10),
io:format("~w~n",[lists:sum(lists:map(fun(X)->pow_mod(X,X,M) end, lists:seq(1,1000))) rem M]).
Ruby: Running time = 0.0s
+-#power_mod
def power_mod(a,b,m)
b=b.to_s(2).reverse.split("").map{|c| c=="1"}
acc=1
while(b.length>0)
c=b.shift
acc=(acc*a)%m if c
a=(a**2)%m
end
acc
end
def p48
p=10**10
puts (1..1000).map{|i|power_mod(i,i,p)}.sum%p
end