diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 2e65742..dc334c2 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -294,3 +294,5 @@ mod p3259_maximum_energy_boost_from_two_drinks; mod p3226_number_of_bit_changes_to_make_two_integers_equal; mod p638_shopping_offers; + +mod p633_sum_of_square_numbers; diff --git a/src/problem/p633_sum_of_square_numbers.rs b/src/problem/p633_sum_of_square_numbers.rs new file mode 100644 index 0000000..5c9a56d --- /dev/null +++ b/src/problem/p633_sum_of_square_numbers.rs @@ -0,0 +1,44 @@ +/** + * [633] Sum of Square Numbers + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn judge_square_sum(c: i32) -> bool { + use std::collections::HashSet; + + let mut set = HashSet::new(); + let mut i = 0i64; + let c = c as i64; + + while i.pow(2) <= c { + let value = c - i.pow(2); + + set.insert(i.pow(2)); + if set.contains(&value) { + return true; + } + + i += 1; + } + + false + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_633() { + assert!(Solution::judge_square_sum(5)); + assert!(Solution::judge_square_sum(4)); + assert!(!Solution::judge_square_sum(3)); + assert!(Solution::judge_square_sum(2)); + } +}