Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@linkin-park
Created September 3, 2017 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linkin-park/b80da1465763ea83cb0a69e7cdd752e2 to your computer and use it in GitHub Desktop.
Save linkin-park/b80da1465763ea83cb0a69e7cdd752e2 to your computer and use it in GitHub Desktop.
% Enter your code here. Read input from STDIN. Print output to STDOUT
% Your class should be named solution
-module(solution).
-export([main/0]).
main() ->
{ok,[N,K]} = io:fread("","~s~d"),
P = ktimes(N,K,""),
{Num,_} = string:to_integer(P),
io:format("~p",[splitONum(Num)]).
%string:concat is ++ operator
ktimes(_,0,R)-> R;
ktimes(IntStr,K,R)-> ktimes(IntStr,K-1,string:concat(IntStr,R)) .
splitONum(N)when N < 10 -> N;
splitONum(N)-> splitONum(foo(N,0)).
%N denotes the number , S as SUM
foo(0,S)-> S;
foo(N,S) -> foo(N div 10,(N rem 10) + S).
@linkin-park
Copy link
Author

linkin-park commented Sep 4, 2017

-module(solution).
-export([main/0]).

main() ->
    {ok,[N,K]} = io:fread("","~d~d"),
    io:format("~w~n",[digitalRoot((N*K)rem 9)]).

digitalRoot(R) when R == 0 -> 9;
digitalRoot(R)  -> R.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment