SHARE
TWEET

Untitled

a guest Jul 19th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::time::Instant;
  2.  
  3. fn cycle_length(n : usize) -> usize {
  4.     let mut cycle = vec![0usize; n];
  5.     let mut length = 0;
  6.     let mut modulus = 1;
  7.     let mut last_modulus : usize;
  8.     loop {
  9.         modulus %= n;
  10.        
  11.         last_modulus = cycle[modulus];
  12.  
  13.         if last_modulus != 0 {
  14.             break;
  15.         }
  16.         cycle[modulus] = length;
  17.         length += 1;
  18.         modulus *= 10;
  19.     }
  20.     length - last_modulus
  21. }
  22.  
  23. pub fn reciprocal_cycles(n : usize) -> usize {
  24.     let mut max = 0;
  25.     let mut longest = 1;
  26.  
  27.     for i in (1..n).rev() {
  28.         if i < max {
  29.             break;
  30.         }
  31.  
  32.         let length = cycle_length(i);
  33.         if length > max {
  34.             max = length;
  35.             longest = i;
  36.         }
  37.        
  38.     }
  39.  
  40.     longest
  41. }
  42.  
  43. pub fn main() {
  44.     let now = Instant::now();
  45.     let solution = reciprocal_cycles(1000);
  46.     println!("{} in {:?}", solution, now.elapsed());
  47. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top