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

Commit ffb138a

Browse files
Minor fixes
- Add default number of iterations - Correct "corner cases" to "edge cases" - Make clear which bounds are inclusive and which are exclusive
1 parent 09ac20d commit ffb138a

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

‎Maths/FermatPrimalityTest.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* However, there are certain numbers (so called Fermat Liars) that screw things up;
99
* if a is one of these liars the equation will hold even though p is composite.
1010
*
11-
* But not everything is lost! It's been proven that at least half of all integers aren't Fermat Liar (these ones called
11+
* But not everything is lost! It's been proven that at least half of all integers aren't Fermat Liars (these ones called
1212
* Fermat Witnesses). Thus, if we keep testing the primality with random integers, we can achieve higher reliability.
1313
*
1414
* The interesting about all of this is that since half of all integers are Fermat Witnesses, the precision gets really
@@ -60,14 +60,14 @@ const modularExponentiation = (base, exponent, modulus) => {
6060
* @param {number} numberOfIterations The number of times to apply Fermat's Little Theorem
6161
* @returns True if prime, false otherwise
6262
*/
63-
const fermatPrimeCheck = (n, numberOfIterations) => {
64-
// first check for corner cases
63+
const fermatPrimeCheck = (n, numberOfIterations=50) => {
64+
// first check for edge cases
6565
if (n <= 1 || n === 4) return false
6666
if (n <= 3) return true // 2 and 3 are included here
6767

6868
for (let i = 0; i < numberOfIterations; i++) {
69-
// pick a random number between 2 and n - 2
70-
const randomNumber = Math.floor(Math.random() * (n - 1-2) + 2)
69+
// pick a random number a, with 2 <= a < n - 2 (remember Math.random() range is [0, 1[ -> 1 exclusive)
70+
const randomNumber = Math.floor(Math.random() * (n - 2) + 2)
7171

7272
// if a^(n - 1) % n is different than 1, n is composite
7373
if (modularExponentiation(randomNumber, n - 1, n) !== 1) {

‎Maths/test/FermatPrimalityTest.test.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ describe('modularExponentiation', () => {
99

1010
describe('fermatPrimeCheck', () => {
1111
it('should give the correct output for prime and composite numbers', () => {
12-
expect(fermatPrimeCheck(2, 50)).toBe(true)
13-
expect(fermatPrimeCheck(10, 50)).toBe(false)
14-
expect(fermatPrimeCheck(94286167,50)).toBe(true)
15-
expect(fermatPrimeCheck(83165867,50)).toBe(true)
16-
expect(fermatPrimeCheck(13268774,50)).toBe(false)
17-
expect(fermatPrimeCheck(13233852,50)).toBe(false)
12+
expect(fermatPrimeCheck(2, 35)).toBe(true)
13+
expect(fermatPrimeCheck(10, 30)).toBe(false)
14+
expect(fermatPrimeCheck(94286167)).toBe(true)
15+
expect(fermatPrimeCheck(83165867)).toBe(true)
16+
expect(fermatPrimeCheck(13268774)).toBe(false)
17+
expect(fermatPrimeCheck(13233852)).toBe(false)
1818
})
1919
})

0 commit comments

Comments
(0)

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