How to resolve the algorithm Harshad or Niven series step by step in the Erlang programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Harshad or Niven series step by step in the Erlang programming language

Table of Contents

Problem Statement

The Harshad or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits. For example,   42   is a Harshad number as   42   is divisible by   (4 + 2)   without remainder. Assume that the series is defined as the numbers in increasing order.

The task is to create a function/method/procedure to generate successive members of the Harshad sequence. Use it to:

Show your output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Harshad or Niven series step by step in the Erlang programming language

Source code in the erlang programming language

-module( harshad ).

-export( [greater_than/1, sequence/1, task/0] ).

greater_than( N ) when N >= 1  ->
        greater_than( 2, N, acc(1, {0, []}) ).

sequence( Find_this_many ) when Find_this_many >= 1 ->
        sequence( 2, Find_this_many, acc(1, {0, []}) ).

task() ->
        io:fwrite( "~p~n", [sequence(20)] ),
        io:fwrite( "~p~n", [greater_than(1000)] ).



acc( N, Acc ) -> acc( N rem lists:sum([X - $0|| X <- erlang:integer_to_list(N)]), N, Acc ).

acc( 0, N, {Found, Acc} ) -> {Found + 1, [N | Acc]};
acc( _Reminder, _N, Acc ) -> Acc.

greater_than( _N, Find, {_, [Found | _T]} ) when Found > Find -> Found;
greater_than( N, Find, Acc ) ->	greater_than( N + 1, Find, acc(N, Acc) ).

sequence( _N, Found, {Found, Acc} ) -> lists:reverse( Acc );
sequence( N, Find, Acc ) -> sequence( N + 1, Find, acc(N, Acc) ).


-module(harshad).
-export([main/0,harshad/1,seq/1]).

% We return the number R if harshad, else 0
harshad(R) ->
        case R
        rem lists:sum([X - $0|| X <- erlang:integer_to_list(R)]) of 0
        -> R; _ -> 0 end. 

% build a list of harshads retrieving input from harshad(R)
% filter out the nulls and return
hlist(A,B) ->
      RL =  [ harshad(X) || X <- lists:seq(A,B) ],
      lists:filter( fun(X) -> X > 0 end,  RL).

seq(Total) -> seq(Total, [], 0).

seq(Total,L,_) when length(L) == Total-> L;
seq(Total,L,Acc) when length(L) < Total ->
      NL = hlist(1,Total + Acc),
      seq(Total,NL,Acc+1).                         
                                        
gt(_,L) when length(L) == 1 ->  hd(L);
gt(X,_) ->
      NL = hlist(X+1,X+2),
      gt(X+2,NL).                  

main() ->
      io:format("seq(20): ~w~n", [ seq(20) ]),
      io:format("gt(1000): ~w~n", [ gt(1000,[]) ]).


  

You may also check:How to resolve the algorithm SHA-256 step by step in the Java programming language
You may also check:How to resolve the algorithm Faces from a mesh step by step in the Raku programming language
You may also check:How to resolve the algorithm Motzkin numbers step by step in the C programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the XLISP programming language
You may also check:How to resolve the algorithm Calkin-Wilf sequence step by step in the Arturo programming language