Use Python programing and do not use functions for this assi
Use Python programing, and do not use functions for this assigmnemt.
Additive Persistence
Additive persistence (http://mathworld.wolfram.com/AdditivePersistence.html) 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 on the sum, repeating until a single integer digit is reached. The number of such cycles is that integer’s additive persistence. Consider the following example:
1. The beginning integer is 1234
2. Sum its digits is 1+2+3+4 = 10
3. The integer is now 10
4. The sum of its digits is 1 + 0 = 1
5. 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 digital root. The additive digital root of 1234 is 1.
Multiplicative Persistence
The multiplicative persistence (http://mathworld.wolfram.com/MultiplicativePersistence.html )and resulting multiplicative root are determined the same way, only multiplying the digits of an integer instead of adding. For example
1. The beginning integer is 1234
2. The product of 1*2*3*4 = 24
3. The integer is now 24
4. The product of 2*4 = 8
5. The integer is now 8. When the value reaches a single digit, we are finished. This final integer is the multiplicative root.
As before, the number of cycles is the multiplicative persistence. For 1234, the multiplicative persistence is 2, and its multiplicative root is 8.
Program Specification
In a file named persistence.py you should write a program that runs as follows:
Ask the user for an integer.
If the integer is less than 0, that is a signal to quit the program.
If the given integer is a single digit, report it’s additive persistence and multiplicative persistence as 0 and both its additive and multiplicative root as itself.
If the given integer is more than a single digit, find the additive/multiplicative persistence and additive/multiplicative root of the given integer and report the results to the user
If the value was non-negative continue by prompting the user until they quit.
Getting Started
Start by understanding the process. Go through the examples in this program specification with paper and pencil, and also try it with new numbers.
Use what you learned about the process to write a design document. You might want to write your document in bullited form that almost resembes code (and not a big paragraph). To start, break the problem into parts. Some parts (but not all) that will be needed are:
gather input from the user, and check for negative numbers (the quit condition)
a part that sums the digits of a number
a part that looks at the sum, and if it still has multiple digits, repeats the process that sums the digits of a number.
Create the persistence.py as usual.
Use the design document for code comments and to write small pieces of your code at a time.
Test each piece before moving on to the next.
Get the whole thing to work with one or the other (additive, multiplicative) before you address the other.
How do you get the digits of an integer? Look at a combination of integer division (//) and remainder(%) operators on integers. Try it out in IDLE first.
FOR DEVELOPMENT PURPOSES ONLY, I would add some “diagnostic output” so you can be sure things are working as they should. For example, for each pass through the loop of the additive (or multiplicative) persistence, print each new integer created. This should be removed before you make your final submission.
Example during development:
Example of final deliverable:
What number should I use for my calculations? 999 Addition New Value 27 New Value 9 Multiplication New Value 729 New Value 126 New Value: 12 New Value 2 For the integer 999 Additive Persistence 2, Additive Root 9 Multiplicative Persistence 4, Multiplicative Root 2 what number should I use for my calculations? 1234 Addition New Value 10 New Value 1 Multiplication New Value: 24 New Value 8 For the integer 1234 Additive Persistence 2, Additive Root-1 Multiplicative Persistence 2 Multiplicative Root 8 what number should I use for my calculations? 2 Addition Multiplication For the integer 2 Additive Persistence 0, Additive Root 2 Multiplicative Persistence 0, Multiplicative Root 2 what number should I use for my calculations? -1 Thanks for playing alongSolution
def mul(m):
prod = 1
print prod,m
while m > 0:
rem = m%10
prod*=rem
print rem,prod,m
m=m/10
if prod/10 >= 1:
mul(prod)
if prod/10 < 1:
print \"in less than\"
return (prod)
#main
n = input(\"Enter a number: \")
print (n)
#check if n less than 0 then quit
if n <= 0:
print \"number less than 0\"
exit()
#check if n is single digit
if n/10 <= 0 :
print \"additive persistence = \", n
print \"multiplicative persistence = \",n
m = n
#first find additive persistence
if n/10 > 1:
sum = 0
prod = 1
first = 1
while n > 0:
rem = n%10
sum+=rem
prod*=rem
print rem,sum,n
if sum/10 >= 1:
n = sum
if first == 1:
sum =0
print n
else:
n=n/10
print \"additive persistence = \", sum
#multiplicative persistence
ret = mul(m)
print ret
print \"multiplicative persistence = \",ret


