-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve performance by checking if the text length is prime or not
- Loading branch information
1 parent
2266fd5
commit ce6a9f8
Showing
4 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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,13 @@ | ||
/** | ||
* @Method Returns if provided number is prime or not. | ||
* @param n - The number to check. | ||
* @returns boolean | ||
*/ | ||
export function isPrime(n: number): boolean { | ||
for (let i = 2; i <= Math.sqrt(n); i++) { | ||
if (n % i === 0) { | ||
return false; | ||
} | ||
} | ||
return n > 1; | ||
} |
This file contains 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 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,39 @@ | ||
import { isPrime } from '../src/util'; | ||
|
||
describe('isPrime', () => { | ||
it('should return true for 2', () => { | ||
expect(isPrime(2)).toBe(true); | ||
}); | ||
|
||
it('should return false for numbers < 2', () => { | ||
expect(isPrime(1)).toBe(false); | ||
expect(isPrime(0)).toBe(false); | ||
expect(isPrime(-1)).toBe(false); | ||
expect(isPrime(-7)).toBe(false); | ||
}); | ||
|
||
it('should return false for even numbers > 2', () => { | ||
expect(isPrime(4)).toBe(false); | ||
expect(isPrime(20)).toBe(false); | ||
expect(isPrime(40)).toBe(false); | ||
expect(isPrime(100)).toBe(false); | ||
}); | ||
|
||
it('should return false for odd numbers which are not prime', () => { | ||
expect(isPrime(9)).toBe(false); | ||
expect(isPrime(27)).toBe(false); | ||
expect(isPrime(9523371)).toBe(false); | ||
expect(isPrime(89525673)).toBe(false); | ||
}); | ||
|
||
it('should return true for prime numbers', () => { | ||
expect(isPrime(3)).toBe(true); | ||
expect(isPrime(5)).toBe(true); | ||
expect(isPrime(7)).toBe(true); | ||
expect(isPrime(11)).toBe(true); | ||
expect(isPrime(89)).toBe(true); | ||
expect(isPrime(97)).toBe(true); | ||
expect(isPrime(6523757)).toBe(true); | ||
expect(isPrime(76521967)).toBe(true); | ||
}); | ||
}); |