Skip to content

Instantly share code, notes, and snippets.

@Lait-au-Cafe
Created August 14, 2019 14:10
Show Gist options
  • Save Lait-au-Cafe/a1754f786ce53c906c32e48d7c7d992c to your computer and use it in GitHub Desktop.
Save Lait-au-Cafe/a1754f786ce53c906c32e48d7c7d992c to your computer and use it in GitHub Desktop.
Product of digits.
use std::time::Instant;
fn split(mut n: u128) -> Vec<u8> {
let mut elems: Vec<u8> = Vec::new();
while n != 0 {
let e = n % 10;
elems.push(e as u8);
n /= 10;
}
return elems;
}
fn count(mut n: u128) -> i32 {
let mut cnt = 0;
loop {
let elems = split(n);
if elems.len() <= 1 { break; }
cnt += 1;
n = elems.into_iter().fold(1, |acc: u128, x: u8| acc * (x as u128));
}
return cnt;
}
fn main() {
let start_time = Instant::now();
let mut cnt_max = 0;
for n in 0_u128.. {
let c = count(n);
if c > cnt_max {
println!("[Updated!!] {}: {} (Time Elapsed: {} sec)", n, count(n), start_time.elapsed().as_secs_f32());
cnt_max = c;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment