Skip to content

Commit

Permalink
prob28: solved
Browse files Browse the repository at this point in the history
  • Loading branch information
cpea2506 committed May 18, 2024
1 parent 83bd963 commit 4af4508
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/bin/prob28.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Number Spiral Diagonals

trait Math {
fn arithmetic_sum(self) -> usize;
}

impl Math for usize {
fn arithmetic_sum(self) -> usize {
8 * self * (self + 1) * (2 * self + 1) / 3 + 2 * self * (self + 1) + 4 * self + 1
}
}

fn spiral_diagonals_sum(length: usize) -> usize {
assert!(length % 2 != 0, "The length of matrix must be odd!");

(length / 2).arithmetic_sum()
}

pj_euler::run!("Number Spiral Diagonals", spiral_diagonals_sum(1001));

pj_euler::test!(
number_spiral_diagonals{
{sum_spiral_diagonals_of_3x3_matrix, spiral_diagonals_sum(3), 25},
{sum_spiral_diagonals_of_5x5_matrix, spiral_diagonals_sum(5), 101},
{sum_spiral_diagonals_of_7x7_matrix, spiral_diagonals_sum(7), 261},
}
);

0 comments on commit 4af4508

Please sign in to comment.