Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created May 30, 2011 18:51
Show Gist options
  • Save iporsut/999307 to your computer and use it in GitHub Desktop.
Save iporsut/999307 to your computer and use it in GitHub Desktop.
Convert Base Number
-module(xbase).
-export([count/2])
count(Base,Max) ->
count(($0+Base),"0",lists:reverse(integer_to_list(Max))).
count(_,Current,Current) ->
io:format("~s~n",[lists:reverse(Current)]),
ok;
count(Base,Current,Max) ->
io:format("~s~n",[lists:reverse(Current)]),
[D|Rest] = Current,
if ((D+1) < Base) ->
count(Base,[(D+1) | Rest],Max);
true ->
Rest2 = carry(Base,Rest,[]),
count(Base,[$0 | Rest2],Max)
end.
carry(_,[],Result) -> lists:reverse([$1 | Result]);
carry(Base,[D | Rest],Result) ->
if ((D+1) < Base) ->
Result ++ [(D+1) | Rest];
true ->
carry(Base,Rest,[$0 | Result])
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment