Computing the last digit of b^e efficiently in Erlang

This is mostly meant as a note to myself, but you can use it too. :-)

I read a blog post at, describing, yes you've gessed it, how to compute the last digit of b\^e efficiently.

I thought it might come in handy for stuff like project Euler. I'm not going to explain the theory behind this, as it is explained in the blog post I read, but I will embed the Erlang code for computing the last digit of b\^e efficiently below. :-)


% returns the last digit of B to the power of E, very fast.
solve(B,E) -> 
  Bl=B rem 10, 
  El=E rem 100,
  Cycle=lists:nth(Bl+1,[ [0], [1], [2,4,8,6], [3,9,7,1], [4,6], [5], [6], [7,9,3,1], [8,4,2,6], [9] ]),
  lists:nth(El rem length(Cycle),Cycle).

Do you know any way to improve the above Erlang code?

Written by Jannich Brendle ons 07 april 2010 In Programming