Consider the process of taking a number, adding its digits, then adding the digi
ID: 3877620 • Letter: C
Question
Consider the process of taking a number, adding its digits, then adding the digits of the number derived from it, etc., until the remaining number has only one digit. The number of additions required to obtain a single digit from a number n is called the additive persistence of n, and the digit obtained is called the digital root of n. For example, the sequence obtained from the starting number 9876 is 9876, 30, 3, so 9876 has an additive persistence of 2 and a digital root of 3. Write two Elm functions additivePersistence : Int -> Int digitalRoot : Int -> Int that take positive integer arguments n and, respectively, return the additive persistence and the digital root of n. Once you have implemented the functions, you should get the following behavior at the Elm REPL: > additivePersistence 9876 2 : Int > digitalRoot 9876 3 Int Think about how you can factor the implementations of these two functions into common, reusable parts.Explanation / Answer
Additive persistence function in elm
Steps for Solution:-
1) Initialize vars sum and loop
2) Convert str to an array of numbers
3) Loop until the sum of digits in array is a single digit number
4) Return number of loops as answer
function additivePersistence(num) {
while (val1.length > 1)
{
val1.forEach( function(number)
{
}
Digital Root
function additivePersistence(num) {
var sum, loop = 0; var val1 = num.toString(10).split("").map(function(t) {return parseInt(t)});while (val1.length > 1)
{
sum = 0;val1.forEach( function(number)
{
sum = sum + number; }); val1 = sum.toString(10).split("").map(function(t){return parseInt(t)}); loop++; } return loop;}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.