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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Iterated digits squaring step by step in the 11l 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 11l programming language

Source code in the 11l programming language

F next_step(=x)
   V result = 0
   L x > 0
      result += (x % 10) ^ 2
      x I/= 10
   R result

F check(number)
   V candidate = 0
   L(n) number
      candidate = candidate * 10 + n

   L candidate != 89 & candidate != 1
      candidate = next_step(candidate)

   I candidate == 89
      V digits_count = [0] * 10
      L(d) number
         digits_count[d]++

      V result = factorial(number.len)
      L(c) digits_count
         result I/= factorial(c)
      R result

   R 0

V limit = 100000000
V cache_size = Int(ceil(log10(limit)))
assert(10 ^ cache_size == limit)

V number = [0] * cache_size
V result = 0
V i = cache_size - 1

L
   I i == 0 & number[i] == 9
      L.break
   I i == cache_size - 1 & number[i] < 9
      number[i]++
      result += check(number)
   E I number[i] == 9
      i--
   E
      number[i]++
      L(j) i + 1 .< cache_size
         number[j] = number[i]
      i = cache_size - 1
      result += check(number)

print(result)

  

You may also check:How to resolve the algorithm 100 prisoners step by step in the Janet programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Pick random element step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Polymorphism step by step in the Python programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the AppleScript programming language