-
-
Save solson/c87de0033d871c5e439db89a9e2bdddb to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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