And if you consider that parts of a number should not necessarily be of equal size, the solution becomes a bit more wordy (suppose, a lot can be improved here):
fn main() { (1usize..) .into_iter() .filter(is_kaprekar) .take(8) .for_each(|num| println!("{}", num)); } fn is_kaprekar(num: &usize) -> bool { NumberParts::of(num * num) .any(|(left, right)| { right > 0 && left + right == *num }) } struct NumberParts { original_number: usize, split_position: usize, } impl NumberParts { fn of(original_number: usize) -> Self { NumberParts { original_number, split_position: 10usize.pow( (original_number as f64).log(10f64) as u32 + 1, ), } } } impl Iterator for NumberParts { type Item = (usize, usize); fn next(&mut self) -> Option<Self::Item> { let split = self.split_position; if split == 1 { None } else { self.split_position /= 10; Some(( self.original_number / split, self.original_number % split, )) } } }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
And if you consider that parts of a number should not necessarily be of equal size, the solution becomes a bit more wordy (suppose, a lot can be improved here):