use crate::Solution;
problem!(Problem0045, 45, "Triangular, Pentagonal, and Hexagonal");
impl Solution for Problem0045 {
fn solve(&self) -> String {
let mut n = 144;
let mut hex_num = n * (2 * n - 1);
while !is_pentagonal(hex_num) {
n += 1;
hex_num = n * (2 * n - 1);
}
hex_num.to_string()
}
}
fn is_pentagonal(num: u64) -> bool {
let i = (1.0 + ((1 + 24 * num) as f64).sqrt()) / 6.0;
(i.round() - i).abs() <= 1e-10
}