There are many properties of numbers that one can investigate. The ancient Greek
ID: 3783567 • Letter: T
Question
There are many properties of numbers that one can investigate. The ancient Greeks were fascinated with the properties of integers, even ascribing them mystical properties.
One such property is an integer’s additive persistence and its resulting additive root (http://mathworld.wolfram.com/AdditivePersistence.html ). Additive persistence is a property of the sum of the digits of an integer. The sum of the digits is found, and then the summation of digits is performed creating a new sum. This process repeats until a single integer digit is reached. Consider the following example:
The beginning integer is 1234
Sum its digits is 1+2+3+4 = 10
The integer is now 10
The sum of its digits is 1 + 0 = 1
The integer is 1. When the value reaches a single digit, we are finished. This final
integer is the additive root
The number of cycles is the additive persistence. The integer 1234 has an additive persistence of 2 (first sum was 10, then the second sum was 1). The final digit reached is called the integer’s additive root. The additive digital root of 1234 is 1.
Program Specifications
The program should run as follows.
1) Gather input as a redirection from a file input.txt. The program ends under
one of the following circumstances
the next gathered integer is a negative number.
if all the integers from the file have been processed.
2) If the given integer is a single digit, report its additive persistence as 0 and its additive root as itself on a single line as two space separated numbers
3) For each multidigit, non-negative, integer output on a single line the two space separated numbers:
the integer's additive persistence
the integer's additive root
Getting Started
1) Create the lab02.cpp in your favorite editor
2) Break the problem down into parts. Some obvious parts:
gather input from the file, and check for negative numbers (the quit condition)
check if the input is between 0 and 9.
i. that's a special case as indicated
c. otherwise write a loop that calculates the persistence i. track the count through this loop
d. inside that write a loop that can sum the digits of an integer until it reaches a single digit.
3) How do you get the digits of an integer? Look at a combination of division (/) and remainder(%) operators on integers.
4) I would add some “diagnostic output” so you can be sure things are working as they should. For each pass through the loop of the additive persistence, print each new integer created. Feel free to always do this as you need to work on your ability to debug problems. You can always fix it to give the exact, required output later.
Explanation / Answer
Additive persistence:
In mathematics, the persistence of a number measures how many times a certain operation must be applied to its digits until some certain fixed condition is reached. we can determine the additive persistence of a positive integer by adding the digits of the integer and repeating. We should keep on adding the digits of the sum until a single digit number is found. The number of repetitions it took to reach that single digit number is the additive persistence of that number.
Example using 84523:
According to our program each line will contain a different integer to process. Input may come from stdin or from a file passed as an argument.
For each integer, we must output the integer, followed by a single space, followed by its additive persistence. Each integer processed must be on it's own line
The sample program to find additive persistence and root .Below is some of the steps we are going to follow.
The additive persistence of an integer is the number of times one has to replace the number by the sum of its digits until one reaches a single digit.
for example, the additive persistence of 496 (base 10) is:
apply 1. 496 => 4+9+6 ie. 19
apply 2. 19 => 1+9 ie. 10
apply 3. 10 => 1+0 ie. 1
The single-digit number finally reached is called the digital root of the number. (the digital root of 496 is 1.) a number's additive persistence is the number of steps it takes to arrive at its digital root.
Another sample program using tuple to know more about additive persistence and the digital root
The digital root X, of a number,n , is calculated:
find X as the sum of the digits of n
find a new by summing the digits of , repeating until has only one digit.
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.:
627615 has additive persistence 2 and digital root of 9;
The digital root may be calculated in bases other than 10.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.