Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 21 lines (17 sloc) 0.519 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
%% Problem
%% ---------------------
%% The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.
%%
%% Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.
%% ---------------------

-module(p048).
-export([solve/0]).


%% Brute force works fast.
%%
solve() -> solve(10000000000).
solve(D) -> lists:sum([ pow(N, N) rem D || N <- lists:seq(1, 1000)]) rem D.

%% math:pow/2 doesn't work for N > 143
%%
pow(X, N) -> power(X, N, 1).
power(_, 0, Acc) -> Acc;
power(X, N, Acc) -> power(X, N - 1, X * Acc).
Something went wrong with that request. Please try again.