Skip to content

Commit 61a2845

Browse files
feat: adds Talia's prime checker implementations (#143)
* feat: add JS and TS prime number implementations to README * feat: add comparison and analysis section to README * docs: update differences section between TypeScript and JavaScript * docs: adds similarities section between TypeScript and JavaScript * fix: correct markdown formatting in similarities section * fix: corrects markdown formatting in similarities section again * feat: implement prime number checker in JS/TS with tests
1 parent 887c461 commit 61a2845

File tree

7 files changed

+333
-0
lines changed

7 files changed

+333
-0
lines changed

lesson_04/taliacrockett/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## JavaScript Implementation
2+
3+
```javascript
4+
function isPrime(number) {
5+
if (number <= 1) return false;
6+
if (number === 2) return true;
7+
if (number % 2 === 0) return false;
8+
9+
for (let i = 3; i <= Math.sqrt(number); i += 2) {
10+
if (number % i === 0) return false;
11+
}
12+
return true;
13+
}
14+
15+
// Example usage:
16+
console.log(isPrime(17)); // Output: true
17+
console.log(isPrime(25)); // Output: false
18+
```
19+
20+
## TypeScript implementation
21+
22+
```typescript
23+
function isPrime(number: number): boolean {
24+
if (number <= 1) {
25+
return false;
26+
}
27+
28+
if (number <= 3) {
29+
return true;
30+
}
31+
32+
if (number % 2 === 0 || number % 3 === 0) {
33+
return false;
34+
}
35+
36+
for (let i = 5; i * i <= number; i += 6) {
37+
if (number % i === 0 || number % (i + 2) === 0) {
38+
return false;
39+
}
40+
}
41+
42+
return true;
43+
}
44+
45+
// Example usage:
46+
console.log(isPrime(17)); // Output: true
47+
console.log(isPrime(25)); // Output: false
48+
```
49+
50+
## Differences Between TypeScript and JavaScript
51+
52+
### TypeScript Features:
53+
- **Type Safety**: Uses `number` and `boolean` type annotations
54+
- **Compile-time Error Checking**: Catches type errors before runtime
55+
- **Better Code Documentation**: Types make the function signature clearer
56+
- **Advanced Algorithm**: Uses 6k±1 optimization for better performance
57+
58+
### JavaScript Features:
59+
- **Dynamic Typing**: No type declarations needed
60+
- **Simpler Syntax**: Less verbose, easier to write quickly
61+
- **Runtime Flexibility**: Types can change during execution
62+
- **Basic Algorithm**: Uses straightforward Math.sqrt() optimization
63+
64+
### Key Differences:
65+
Both solve the same problem but TypeScript provides more safety and documentation through its type system, while JavaScript offers simplicity and flexibility. The TypeScript version also uses a more mathematically advanced algorithm that performs better on larger numbers.
66+
67+
## Similarities Between TypeScript and JavaScript
68+
- **Conditional statements**: Both use identical conditional statements (if, else), loops (for), and operators (%, ===, <=)
69+
- **Function Structure**: Both define functions with parameters and return values
70+
- **Variable Declarations**: Both use let for variable declarations
71+
- **Mathematical Operations**: Both perform the same modulo (%) and comparison operations

lesson_04/taliacrockett/isPrime.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Determines if a number is prime
3+
* @param {number} number - The number to check
4+
* @returns {boolean} - True if the number is prime, false otherwise
5+
*/
6+
function isPrime(number) {
7+
if (number <= 1) return false;
8+
if (number === 2) return true;
9+
if (number % 2 === 0) return false;
10+
11+
for (let i = 3; i <= Math.sqrt(number); i += 2) {
12+
if (number % i === 0) return false;
13+
}
14+
return true;
15+
}
16+
17+
// Export for testing (Node.js style)
18+
if (typeof module !== 'undefined' && module.exports) {
19+
module.exports = isPrime;
20+
}
21+
22+
// Example usage:
23+
console.log(isPrime(17)); // Output: true
24+
console.log(isPrime(25)); // Output: false
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const isPrime = require('./isPrime');
2+
3+
describe('isPrime function tests', () => {
4+
5+
test('should return false for numbers less than or equal to 1', () => {
6+
expect(isPrime(-5)).toBe(false);
7+
expect(isPrime(0)).toBe(false);
8+
expect(isPrime(1)).toBe(false);
9+
});
10+
11+
test('should return true for prime number 2', () => {
12+
expect(isPrime(2)).toBe(true);
13+
});
14+
15+
test('should return true for small prime numbers', () => {
16+
expect(isPrime(3)).toBe(true);
17+
expect(isPrime(5)).toBe(true);
18+
expect(isPrime(7)).toBe(true);
19+
expect(isPrime(11)).toBe(true);
20+
expect(isPrime(13)).toBe(true);
21+
});
22+
23+
test('should return false for small composite numbers', () => {
24+
expect(isPrime(4)).toBe(false);
25+
expect(isPrime(6)).toBe(false);
26+
expect(isPrime(8)).toBe(false);
27+
expect(isPrime(9)).toBe(false);
28+
expect(isPrime(10)).toBe(false);
29+
expect(isPrime(12)).toBe(false);
30+
});
31+
32+
test('should return true for larger prime numbers', () => {
33+
expect(isPrime(17)).toBe(true);
34+
expect(isPrime(19)).toBe(true);
35+
expect(isPrime(23)).toBe(true);
36+
expect(isPrime(29)).toBe(true);
37+
expect(isPrime(97)).toBe(true);
38+
});
39+
40+
test('should return false for larger composite numbers', () => {
41+
expect(isPrime(15)).toBe(false);
42+
expect(isPrime(21)).toBe(false);
43+
expect(isPrime(25)).toBe(false);
44+
expect(isPrime(49)).toBe(false);
45+
expect(isPrime(100)).toBe(false);
46+
});
47+
48+
test('should handle edge cases correctly', () => {
49+
expect(isPrime(2)).toBe(true); // Smallest prime
50+
expect(isPrime(4)).toBe(false); // Smallest composite > 1
51+
});
52+
53+
test('should work with larger numbers', () => {
54+
expect(isPrime(101)).toBe(true); // Prime
55+
expect(isPrime(121)).toBe(false); // 11^2
56+
expect(isPrime(997)).toBe(true); // Large prime
57+
expect(isPrime(1000)).toBe(false); // Large composite
58+
});
59+
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import isPrime from './isPrime';
2+
3+
describe('isPrime TypeScript function tests', () => {
4+
5+
test('should return false for numbers less than or equal to 1', () => {
6+
expect(isPrime(-5)).toBe(false);
7+
expect(isPrime(0)).toBe(false);
8+
expect(isPrime(1)).toBe(false);
9+
});
10+
11+
test('should return true for prime number 2', () => {
12+
expect(isPrime(2)).toBe(true);
13+
});
14+
15+
test('should return true for prime number 3', () => {
16+
expect(isPrime(3)).toBe(true);
17+
});
18+
19+
test('should return true for small prime numbers', () => {
20+
expect(isPrime(5)).toBe(true);
21+
expect(isPrime(7)).toBe(true);
22+
expect(isPrime(11)).toBe(true);
23+
expect(isPrime(13)).toBe(true);
24+
expect(isPrime(17)).toBe(true);
25+
expect(isPrime(19)).toBe(true);
26+
});
27+
28+
test('should return false for small composite numbers', () => {
29+
expect(isPrime(4)).toBe(false);
30+
expect(isPrime(6)).toBe(false);
31+
expect(isPrime(8)).toBe(false);
32+
expect(isPrime(9)).toBe(false);
33+
expect(isPrime(10)).toBe(false);
34+
expect(isPrime(12)).toBe(false);
35+
expect(isPrime(14)).toBe(false);
36+
expect(isPrime(15)).toBe(false);
37+
expect(isPrime(16)).toBe(false);
38+
expect(isPrime(18)).toBe(false);
39+
});
40+
41+
test('should return true for larger prime numbers', () => {
42+
expect(isPrime(23)).toBe(true);
43+
expect(isPrime(29)).toBe(true);
44+
expect(isPrime(31)).toBe(true);
45+
expect(isPrime(37)).toBe(true);
46+
expect(isPrime(97)).toBe(true);
47+
expect(isPrime(101)).toBe(true);
48+
});
49+
50+
test('should return false for larger composite numbers', () => {
51+
expect(isPrime(21)).toBe(false);
52+
expect(isPrime(25)).toBe(false);
53+
expect(isPrime(49)).toBe(false);
54+
expect(isPrime(100)).toBe(false);
55+
expect(isPrime(121)).toBe(false); // 11^2
56+
expect(isPrime(169)).toBe(false); // 13^2
57+
});
58+
59+
test('should handle perfect squares correctly', () => {
60+
expect(isPrime(4)).toBe(false); // 2^2
61+
expect(isPrime(9)).toBe(false); // 3^2
62+
expect(isPrime(25)).toBe(false); // 5^2
63+
expect(isPrime(49)).toBe(false); // 7^2
64+
});
65+
66+
test('should work efficiently with larger numbers', () => {
67+
expect(isPrime(997)).toBe(true); // Large prime
68+
expect(isPrime(1009)).toBe(true); // Large prime
69+
expect(isPrime(1000)).toBe(false); // Large composite
70+
expect(isPrime(1001)).toBe(false); // 7 × 11 × 13
71+
});
72+
73+
test('should handle numbers divisible by 2 or 3', () => {
74+
expect(isPrime(6)).toBe(false); // divisible by both 2 and 3
75+
expect(isPrime(12)).toBe(false); // divisible by both 2 and 3
76+
expect(isPrime(18)).toBe(false); // divisible by both 2 and 3
77+
});
78+
});

lesson_04/taliacrockett/isPrime.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Determines if a number is prime using optimized algorithm
3+
* @param number - The number to check
4+
* @returns True if the number is prime, false otherwise
5+
*/
6+
function isPrime(number: number): boolean {
7+
if (number <= 1) {
8+
return false;
9+
}
10+
11+
if (number <= 3) {
12+
return true;
13+
}
14+
15+
if (number % 2 === 0 || number % 3 === 0) {
16+
return false;
17+
}
18+
19+
for (let i = 5; i * i <= number; i += 6) {
20+
if (number % i === 0 || number % (i + 2) === 0) {
21+
return false;
22+
}
23+
}
24+
25+
return true;
26+
}
27+
28+
// Export for testing
29+
export default isPrime;
30+
31+
// Example usage:
32+
console.log(isPrime(17)); // Output: true
33+
console.log(isPrime(25)); // Output: false
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "prime-number-checker",
3+
"version": "1.0.0",
4+
"description": "Prime number detection implementations in JavaScript and TypeScript with unit tests",
5+
"main": "isPrime.js",
6+
"scripts": {
7+
"test": "jest",
8+
"test:js": "jest isPrime.test.js",
9+
"test:ts": "jest isPrime.test.ts",
10+
"test:watch": "jest --watch",
11+
"build": "tsc",
12+
"start": "node isPrime.js"
13+
},
14+
"devDependencies": {
15+
"@types/jest": "^29.5.0",
16+
"@types/node": "^18.15.0",
17+
"jest": "^29.5.0",
18+
"ts-jest": "^29.1.0",
19+
"typescript": "^5.0.0"
20+
},
21+
"jest": {
22+
"preset": "ts-jest",
23+
"testEnvironment": "node",
24+
"roots": ["<rootDir>"],
25+
"testMatch": ["**/*.test.(js|ts)"],
26+
"transform": {
27+
"^.+\\.ts$": "ts-jest",
28+
"^.+\\.js$": "babel-jest"
29+
}
30+
}
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "commonjs",
5+
"lib": ["ES2020"],
6+
"outDir": "./dist",
7+
"rootDir": "./",
8+
"strict": true,
9+
"esModuleInterop": true,
10+
"skipLibCheck": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"declaration": true,
13+
"declarationMap": true,
14+
"sourceMap": true,
15+
"removeComments": false,
16+
"noImplicitAny": true,
17+
"strictNullChecks": true,
18+
"strictFunctionTypes": true,
19+
"noImplicitThis": true,
20+
"noImplicitReturns": true,
21+
"noFallthroughCasesInSwitch": true,
22+
"moduleResolution": "node",
23+
"allowSyntheticDefaultImports": true,
24+
"experimentalDecorators": true,
25+
"emitDecoratorMetadata": true,
26+
"resolveJsonModule": true,
27+
"isolatedModules": true
28+
},
29+
"include": [
30+
"*.ts",
31+
"*.test.ts"
32+
],
33+
"exclude": [
34+
"node_modules",
35+
"dist"
36+
]
37+
}

0 commit comments

Comments
 (0)