Skip to content

Instantly share code, notes, and snippets.

@micedreams
Created February 14, 2022 18:05
Show Gist options
  • Save micedreams/518e76f9bcc8f228e47f4210ee480cf0 to your computer and use it in GitHub Desktop.
Save micedreams/518e76f9bcc8f228e47f4210ee480cf0 to your computer and use it in GitHub Desktop.
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