Skip to content

Commit 7038c81

Browse files
committed
Multiplicative Persistence
1 parent 35ae53b commit 7038c81

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package easy;
2+
3+
/**
4+
* Have the function MultiplicativePersistence(num) take the num parameter being passed
5+
* which will always be a positive integer
6+
* and return its multiplicative persistence which is
7+
* the number of times you must multiply the digits in num until you reach a single digit.
8+
* ---
9+
* For example: if num is 39 then your program
10+
* should return 3 because 3 * 9 = 27 then 2 * 7 = 14
11+
* and finally 1 * 4 = 4 then you stop at 4.
12+
*/
13+
public class MultiplicativePersistence {
14+
15+
/**
16+
* Multiplicative Persistence function.
17+
*
18+
* @param num input number
19+
* @return the number of times you must multiply
20+
*/
21+
private static int multiplicativePersistence(int num) {
22+
int times = 0;
23+
int multiplied = num;
24+
while (multiplied > 9) {
25+
int product = 1;
26+
String[] intArr = Integer.toString(multiplied).split("");
27+
for (String i : intArr) {
28+
product *= Integer.parseInt(i);
29+
}
30+
multiplied = product;
31+
times++;
32+
}
33+
return times;
34+
}
35+
36+
/**
37+
* Entry point.
38+
*
39+
* @param args command line arguments
40+
*/
41+
public static void main(String[] args) {
42+
var result1 = multiplicativePersistence(2677889);
43+
System.out.println(result1);
44+
var result2 = multiplicativePersistence(8192);
45+
System.out.println(result2);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)