Closed
Description
This is a performance bug report.
This little program solves a problem derived from this (a ten times larger problem):
https://projecteuler.net/problem=55
The solution (this is not the fastest solution for this problem, but it's a easy to understand one):
#![feature(i128_type)]
fn is_lychrel(mut n: u128) -> bool {
for _ in 0 .. 50 {
n += n.to_string().chars().rev().collect::<String>().parse().unwrap();
if n.to_string() == n.to_string().chars().rev().collect::<String>() {
return false;
}
}
true
}
fn e55() -> usize {
(0u32 .. 100_000).filter(|&i| is_lychrel(i.into())).count()
}
fn main() {
println!("{}", e55() == 6208);
}
On my PC it runs in about 1.12 seconds.
Using an external crate for the u128 bit values, with the same code (extprim = "1.1.0"):
extern crate extprim;
use extprim::u128::u128;
fn is_lychrel(mut n: u128) -> bool {
for _ in 0 .. 50 {
n += n.to_string().chars().rev().collect::<String>().parse().unwrap();
if n.to_string() == n.to_string().chars().rev().collect::<String>() {
return false;
}
}
true
}
fn e55() -> usize {
(0u32 .. 100_000).filter(|&i| is_lychrel(i.into())).count()
}
fn main() {
println!("{}", e55() == 6208);
}
This runs in about 0.72 seconds.