Skip to content

Instantly share code, notes, and snippets.

@solson
Forked from anonymous/playground.rs
Created December 15, 2017 20:15
Show Gist options
  • Save solson/c87de0033d871c5e439db89a9e2bdddb to your computer and use it in GitHub Desktop.
Save solson/c87de0033d871c5e439db89a9e2bdddb to your computer and use it in GitHub Desktop.
Rust code shared from the playground
// const STARTING_A: i64 = 65;
// const STARTING_B: i64 = 8921;
const STARTING_A: i64 = 634;
const STARTING_B: i64 = 301;
const FACTOR_A: i64 = 16807;
const FACTOR_B: i64 = 48271;
const DIVISOR_A: i64 = 4;
const DIVISOR_B: i64 = 8;
const ITERATIONS: usize = 5_000_000;
fn generate(state: &mut i64, factor: i64, divisor: i64) {
loop {
*state = (*state * factor) % (i32::max_value() as i64);
if *state % divisor == 0 { break; }
}
}
fn main() {
let mut state_a = STARTING_A;
let mut state_b = STARTING_B;
let mut matches = 0;
for _ in 0..ITERATIONS {
generate(&mut state_a, FACTOR_A, DIVISOR_A);
generate(&mut state_b, FACTOR_B, DIVISOR_B);
if state_a as u16 == state_b as u16 {
matches += 1;
}
}
println!("{}", matches);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment