mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 07:29:47 +08:00

* Updated Documentation in README.md * merge: Fix GetEuclidGCD (#1068) (#1069) * Fix GetEuclidGCD Implement the actual Euclidean Algorithm * Replace == with === * Lua > JS * Standard sucks * Oops * Update GetEuclidGCD.js * Updated Documentation in README.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> * feat: implement Shor's Algorithm * chore: add tests * Updated Documentation in README.md * chore: fix spelling Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
30 lines
793 B
JavaScript
30 lines
793 B
JavaScript
import { ShorsAlgorithm } from '../ShorsAlgorithm'
|
|
import { fermatPrimeCheck } from '../FermatPrimalityTest'
|
|
|
|
describe("Shor's Algorithm", () => {
|
|
const N = 10 // number of tests
|
|
const max = 35000 // max value to factorize
|
|
const min = 1000 // min value to factorize
|
|
|
|
for (let i = 0; i < N; i++) {
|
|
while (true) {
|
|
const num = Math.floor(Math.random() * max) + min
|
|
// num must be composite, don't care for false negatives
|
|
if (fermatPrimeCheck(num, 1)) continue
|
|
|
|
it('should find a non-trivial factor of ' + num, () => {
|
|
const f = ShorsAlgorithm(num)
|
|
|
|
// should not be trivial
|
|
expect(f).not.toEqual(1)
|
|
expect(f).not.toEqual(num)
|
|
|
|
// should be a factor
|
|
expect(num % f).toEqual(0)
|
|
})
|
|
|
|
break
|
|
}
|
|
}
|
|
})
|