How to resolve the algorithm Sum digits of an integer step by step in the Miranda 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 Miranda 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 Miranda programming language

Source code in the miranda programming language

main :: [sys_message]
main = [Stdout (lay (map fmt tests))]
       where tests     = [(1,10), (1234,10), (0xfe,16), (0xf0e,16)]
             fmt (d,b) = (shownum d) ++ "_" ++ (shownum b) ++ " -> " ++
                         (shownum (digitsum b d))

digitsum :: num->num->num
digitsum base 0 = 0
digitsum base n = n mod base + digitsum base (n div base)

  

You may also check:How to resolve the algorithm Scope modifiers step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Selectively replace multiple instances of a character within a string step by step in the Nim programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the C# programming language
You may also check:How to resolve the algorithm Scope modifiers step by step in the Eiffel programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Rebol programming language