Performing the Lychrel algorithm in Rust A Lychrel number is a natural number that cannot form a palindrome through the iterative process of repeatedly reversing its digits and adding the resulting numbers.


In the code below I'm trying to calculate the palindrome that comes from the end of this algorithm up to some limit (as it is hypothesized to be infinite for some values).

extern crate num;
extern crate time;

use num::bigint::BigInt;
use num::FromPrimitive;
use time::PreciseTime;
use std::env;

// The limit for number of iterations to run.
static LIMIT: u64 = 5000;

// Return the "lychrel" number for a given input, or the number reached
// at iteration LIMIT, whichever comes first.
fn lychrel(number: u64) -&gt; BigInt {
    let mut next = FromPrimitive::from_u64(number).unwrap();
    let mut iterations = 0;
    let mut rev;

    loop {
        // rev = reverse(&amp;next, &amp;zero);
        rev = reverse_str(&amp;next);

        if &amp;next == &amp;rev {
            break;
        }

        next = &amp;next + &amp;rev;

        if iterations % 1000 == 0 { println!("{} iterations complete.", iterations) }
        if iterations == LIMIT { break; }

        iterations += 1;
    }

    next
}

// Integer function is nearly 4 (!) times slower.
// So this function has been removed, in favour of reverse_str()
// fn reverse(number: &amp;BigInt, zero: &amp;BigInt) -&gt; BigInt {
//  let zero: BigInt = FromPrimitive::from_u64(0).unwrap();
//  let mut reverse = FromPrimitive::from_u64(0).unwrap();
//  let mut temp = number.clone();

//  while &amp;temp &gt; &amp;zero {
//      reverse = reverse * 10 + &amp;temp % 10;
//      temp = &amp;temp / 10;
//  }

//  reverse
// }

// Transform a BigInt into a char array, reverse and parse back into a BigInt.
fn reverse_str(number: &amp;BigInt) -&gt; BigInt {
    let reverse_string: String = number.to_string().chars().rev().collect();
    let reverse_value: BigInt = reverse_string.parse().unwrap();
    number + reverse_value
}

fn main() {
    let args: Vec&lt;_&gt; = env::args().collect();
    let start = PreciseTime::now();

    let input = args[1].parse::&lt;u64&gt;().expect("Usage: ./lychrel &lt;number&gt;");

    lychrel(input);
    // println!("{}", lychrel(input));

    let end = PreciseTime::now();
    println!("Processed {} iterations in {} seconds.", LIMIT, start.to(end));
}


(Note that this requires the num and time crates to be installed)

This is my first time using Rust, so I'm finding the entire idea of "ownership" pretty new. Am I passing data by reference too much?  I seem to have a lot of ampersands.
Use const instead of static if you don't need to take the address of the global.
Use for over a range instead of mutating a counter.
It's a little strange to return a value when the iteration runs out. Perhaps an Option would be better?
There's no need to take references just to compare for equality; eq takes &amp;self.
Don't need &amp;next or &amp;rev when adding to get next because we no longer need these values.
reverse_str does more than just reverse; perhaps a better name is in order?
No need to collect all the arguments to an array, just use the iterator directly.




extern crate num;
extern crate time;

use num::bigint::BigInt;
use num::FromPrimitive;
use time::PreciseTime;
use std::env;

const LIMIT: u64 = 5000;

fn lychrel(number: u64) -&gt; BigInt {
    let mut next = FromPrimitive::from_u64(number).unwrap();
    let mut rev;

    for i in 0..LIMIT {
        rev = reverse_str(&amp;next);

        if next == rev {
            break;
        }

        next = next + rev;

        if i % 1000 == 0 {
            println!("{} iterations complete.", i)
        }
    }

    next
}

fn reverse_str(number: &amp;BigInt) -&gt; BigInt {
    let reverse_string: String = number.to_string().chars().rev().collect();
    let reverse_value: BigInt = reverse_string.parse().unwrap();
    number + reverse_value
}

fn main() {
    let arg = env::args().nth(1).expect("Usage: ./lychrel &lt;number&gt;");

    let start = PreciseTime::now();
    let input = arg.parse::&lt;u64&gt;().expect("Argument must be a number");
    lychrel(input);
    let end = PreciseTime::now();

    println!("Processed {} iterations in {} seconds.", LIMIT, start.to(end));
}


I think it would be interesting to convert the iteration to an iterator.