SHARE
TWEET

Untitled

a guest Oct 15th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn main() {
  2.     let n0:i32 = read();
  3.     let (max, i) = collatz(&mut n0);
  4.     println!("{}", i);
  5.     println!("{}", max);
  6. }
  7.  
  8. fn read<T: std::str::FromStr>() -> T {
  9.     let mut s = String::new();
  10.     std::io::stdin().read_line(&mut s).ok();
  11.     s.trim().parse().ok().unwrap()
  12. }
  13.  
  14. fn collatz(ni:&mut i32) -> (i32,i32) {
  15.     let mut max = 1_i32;
  16.     let mut index = 0_i32;
  17.     match ni.clone() {
  18.         1_i32 => (max,index),
  19.         _ => {
  20.             while ni.clone() != 1_i32 {
  21.                 ni = if *ni % 2_i32 == 0_i32{
  22.                     &mut ((*ni) / 2_i32)
  23.                 }else{
  24.                     &mut(3_i32 * (*ni) + 1_i32)
  25.                 };
  26.                 if max < ni {max = ni.clone()}
  27.                 index = index + 1_i32;
  28.             }
  29.              (max,index)
  30.         }
  31.     }
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top