Created
September 25, 2018 11:07
-
-
Save JellyWX/b50fe14e587763fddd1f3d24fbc478f0 to your computer and use it in GitHub Desktop.
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
fn main() | |
{ | |
const RANGES: [i32; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
let mut triangles: i32 = 0; | |
for a in 0..RANGES.len()-2 | |
{ | |
for b in 1..RANGES.len()-a | |
{ | |
for c in 1..RANGES.len()-a-b | |
{ | |
let t: bool = triangle(RANGES[a], RANGES[a+b], RANGES[a+b+c]); | |
if t | |
{ | |
println!("{}:{}:{} is a triangle", RANGES[a], RANGES[a+b], RANGES[a+b+c]); | |
triangles += 1; | |
} | |
else | |
{ | |
println!("{}:{}:{} is not a triangle", RANGES[a], RANGES[a+b], RANGES[a+b+c]); | |
} | |
} | |
} | |
} | |
println!("Number of triangles: {}", triangles); | |
} | |
fn triangle(len1: i32, len2: i32, len3: i32) -> bool | |
{ | |
let lengths: [&i32; 3] = [&len1, &len2, &len3]; | |
let mut hyp: &i32 = &len1; | |
let mut total: i32 = 0; | |
for length in lengths.iter() | |
{ | |
if *length > hyp | |
{ | |
hyp = length; | |
} | |
total += *length; | |
} | |
total -= hyp; | |
/* | |
println!("{}", hyp); | |
println!("{}", total); | |
*/ | |
if total > *hyp | |
{ | |
true | |
} | |
else | |
{ | |
false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment