-
-
Save RandyMcMillan/69a50bdc05d6e1dc08936c6bbd16df64 to your computer and use it in GitHub Desktop.
theodorus.rs
This file contains hidden or 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
| use num_traits::Float; | |
| /// Calculates the lengths of the hypotenuses in the Spiral of Theodorus, | |
| /// which are the square roots of integers starting from sqrt(2). | |
| fn calculate_theodorus_hypotenuses<F: Float>(count: u32) -> Vec<F> { | |
| // We want roots from sqrt(2) up to sqrt(count + 1). | |
| // The number of triangles is 'count'. | |
| let mut lengths = Vec::with_capacity(count as usize); | |
| // The length of the first hypotenuse (the base of the spiral) is sqrt(1^2 + 1^2) = sqrt(2). | |
| let mut current_hypotenuse = F::from(1.0).unwrap().sqrt(); // Start with sqrt(1) = 1, will be squared in the loop | |
| let one = F::from(1.0).unwrap(); | |
| // The spiral starts with the hypotenuse sqrt(2) | |
| lengths.push(one.hypot(one)); // sqrt(1^2 + 1^2) = sqrt(2) | |
| // Calculate the remaining hypotenuses up to sqrt(17) | |
| // The previous hypotenuse 'h_n' and the constant side '1' form the legs of the | |
| // next triangle, with the next hypotenuse 'h_{n+1}' = sqrt(h_n^2 + 1^2). | |
| // Since h_n = sqrt(n), h_n^2 = n, so h_{n+1} = sqrt(n + 1). | |
| for n in 2..=(count as usize) { | |
| // The square of the previous hypotenuse is 'n-1'. | |
| let prev_hypotenuse_squared = F::from(n as f64 - 1.0).unwrap(); | |
| // The next hypotenuse is sqrt((n-1) + 1) = sqrt(n). | |
| // For the n-th iteration (starting at n=2), we calculate sqrt(n). | |
| let next_hypotenuse = (prev_hypotenuse_squared + one).sqrt(); | |
| lengths.push(next_hypotenuse); | |
| } | |
| lengths | |
| } | |
| fn main() { | |
| // The image shows hypotenuses from sqrt(2) up to sqrt(17). | |
| // This requires 16 triangles (n=2 to n=17). | |
| const NUM_TRIANGLES: u32 = 16; | |
| // We'll use f64 for precision. | |
| let hypotenuse_lengths: Vec<f64> = calculate_theodorus_hypotenuses(NUM_TRIANGLES); | |
| println!("📐 Spiral of Theodorus Hypotenuse Lengths (The square roots):"); | |
| println!("---"); | |
| // The lengths correspond to sqrt(2), sqrt(3), ..., sqrt(17). | |
| for (i, length) in hypotenuse_lengths.iter().enumerate() { | |
| // i starts at 0, representing sqrt(2). So we use i + 2 for the root number. | |
| let n = i + 2; | |
| println!("√{n:<2} = {length:.8}"); | |
| // Add a blank line for sqrt(4), sqrt(9), and sqrt(16) for visual grouping | |
| // as they represent integer lengths in the spiral. | |
| if n == 4 || n == 9 || n == 16 { | |
| println!("---"); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=4189e60ef84953357f30cfbfa1f1936d