Skip to content

Commit 60f893f

Browse files
feat: adds Xavier's writeup for Prime number algo in python and java (#133)
* feat: Created writeup python/java * update: updated writeup * hotfix: misread problem. fixed using prime number algo
1 parent 70dccf2 commit 60f893f

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

lesson_04/xaviercruz/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## Java Implementation
2+
3+
```java
4+
5+
public class Main{
6+
public static void main(String args[]){
7+
8+
System.out.println(isPrime(3)); // Output: true
9+
System.out.println(isPrime(4)); // Output: false;
10+
}
11+
12+
13+
public static boolean isPrime(int n){
14+
if (n <= 1) {
15+
return false;
16+
}
17+
for (int i = 2; i < Math.sqrt(n); i++) {
18+
if (n % i == 0) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
}
25+
26+
27+
```
28+
29+
## Python Implementation
30+
31+
```python
32+
33+
def is_prime(n: int) -> bool:
34+
if n <= 1:
35+
return False
36+
for i in range(2, int(math.sqrt(n)) + 1):
37+
if n % i == 0:
38+
return False
39+
return True
40+
41+
print(is_prime(3)) # Output: true
42+
print(is_prime(4)) # Output: false
43+
44+
```
45+
46+
## Explanation
47+
48+
### Core Differences
49+
50+
**Syntax**:
51+
52+
- Java: Java requires explicit class definitions, and all code must reside within a class. The main method is the entry point of the application. Java uses semicolons to terminate statements and curly braces to define blocks of code.
53+
54+
- Python: Python has a more concise and readable syntax, using indentation to define blocks of code without the need for curly braces or semicolons. There is no need for an explicit main method; you can simply write functions at the top level.
55+
56+
**Type Declaration**:
57+
58+
- Java: It is a statically typed language, meaning variable types must be declared explicitly (e.g., `int n`). This can help catch type-related errors at compile time.
59+
60+
- Python: Python is dynamically typed, allowing variables to be assigned without explicit type declarations. Type hints can be used (as seen in `n: int`), but they are not enforced at runtime.
61+
62+
**Libraries and Imports**:
63+
64+
- Java: You need to import specific libraries (e.g., `Math` for square root calculations) explicitly.
65+
66+
- Python: The math library needs to be imported, as shown in the Python implementation. However, the import syntax is simple (`import math`), and Python's built-in functions often eliminate the need for additional libraries.
67+
68+
**Boolean Representation**:
69+
70+
- Java: Uses `true` and `false` (with a lowercase 'T' and 'F') for boolean values.
71+
72+
- Python: Uses `True` and `False` (with a capital 'T' and 'F') but it’s more flexible with types and doesn’t require strict boolean checks.
73+
74+
### Core Similarities
75+
76+
**Logic of Prime Detection**:
77+
78+
- Both implementations use a similar logic to determine if a number is prime:
79+
- A number less than or equal to 1 is not prime.
80+
- The algorithm checks for divisibility from 2 up to the square root of the number.
81+
- If any divisor is found, the number is not prime; otherwise, it is prime.
82+
83+
**Control Structures**:
84+
85+
- Both languages employ similar control structures such as if statements and for loops. The iteration over possible divisors is done using a loop, and the logic to return a boolean value is consistent.
86+
87+
**Functionality**:
88+
89+
- Both implementations return a boolean value indicating whether the input number is prime.
90+
91+
## Conclusion
92+
Both Java and Python successfully implement a prime number detection algorithm with logical similarities and notable differences in syntax, type handling, and structure. The choice between the two languages often comes down to the specific use case, existing codebase, or personal preference, as each offers unique advantages. Java's robustness and performance are great for large-scale applications, while Python's simplicity and readability are ideal for rapid development and scripting.

0 commit comments

Comments
 (0)