Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat(euler44): look ahead checking saves iterations
  • Loading branch information
crhallberg committed Dec 16, 2025
commit 334cab4554be2acc1dda413545b46c7dad1e40db
14 changes: 11 additions & 3 deletions Project-Euler/Problem044.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ function problem44(k) {

for (let j = k - 1; j > 0; j--) {
const m = (j * (3 * j - 1)) / 2 // calculate all Pj < Pk
if (isPentagonal(n - m) && isPentagonal(n + m)) {
// Check sum and difference
return n - m // return D
// Check forward, checking n - m doubles work from previous iterations
const sum = n + m
if (isPentagonal(sum)) {
// Check sum - m = n
if (isPentagonal(sum + m)) {
return n // return D
}
// Check sum - n = m
if (isPentagonal(sum + n)) {
return m // return D
}
}
}
}
Expand Down