# Multiplicative Persistence Coderbyte Solution

0

Have the function MultiplicativePersistence(num) take the num parameter being passed which will always be a positive integer and return its multiplicative persistence which is the number of times you must multiply the digits in num until you reach a single digit.

For example: if num is 39 then your program should return 3 because 3 * 9 = 27 then 2 * 7 = 14 and finally 1 * 4 = 4 and you stop at

Multiplicative Persistence Java

import java.util.Scanner;

public class MultiplicativePersistence {
int MultiplicativePersistence(int num) {
return getMultiplicativePersistence(num, 0);
}

int getMultiplicativePersistence(int num, int count) {
String numStr = String.valueOf(num);
if (numStr.length() == 1) {
return count;
}

int newNum = 1;
for (char c : numStr.toCharArray()) {
newNum *= Integer.valueOf(String.valueOf(c));
}
return getMultiplicativePersistence(newNum, ++count);
}

public static void main (String[] args) {
Scanner s = new Scanner(System.in);
MultiplicativePersistence c = new MultiplicativePersistence();
System.out.print(c.MultiplicativePersistence(39));
}
}

Multiplicative Persistence Python

def MultiplicativePersistence(num):
steps = 0
while num > 9:
snum = str(num)
sdigits = list(snum)
num = 1
for snum in sdigits:
n = int(snum)
num = num * n
steps = steps + 1
return steps

print MultiplicativePersistence(raw_input())

Multiplicative Persistence Javascript

Using the JavaScript language, have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it (ie. if num = 4, return (4 * 3 * 2 * 1)). For the test cases, the range will be between 1 and 18.

function MultiplicativePersistence(num) {

var sum, loop = 0;
var val1 = num.toString(10).split("");
while( val1.length > 1 ) {
sum = 1;
val1.forEach( function(number) {
sum = sum * number;
});
val1 = sum.toString(10).split("");
loop++;
} ;
return loop;
}

Explanation

The challenge passes a string but it expects us to do Math on it so it needs to be converted to numbers. I will use the base 10 parameter of the toString() function to convert each entry in the array to a Number. Then I am going to multiply each entry in the array to get a total. I will repeat this until my total is a single digit number. The number of times I multiplied is returned as the answer.

Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.