Skip to content

Commit 36e3638

Browse files
Boyce007Daniel Boyce
andauthored
feat: adds Daniel's prime number checker (#135)
* feat:adds code for checking if a number is prime * chore:fixes junit imports to j unit 5 * feat:adds code to check prime numbers in pyhton with unit tests * test:adds more testing functions for java implementation along with readme * chore:added code for unit testing in readme * docs:added info of differences to readme * chore:creates git ignore and adds all target files into it --------- Co-authored-by: Daniel Boyce <[email protected]>
1 parent 1fd5dc4 commit 36e3638

File tree

14 files changed

+295
-0
lines changed

14 files changed

+295
-0
lines changed

lesson_04/danielboyce/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
target/

lesson_04/danielboyce/README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
## Java Implementation
2+
```
3+
public class IsPrime {
4+
public static void main(String[] args) {
5+
System.out.println("Hello world!");
6+
}
7+
8+
public static boolean isPrime(int n) {
9+
if (n <= 1) {
10+
return false;
11+
}
12+
for (int i = 2; i <= Math.sqrt(n); i++) {
13+
if (n % i == 0) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
}
20+
```
21+
22+
## python implementation
23+
```
24+
import unittest
25+
def is_prime(n):
26+
"""Check if a number is prime."""
27+
if n <= 1:
28+
return False
29+
for i in range(2, int(n**0.5) + 1):
30+
if n % i == 0:
31+
return False
32+
return True
33+
34+
```
35+
36+
## Unit testing
37+
38+
### Pyhton
39+
```
40+
class TestIsPrime(unittest.TestCase):
41+
def test_is_prime(self):
42+
self.assertFalse(is_prime(12))
43+
44+
def test_is_prime_negative(self):
45+
self.assertFalse(is_prime(-5))
46+
def test_is_prime_zero(self):
47+
self.assertFalse(is_prime(0))
48+
def test_is_prime_two(self):
49+
self.assertTrue(is_prime(2))
50+
51+
52+
if __name__ == '__main__':
53+
unittest.main()
54+
```
55+
56+
### Java
57+
```
58+
59+
import static org.junit.jupiter.api.Assertions.assertFalse;
60+
import static org.junit.jupiter.api.Assertions.assertTrue;
61+
import org.junit.jupiter.api.Test;
62+
63+
import com.primenumbers.IsPrime;
64+
65+
public class IsPrimeTest {
66+
67+
68+
@Test
69+
public void testIsPrime() {
70+
assertTrue(IsPrime.isPrime(7));
71+
}
72+
73+
74+
@Test
75+
public void testIsNotPrime() {
76+
assertFalse(IsPrime.isPrime(4));
77+
}
78+
79+
80+
@Test
81+
public void testIsPrimeEdgeCases() {
82+
assertTrue(IsPrime.isPrime(2));
83+
assertTrue(IsPrime.isPrime(1));
84+
assertFalse(IsPrime.isPrime(0));
85+
}
86+
87+
@Test
88+
public void testNegativeNumbers() {
89+
assertFalse(IsPrime.isPrime(-5));
90+
assertFalse(IsPrime.isPrime(-1));
91+
}
92+
93+
94+
}
95+
96+
```
97+
98+
## Explanation
99+
The Java implementation uses a function called `IsPrime` that takes in a single int parameter `n` and then uses a for loop to iterate from every number from 2 to the square root of n then checks any numbers are divisible by n if the number is then it returns false and if it finished the loop then we return true. Using the square root of n as end condition for the loop allows us to avoid looking at every number from 1-n
100+
101+
The python uses a function called `is_prime` with the same logic as the java implementation
102+
103+
### Differences
104+
105+
1. **Syntax**:
106+
- In Java, methods are defined inside classes using access modifiers like `public static`, and the `main` method is the program’s entry point. In Python, functions are defined with the `def` keyword and don’t need to be wrapped in a class unless desired.
107+
- Java uses curly braces `{}` for code blocks, while Python uses indentation.
108+
109+
2. **Type System**:
110+
- Java is statically typed, so the parameter `n` in `isPrime` must be declared as `int`. Python is dynamically typed, inferring `n`’s type at runtime without explicit type declarations.
111+
112+
3. **Testing Frameworks**:
113+
- The Java implementation uses **JUnit 5** with `@Test` annotations and assertion methods like `assertTrue()` and `assertFalse()` from `org.junit.jupiter.api.Assertions`. Python uses the built-in `unittest` framework, where tests are methods in a class extending `unittest.TestCase` and use `self.assertTrue()`/`self.assertFalse()`.
114+
- In Java, test discovery is handled by the build/test runner (e.g., Maven, Gradle), while in Python, `unittest.main()` is explicitly called to run tests.
115+
116+
4. **Math Operations**:
117+
- Java uses `Math.sqrt(n)` for the square root in its loop condition, while Python uses `n**0.5` (exponentiation) for the same result.
118+
119+
5. **Boolean Literals**:
120+
- Java uses lowercase `true` and `false`, while Python uses capitalized `True` and `False`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.primenumbers</groupId>
8+
<artifactId>lesson04</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.junit.jupiter</groupId>
19+
<artifactId>junit-jupiter</artifactId>
20+
<version>5.11.0</version> <!-- Use the latest stable version -->
21+
<scope>test</scope>
22+
</dependency>
23+
</dependencies>
24+
25+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.primenumbers;
2+
3+
public class IsPrime {
4+
public static void main(String[] args) {
5+
System.out.println("Hello world!");
6+
}
7+
8+
public static boolean isPrime(int n) {
9+
if (n <= 1) {
10+
return false;
11+
}
12+
for (int i = 2; i <= Math.sqrt(n); i++) {
13+
if (n % i == 0) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
import static org.junit.jupiter.api.Assertions.assertFalse;
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
import org.junit.jupiter.api.Test;
5+
6+
import com.primenumbers.IsPrime;
7+
8+
public class IsPrimeTest {
9+
10+
11+
@Test
12+
public void testIsPrime() {
13+
assertTrue(IsPrime.isPrime(7));
14+
}
15+
16+
17+
@Test
18+
public void testIsNotPrime() {
19+
assertFalse(IsPrime.isPrime(4));
20+
}
21+
22+
23+
@Test
24+
public void testIsPrimeEdgeCases() {
25+
assertTrue(IsPrime.isPrime(2));
26+
assertTrue(IsPrime.isPrime(1));
27+
assertFalse(IsPrime.isPrime(0));
28+
}
29+
30+
@Test
31+
public void testNegativeNumbers() {
32+
assertFalse(IsPrime.isPrime(-5));
33+
assertFalse(IsPrime.isPrime(-1));
34+
}
35+
36+
37+
}
Binary file not shown.

lesson_04/danielboyce/java_is_prime/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/workspaces/code-society-25-2/lesson_04/danielboyce/java_is_prime/src/main/java/com/primenumbers/IsPrime.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
IsPrimeTest.class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/workspaces/code-society-25-2/lesson_04/danielboyce/java_is_prime/src/test/java/IsPrimeTest.java

0 commit comments

Comments
 (0)