How to resolve the algorithm Sum digits of an integer step by step in the Erlang programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sum digits of an integer step by step in the Erlang programming language
Table of Contents
Problem Statement
Take a Natural Number in a given base and return the sum of its digits:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum digits of an integer step by step in the Erlang programming language
Source code in the erlang programming language
-module(sum_digits).
-export([sum_digits/2, sum_digits/1]).
sum_digits(N) ->
sum_digits(N,10).
sum_digits(N,B) ->
sum_digits(N,B,0).
sum_digits(0,_,Acc) ->
Acc;
sum_digits(N,B,Acc) when N < B ->
Acc+N;
sum_digits(N,B,Acc) ->
sum_digits(N div B, B, Acc + (N rem B)).
You may also check:How to resolve the algorithm A+B step by step in the XBS programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Ruby programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Racket programming language
You may also check:How to resolve the algorithm Image convolution step by step in the D programming language
You may also check:How to resolve the algorithm Program termination step by step in the Phix programming language