How to resolve the algorithm Digital root step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Digital root step by step in the Wren programming language
Table of Contents
Problem Statement
The digital root,
X
{\displaystyle X}
, of a number,
n
{\displaystyle n}
, is calculated: The additive persistence is the number of summations required to obtain the single digit. The task is to calculate the additive persistence and the digital root of a number, e.g.: The digital root may be calculated in bases other than 10.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Digital root step by step in the Wren programming language
Source code in the wren programming language
import "./fmt" for Fmt
var sumDigits = Fn.new { |n|
var sum = 0
while (n > 0) {
sum = sum + (n%10)
n = (n/10).floor
}
return sum
}
var digitalRoot = Fn.new { |n|
if (n < 0) Fiber.abort("Argument must be non-negative.")
if (n < 10) return [n, 0]
var dr = n
var ap = 0
while (dr > 9) {
dr = sumDigits.call(dr)
ap = ap + 1
}
return [dr, ap]
}
var a = [1, 14, 267, 8128, 627615, 39390, 588225, 393900588225]
for (n in a) {
var res = digitalRoot.call(n)
var dr = res[0]
var ap = res[1]
Fmt.print("$,15d has additive persistence $d and digital root of $d", n, ap, dr)
}
You may also check:How to resolve the algorithm Brazilian numbers step by step in the Arturo programming language
You may also check:How to resolve the algorithm Anonymous recursion step by step in the Clojure programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Groovy programming language
You may also check:How to resolve the algorithm O'Halloran numbers step by step in the Phix programming language
You may also check:How to resolve the algorithm Search a list of records step by step in the Ring programming language