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

Source code in the excel programming language

digitSum
=LAMBDA(s,
    FOLDROW(
        LAMBDA(a,
            LAMBDA(c,
                a + digitValue(c)
            )
        )
    )(0)(
        CHARSROW(s)
    )
)


digitValue
=LAMBDA(c,
    LET(
        ic, UNICODE(MID(c, 1, 1)),

        IF(AND(47 < ic, 58 > ic),
            ic - 48,
            IF(AND(64 < ic, 91 > ic),
                10 + (ic - 65),
                IF(AND(96 < ic, 123 > ic),
                    10 + (ic - 97),
                    0
                )
            )
        )
    )
)


CHARSROW
=LAMBDA(s,
    MID(s,
        SEQUENCE(1, LEN(s), 1, 1),
        1
    )
)


FOLDROW
=LAMBDA(op,
    LAMBDA(a,
        LAMBDA(xs,
            LET(
                b, op(a)(HEADROW(xs)),

                IF(1 < COLUMNS(xs),
                    FOLDROW(op)(b)(
                        TAILROW(xs)
                    ),
                    b
                )
            )
        )
    )
)


HEADROW
=LAMBDA(xs,
    LET(REM, "The first item of each row in xs",

        INDEX(
            xs,
            SEQUENCE(ROWS(xs)),
            SEQUENCE(1, 1)
        )
    )
)


TAILROW
=LAMBDA(xs,
    LET(REM,"The tail of each row in the grid",
        n, COLUMNS(xs) - 1,

        IF(0 < n,
            INDEX(
                xs,
                SEQUENCE(ROWS(xs), 1, 1, 1),
                SEQUENCE(1, n, 2, 1)
            ),
            NA()
        )
    )
)


  

You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the Java programming language
You may also check:How to resolve the algorithm Fibonacci word/fractal step by step in the Delphi programming language
You may also check:How to resolve the algorithm Yellowstone sequence step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Left factorials step by step in the zkl programming language