mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
merge: Implement Shor's factorization algorithm (#1070)
* 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>
This commit is contained in:
29
Maths/test/ShorsAlgorithm.test.js
Normal file
29
Maths/test/ShorsAlgorithm.test.js
Normal file
@ -0,0 +1,29 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user