How to resolve the algorithm Iterated digits squaring step by step in the Arturo programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Iterated digits squaring step by step in the Arturo programming language

Table of Contents

Problem Statement

If you add the square of the digits of a Natural number (an integer bigger than zero), you always end with either 1 or 89: An example in Python:

Or, for much less credit - (showing that your algorithm and/or language is slow): This problem derives from the Project Euler problem 92. For a quick algorithm for this task see the talk page

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Iterated digits squaring step by step in the Arturo programming language

Source code in the arturo programming language

gen: function [n][
    result: n
    while [not? in? result [1 89]][
        s: new 0
        loop digits result 'd ->
            's + d*d
        result: s
    ]
    return result
]

chainsEndingWith89: function [ndigits][
    [prevCount,currCount]: #[]
    loop 0..9 'i -> prevCount\[i*i]: 1

    res: new 0

    loop 2..ndigits 'x [
        currCount: #[]
        loop prevCount [val,cnt][
            v: to :integer val
            loop 0..9 'newDigit [
                mm: v + newDigit*newDigit
                if not? key? currCount mm -> currCount\[mm]: 0
                currCount\[mm]: currCount\[mm] + cnt
            ]
        ]
        prevCount: currCount
    ]
    loop currCount [val,cnt][
        v: to :integer val
        if and? [v <> 0] [89=gen v] ->
            'res + cnt
    ]
    return res
]

print [
    "Number chains for integers <100000000 that end with an 89 value:" 
    chainsEndingWith89 8
]


  

You may also check:How to resolve the algorithm Matrix-exponentiation operator step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Ada programming language
You may also check:How to resolve the algorithm Special variables step by step in the NetRexx programming language
You may also check:How to resolve the algorithm File size step by step in the Factor programming language
You may also check:How to resolve the algorithm Special variables step by step in the Go programming language