Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

base26

By: a guest on Mar 24th, 2012  |  syntax: Erlang  |  size: 0.41 KB  |  views: 66  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. -module(base26).
  2.  
  3. -export([to_d/1, to_b26/1, mult/2]).
  4.  
  5. to_d(Num) ->
  6.     F = fun(H, {N, Acc}) -> {N+1, (H - $A) * trunc(math:pow(26, N)) + Acc} end,
  7.     {_, Result} = lists:foldl(F, {0, 0}, lists:reverse(Num)),
  8.     Result.
  9.  
  10. to_b26(N) -> to_b26_impl(N, []).
  11.  
  12. to_b26_impl(0, Acc) -> Acc;
  13. to_b26_impl(N, Acc) -> to_b26_impl(N div 26, [N rem 26 + $A|Acc]).
  14.  
  15. mult(A, B) ->
  16.     to_b26(to_d(A) * to_d(B)).