-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
feat: Fermat's last theorem test #1789
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
Closed
ridge-kimani
wants to merge
4
commits into
TheAlgorithms:master
from
ridge-kimani:feat/fermats-last-theorem
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Fermat's Last Theorem states: | ||
* | ||
* For every integer n >= 3, the equation | ||
* | ||
* x^n + y^n = z^n | ||
* | ||
* has no nontrivial integer solutions (x, y, z > 0). | ||
* | ||
* In contrast, for n = 2 there are infinitely many solutions (the famous | ||
* Pythagorean triples like 3^2 + 4^2 = 5^2). | ||
* | ||
* While Andrew Wiles proved this theorem in 1994 using advanced mathematics | ||
* (elliptic curves and modular forms), we can illustrate the theorem in JavaScript | ||
* by brute-force checking ranges of x, y, z for small exponents n. | ||
* | ||
* within checked ranges, no counterexamples exist for n >= 3. | ||
*/ | ||
|
||
/** | ||
* Check for counterexamples to Fermat's Last Theorem within a bounded range. | ||
* | ||
* This function uses BigInt arithmetic to handle large ranges safely. | ||
* | ||
* @param {number} maxValue - The maximum integer to check for x and y | ||
* @param {number} exponent - The power n (>= 3) | ||
* @returns {Array} - Array of counterexamples found (should be empty) | ||
*/ | ||
|
||
const checkFermatLastTheorem = (maxValue, exponent) => { | ||
if (exponent < 3) { | ||
throw new Error("Fermat's Last Theorem only applies for n >= 3") | ||
} | ||
|
||
const counterexamples = [] | ||
const exponentBigInt = BigInt(exponent) | ||
|
||
for (let baseX = 1; baseX <= maxValue; baseX++) { | ||
const baseXPowerN = BigInt(baseX) ** exponentBigInt | ||
|
||
for (let baseY = baseX; baseY <= maxValue; baseY++) { | ||
const baseYPowerN = BigInt(baseY) ** exponentBigInt | ||
const sumOfPowers = baseXPowerN + baseYPowerN | ||
|
||
// compute integer nth root of sumOfPowers using binary search | ||
let lowerBound = 1n | ||
let upperBound = sumOfPowers | ||
let potentialZ = 0n | ||
|
||
while (lowerBound <= upperBound) { | ||
const middle = (lowerBound + upperBound) >> 1n | ||
const middlePowerN = middle ** exponentBigInt | ||
|
||
if (middlePowerN === sumOfPowers) { | ||
potentialZ = middle | ||
break | ||
} else if (middlePowerN < sumOfPowers) { | ||
lowerBound = middle + 1n | ||
} else { | ||
upperBound = middle - 1n | ||
} | ||
} | ||
|
||
// exact check | ||
if (potentialZ > 0n && potentialZ ** exponentBigInt === sumOfPowers) { | ||
counterexamples.push({ | ||
x: baseX, | ||
y: baseY, | ||
z: potentialZ.toString(), | ||
n: exponent | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return counterexamples | ||
} | ||
|
||
export default checkFermatLastTheorem |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import checkFermatLastTheorem from '../FermatsLastTheoremTest.js' | ||
|
||
describe("Fermat's Last Theorem Checker (BigInt version)", () => { | ||
test('throws an error if exponent is less than 3', () => { | ||
expect(() => checkFermatLastTheorem(10, 2)).toThrow( | ||
"Fermat's Last Theorem only applies for n >= 3" | ||
) | ||
expect(() => checkFermatLastTheorem(10, 1)).toThrow() | ||
}) | ||
|
||
test('small range test: returns empty array for n = 3, max 20', () => { | ||
const results = checkFermatLastTheorem(20, 3) | ||
expect(results).toEqual([]) | ||
}) | ||
|
||
test('moderate range test: returns empty array for n = 3, max 1000', () => { | ||
const results = checkFermatLastTheorem(1000, 3) | ||
expect(results).toEqual([]) | ||
}) | ||
|
||
test('small range test: returns empty array for n = 4, max 20', () => { | ||
const results = checkFermatLastTheorem(20, 4) | ||
expect(results).toEqual([]) | ||
}) | ||
|
||
test('moderate range test: returns empty array for n = 4, max 500', () => { | ||
const results = checkFermatLastTheorem(500, 4) | ||
expect(results).toEqual([]) | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.