per/src/main.rs

88 lines
2.2 KiB
Rust
Raw Normal View History

2019-03-22 15:47:19 +01:00
extern crate num_bigint;
// extern crate num_traits;
use num_bigint::BigUint;
2019-03-24 18:07:14 +01:00
struct Counter {
length: u64,
prefix: u8,
last: BigUint,
total: BigUint
}
impl Counter {
fn new() -> Counter {
Counter {length: 1, prefix: 0, last: BigUint::from(0u8), total: BigUint::from(0u8)}
}
}
impl Iterator for Counter {
type Item = BigUint;
fn next(&mut self) -> Option<BigUint> {
let old = self.last;
let old_str = old.to_str_radix(10);
let mut new_str = old_str;
let mut carry = true;
let mut index = 0;
2019-03-22 15:47:19 +01:00
2019-03-24 18:07:14 +01:00
while carry != true {
match old_str[old_str.chars().count() - 1 - index] {
'5' => {carry = false; new_str[new_str.chars().count() - 1 - index] = '7'},
'7' => {carry = false; new_str[new_str.chars().count() - 1 - index] = '8'},
'8' => {carry = false; new_str[new_str.chars().count() - 1 - index] = '9'},
'9' => {carry = true; index += 1; new_str[new_str.chars().count() - 1 - index + 1] = '5'}
}
}
if carry == true {
self.prefix += 1;
}
let new_string = String::new();
new_string.push_str(match self.prefix {
0 => "",
1 => "2",
2 => "3",
3 => "4",
4 => "6",
5 => "26"
});
new_string.push_str(new_str);
return BigUint::from_str(new_string);
}
}
fn main() {
2019-03-22 15:47:19 +01:00
let mut num = BigUint::from(0 as u8);
2019-03-24 18:07:14 +01:00
// num = num + 277777788888899u128;
let mut max: u8 = 0;
2019-03-22 15:47:19 +01:00
2019-03-24 18:07:14 +01:00
let mut n = test(num.clone());
while n < 10 {
n = test(num.clone());
if (n > max) {
max = n;
println!("{}: {}", n, num);
}
num += 1u8;
}
2019-03-22 15:47:19 +01:00
}
fn test(input: BigUint) -> u8 {
let mut n = factorize(input);
let mut counter = 1;
2019-03-22 17:27:22 +01:00
while (n.to_str_radix(10).chars().count() > 1) {
2019-03-22 15:47:19 +01:00
n = factorize(n);
2019-03-24 18:07:14 +01:00
// println!("n: {:?}", n);
2019-03-22 15:47:19 +01:00
counter += 1;
}
return counter;
}
fn factorize(input: BigUint) -> BigUint {
let string = input.to_str_radix(10);
let numbers: Vec<u8> = string.chars().map(|x| x as u8 - 48).collect();
numbers.into_iter().product::<BigUint>()
2019-03-24 18:07:14 +01:00
}