Created
February 14, 2022 18:05
-
-
Save micedreams/518e76f9bcc8f228e47f4210ee480cf0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
int resultPersistance; | |
resultPersistance = multiplicativePersistenceOf(489); | |
print("multiplicativePersistenceOf(489) =$resultPersistance"); | |
resultPersistance = multiplicativePersistenceOf(912); | |
print("multiplicativePersistenceOf(912) =$resultPersistance"); | |
} | |
int multiplicativePersistenceOf(int value) { | |
assert(value > 0); | |
int temp = value; | |
int result = 0; | |
while (getLength(temp) > 1) { | |
temp = multiply(temp); | |
result++; | |
} | |
return result; | |
} | |
getLength(number) { | |
return "$number".length; | |
} | |
multiply(int number) { | |
int product = 1; | |
String num = number.toString(); | |
List<int> numberArray = []; | |
for (int i = 0; i < num.length; i++) { | |
numberArray.add(int.parse(num[i])); | |
} | |
numberArray.map((e) => product = product * e).toList(); | |
return product; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment