Skip to content

Update euler-0042.cpp #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

honeyranjan1
Copy link

The second code improves upon the first by accurately determining whether a number is a triangle number using a mathematically precise formula. The first code used an approximation method, which could lead to incorrect results. Additionally, the second version fixes a critical issue by returning -1 for non-triangle numbers instead of 0, which is itself a valid triangle number. These changes ensure correct output for all test cases.

Constant Name and Value Changed

const int NoTriangle = 0; → const int NotTriangle = -1;

Triangle Check Logic Modified

Old: n = sqrt(2 * x); → Checks if n(n+1)/2 == x

New: Solves using n = (-1 + sqrt(1 + 8x)) / 2 with full discriminant check

Return Value on Invalid Triangle

Old: Returns NoTriangle (0) if not a triangle

New: Returns NotTriangle (-1) if not a triangle

Condition in main() Updated

Old: if (getTriangle(sum) != NoTriangle)

New: if (getTriangle(sum) != NotTriangle)

Mathematical Accuracy

Old Code: Uses approximation (sqrt)

New Code: Uses exact formula with perfect square and integer check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant