Skip to content

Commit 70b4fc5

Browse files
feat: adds Devyn's custom code samples (#173)
* Created README.md of functions w/test cases * Added test and more checks for functions JS & Python
1 parent 888672a commit 70b4fc5

File tree

4 files changed

+334
-0
lines changed

4 files changed

+334
-0
lines changed

lesson_04/devynbenson/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## Python implementation
2+
3+
```python
4+
def prime_function(n : int) -> bool:
5+
if n <= 1:
6+
return False
7+
8+
if n <= 3:
9+
return True
10+
11+
if n % 2 == 0 or n % 3 == 0:
12+
return False
13+
14+
# Check for divisors from 5 to √n
15+
i = 5
16+
while i * i <= n:
17+
if n % i == 0 or n % (i + 2) == 0:
18+
return False
19+
i += 6
20+
21+
return True
22+
23+
24+
25+
# Example usage:
26+
print(prime_function(1)) # Output: False (only one positive divisor)
27+
print(prime_function(2)) # Output: True (only even prime)
28+
print(prime_function(4)) # Output: False (4 = 2×2, composite)
29+
print(prime_function(6)) # Output: False (6 = 2×3, composite)
30+
print(prime_function(9)) # Output: False (9 = 3×3, composite)
31+
```
32+
33+
## JavaScript implementation
34+
35+
```javascript
36+
function primeFunction(n) {
37+
if (!Number.isInteger(n) || n <= 1)
38+
return false;
39+
40+
if (n <= 3)
41+
return n >= 2;
42+
43+
if (n % 2 === 0 || n % 3 === 0)
44+
return false;
45+
46+
// Check for divisors from 5 to √n
47+
for (let i = 5; i * i <= n; i += 6) {
48+
if (n % i === 0 || n % (i + 2) === 0) {
49+
return false;
50+
}
51+
}
52+
53+
return true;
54+
}
55+
56+
// Example usage:
57+
console.log(primeFunction(1)); // Output: false (only one positive divisor)
58+
console.log(primeFunction(2)); // Output: true (only even prime)
59+
console.log(primeFunction(4)); // Output: false (4 = 2×2, composite)
60+
console.log(primeFunction(6)); // Output: false (6 = 2×3, composite)
61+
console.log(primeFunction(9)); // Output: false (9 = 3×3, composite)
62+
```
63+
64+
## Explanation
65+
66+
The **Python** implementation uses a function named ``prime_function`` that takes a ``single argument`` represented as an ``int`` (for my case) used to store ``whole numbers``, which can be ``positive, negative, or zero,`` ``WITHOUT ANY DECIMAL COMPONENT``. Returns ``True`` if ``n`` is a prime number; otherwise, returns ``False``. The function implements an optimized algorithm that first handles edge cases (``n <= 1`` returns ``False``, ``n <= 3`` returns ``True``), then checks divisibility by 2 and 3. For remaining candidates, it uses a ``while`` loop starting at ``i = 5`` and incrementing by 6 (``i += 6``) to check only numbers of the form ``6k±1``, testing divisors ``i`` and ``i + 2`` up to ``√n`` using the condition ``i * i <= n``.
67+
68+
The **JavaScript** implementation uses a function named ``primeFunction`` that also takes a ``single argument`` represented as a ``number`` (JavaScript's universal numeric type) used to store ``any numeric value``, including ``integers, floating-point numbers, positive, negative, or zero``. Returns ``true`` if ``n`` is a prime number; otherwise, returns ``false``. The function includes ``Number.isInteger(n)`` check to ensure the input is actually an integer before proceeding with prime validation. After handling edge cases with ``n <= 1`` and ``n <= 3`` conditions, it eliminates even numbers and multiples of 3. The core algorithm uses a ``for`` loop (``for (let i = 5; i * i <= n; i += 6)``) to efficiently test only potential divisors of the form ``6k±1``, checking both ``n % i === 0`` and ``n % (i + 2) === 0`` until reaching ``√n``.
69+
70+
71+
## Differences
72+
1. **Syntax, Typing, and Coercion**
73+
- **Function defs:** Python uses `def`; JavaScript uses `function` (or arrow `() => {}`).
74+
- **Booleans:** Python has `True` / `False`; JS has `true` / `false`.
75+
76+
77+
2. **Conditional Logic Differences**
78+
- **Edge case handling:**
79+
- Python: ``if n <= 3: return True`` (assumes 2 and 3 are prime after checking n <= 1)
80+
- JavaScript: ``if (n <= 3) return n >= 2`` (explicitly checks if n >= 2 for numbers 2 and 3)
81+
- **Input validation:**
82+
- Python: ``if n <= 1: return False`` (simple boundary check)
83+
- JavaScript: ``if (!Number.isInteger(n) || n <= 1) return false`` (combines type and boundary validation)
84+
85+
3. **Function Calls and Output**
86+
- **Printing:** Python uses ``print(prime_function(n))``; JavaScript uses ``console.log(primeFunction(n))``.
87+
88+
89+
## Similarities
90+
91+
- **Typing vibe:** You **don’t have** to declare argument types in either language.
92+
- **Core algorithm logic:** Both check divisibility by 2 and 3: ``n % 2 == 0 or n % 3 == 0`` (Python) vs ``n % 2 === 0 || n % 3 === 0`` (JavaScript).
93+
- **Functions are first-class:** Both languages treat functions as objects that can be passed around, returned, and stored.
94+
- **Control flow:** Both use identical ``if/else`` structures and early ``return`` statements for efficiency.
95+
- **Short-circuiting:** Python uses ``or`` operator; JavaScript uses ``||`` for the same logical short-circuit behavior.
96+
- **Modulo operation:** Both use ``%`` operator for checking divisibility in prime validation.
97+
- **Same parameter handling:** Both functions accept a ``single argument`` representing the number to test for primality.
98+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Prime Function Tests
2+
3+
This directory contains unit tests for both Python and JavaScript prime function implementations.
4+
5+
## Running Python Tests
6+
7+
```bash
8+
# Run the Python unit tests
9+
python test_prime_function.py
10+
11+
# Or using unittest module
12+
python -m unittest test_prime_function.py
13+
```
14+
15+
Expected output should show all tests passing:
16+
```
17+
......
18+
----------------------------------------------------------------------
19+
Ran 6 tests in 0.001s
20+
21+
OK
22+
```
23+
24+
## Running JavaScript Tests
25+
26+
```bash
27+
# Run the JavaScript tests
28+
node test_prime_function.js
29+
```
30+
31+
Expected output should show all tests passing with ✅ markers.
32+
33+
## Test Coverage
34+
35+
Both test suites include:
36+
- Edge cases (negative numbers, 0, 1, non-integers)
37+
- Small prime numbers (2, 3, 5, 7, 11, 13)
38+
- Small composite numbers (4, 6, 8, 9, 10, 12, 15)
39+
- Larger prime numbers (17, 19, 23, 29, 31, 97)
40+
- Larger composite numbers (21, 25, 49, 77, 91)
41+
42+
The tests verify the specific examples mentioned in the README:
43+
- 1 returns false (only one positive divisor)
44+
- 2 returns true (only even prime)
45+
- 4 returns false (4 = 2×2, composite)
46+
- 6 returns false (6 = 2×3, composite)
47+
- 9 returns false (9 = 3×3, composite)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
function primeFunction(n) {
2+
if (!Number.isInteger(n) || n <= 1)
3+
return false;
4+
5+
if (n <= 3)
6+
return n >= 2;
7+
8+
if (n % 2 === 0 || n % 3 === 0)
9+
return false;
10+
11+
// Check for divisors from 5 to √n
12+
for (let i = 5; i * i <= n; i += 6) {
13+
if (n % i === 0 || n % (i + 2) === 0) {
14+
return false;
15+
}
16+
}
17+
18+
return true;
19+
}
20+
21+
22+
// Simple test framework
23+
function test(description, testFunction) {
24+
try {
25+
testFunction();
26+
console.log(`PASS: ${description}`);
27+
} catch (error) {
28+
console.log(`FAIL: ${description}`);
29+
console.log(`ERROR: ${error.message}`);
30+
}
31+
}
32+
33+
function assertEqual(actual, expected, message = "") {
34+
if (actual !== expected) {
35+
throw new Error(`Expected ${expected}, but got ${actual}. ${message}`);
36+
}
37+
}
38+
39+
// Test Suite
40+
console.log("Running JavaScript Prime Function Tests...\n");
41+
42+
// Test edge cases
43+
test("Edge case: 1 (only one positive divisor)", () => {
44+
assertEqual(primeFunction(1), false);
45+
});
46+
47+
test("Edge case: 0", () => {
48+
assertEqual(primeFunction(0), false);
49+
});
50+
51+
test("Edge case: negative numbers", () => {
52+
assertEqual(primeFunction(-1), false);
53+
assertEqual(primeFunction(-5), false);
54+
});
55+
56+
test("Edge case: non-integer input", () => {
57+
assertEqual(primeFunction(2.5), false);
58+
assertEqual(primeFunction(3.14), false);
59+
});
60+
61+
// Test small primes
62+
test("Small prime: 2 (only even prime)", () => {
63+
assertEqual(primeFunction(2), true);
64+
});
65+
66+
test("Small primes: 3, 5, 7, 11, 13", () => {
67+
assertEqual(primeFunction(3), true);
68+
assertEqual(primeFunction(5), true);
69+
assertEqual(primeFunction(7), true);
70+
assertEqual(primeFunction(11), true);
71+
assertEqual(primeFunction(13), true);
72+
});
73+
74+
// Test composite numbers
75+
test("Composite: 4 (4 = 2×2)", () => {
76+
assertEqual(primeFunction(4), false);
77+
});
78+
79+
test("Composite: 6 (6 = 2×3)", () => {
80+
assertEqual(primeFunction(6), false);
81+
});
82+
83+
test("Composite: 9 (9 = 3×3)", () => {
84+
assertEqual(primeFunction(9), false);
85+
});
86+
87+
test("Composite numbers: 8, 10, 12, 15", () => {
88+
assertEqual(primeFunction(8), false);
89+
assertEqual(primeFunction(10), false);
90+
assertEqual(primeFunction(12), false);
91+
assertEqual(primeFunction(15), false);
92+
});
93+
94+
// Test larger primes
95+
test("Larger primes: 17, 19, 23, 29, 31, 97", () => {
96+
assertEqual(primeFunction(17), true);
97+
assertEqual(primeFunction(19), true);
98+
assertEqual(primeFunction(23), true);
99+
assertEqual(primeFunction(29), true);
100+
assertEqual(primeFunction(31), true);
101+
assertEqual(primeFunction(97), true);
102+
});
103+
104+
// Test larger composites
105+
test("Larger composites: 21, 25, 49, 77, 91", () => {
106+
assertEqual(primeFunction(21), false); // 3×7
107+
assertEqual(primeFunction(25), false); // 5×5
108+
assertEqual(primeFunction(49), false); // 7×7
109+
assertEqual(primeFunction(77), false); // 7×11
110+
assertEqual(primeFunction(91), false); // 7×13
111+
});
112+
113+
console.log("\nAll tests completed!");
114+
115+
// Export for potential use with other test frameworks
116+
if (typeof module !== 'undefined' && module.exports) {
117+
module.exports = primeFunction;
118+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import unittest
2+
3+
def prime_function(n: int) -> bool:
4+
if n <= 1:
5+
return False
6+
7+
if n <= 3:
8+
return True
9+
10+
if n % 2 == 0 or n % 3 == 0:
11+
return False
12+
13+
# Check for divisors from 5 to √n
14+
i = 5
15+
while i * i <= n:
16+
if n % i == 0 or n % (i + 2) == 0:
17+
return False
18+
i += 6
19+
20+
return True
21+
22+
23+
class TestPrimeFunction(unittest.TestCase):
24+
25+
def test_edge_cases(self):
26+
"""Test edge cases: numbers <= 1"""
27+
self.assertFalse(prime_function(1)) # only one positive divisor
28+
self.assertFalse(prime_function(0))
29+
self.assertFalse(prime_function(-1))
30+
self.assertFalse(prime_function(-5))
31+
32+
def test_small_primes(self):
33+
"""Test small prime numbers"""
34+
self.assertTrue(prime_function(2)) # only even prime
35+
self.assertTrue(prime_function(3))
36+
self.assertTrue(prime_function(5))
37+
self.assertTrue(prime_function(7))
38+
self.assertTrue(prime_function(11))
39+
self.assertTrue(prime_function(13))
40+
41+
def test_composite_numbers(self):
42+
"""Test composite numbers"""
43+
self.assertFalse(prime_function(4)) # 4 = 2×2, composite
44+
self.assertFalse(prime_function(6)) # 6 = 2×3, composite
45+
self.assertFalse(prime_function(8))
46+
self.assertFalse(prime_function(9)) # 9 = 3×3, composite
47+
self.assertFalse(prime_function(10))
48+
self.assertFalse(prime_function(12))
49+
self.assertFalse(prime_function(15))
50+
51+
def test_larger_primes(self):
52+
"""Test larger prime numbers"""
53+
self.assertTrue(prime_function(17))
54+
self.assertTrue(prime_function(19))
55+
self.assertTrue(prime_function(23))
56+
self.assertTrue(prime_function(29))
57+
self.assertTrue(prime_function(31))
58+
self.assertTrue(prime_function(97))
59+
60+
def test_larger_composites(self):
61+
"""Test larger composite numbers"""
62+
self.assertFalse(prime_function(21)) # 3×7
63+
self.assertFalse(prime_function(25)) # 5×5
64+
self.assertFalse(prime_function(49)) # 7×7
65+
self.assertFalse(prime_function(77)) # 7×11
66+
self.assertFalse(prime_function(91)) # 7×13
67+
68+
69+
if __name__ == '__main__':
70+
# Run the tests
71+
unittest.main()

0 commit comments

Comments
 (0)