slovo/benchmarks/math-loop/rust/math_loop.rs
2026-05-22 08:38:43 +02:00

40 lines
823 B
Rust

const LOOP_COUNT: i32 = 1_000_000;
const EXPECTED_CHECKSUM: i32 = 5_000_001;
fn math_loop(limit: i32) -> i32 {
let mut i = 0;
let mut acc = 1;
while i < limit {
acc = acc + ((i + 3) * 2);
acc = if acc > 1_000_000_000 {
acc - 1_000_000_000
} else {
acc
};
i += 1;
}
acc
}
fn configured_loop_count() -> i32 {
let mut input = String::new();
if std::io::stdin().read_line(&mut input).is_err() {
return LOOP_COUNT;
}
input
.trim()
.parse::<i32>()
.ok()
.filter(|value| *value > 0)
.unwrap_or(LOOP_COUNT)
}
fn main() {
let result = math_loop(configured_loop_count());
println!("{}", result);
std::process::exit(if result == EXPECTED_CHECKSUM { 0 } else { 1 });
}