Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 10, 2025 15:41
Show Gist options
  • Save rust-play/0bc726f276943b5fb5f7fa8b821e1765 to your computer and use it in GitHub Desktop.
Save rust-play/0bc726f276943b5fb5f7fa8b821e1765 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn main() {
let mut sum: u64 = 0;
for x in 0..=32 {
// Calculate the term inside the summation
let term: f64 = (50.0 * 1e8) / (2.0_f64.powi(x as i32));
println!("{} {}", x, term.to_string());
// Take the floor of the result
let floor_term: u64 = term.floor() as u64;
// Add it to the sum
sum += floor_term;
}
// Multiply by 210000 as per the formula
let result = 210000 * sum;
assert_eq!(result, 2099999997690000);
println!("{}", result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment