How to resolve the algorithm Iterated digits squaring step by step in the Factor programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Iterated digits squaring step by step in the Factor 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 Factor programming language
Source code in the factor programming language
USING: kernel math math.ranges math.text.utils memoize
prettyprint sequences tools.time ;
IN: rosetta-code.iterated-digits-squaring
: sum-digit-sq ( n -- m ) 1 digit-groups [ sq ] map-sum ;
MEMO: 1or89 ( n -- m )
[ dup [ 1 = ] [ 89 = ] bi or ] [ sum-digit-sq ] until ;
[
0 1
[
dup sum-digit-sq 1or89 89 = [ [ 1 + ] dip ] when
1 + dup 100,000,000 <
] loop
drop .
] time
You may also check:How to resolve the algorithm Draw a pixel step by step in the C programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the F# programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Racket programming language
You may also check:How to resolve the algorithm Shell one-liner step by step in the Huginn programming language
You may also check:How to resolve the algorithm Fairshare between two and more step by step in the D programming language