Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions DIRECTORY.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
* [Factors](Maths/Factors.js)
* [FareyApproximation](Maths/FareyApproximation.js)
* [FermatPrimalityTest](Maths/FermatPrimalityTest.js)
* [FermatsLastTheoremTest](Maths/FermatsLastTheoremTest.js)
* [Fibonacci](Maths/Fibonacci.js)
* [FigurateNumber](Maths/FigurateNumber.js)
* [FindHcf](Maths/FindHcf.js)
Expand Down
79 changes: 79 additions & 0 deletions Maths/FermatsLastTheoremTest.js
View file Open in desktop
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
30 changes: 30 additions & 0 deletions Maths/test/FermatsLastTheoremTest.test.js
View file Open in desktop
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([])
})
})
Loading
Loading

AltStyle によって変換されたページ (->オリジナル) /