Skip to content

Commit a39c9ab

Browse files
dxsmith244Dsmith07
andauthored
feat: adds David Smith code samples (#139)
* Final Commit * README Updated * Updating Readme to read better --------- Co-authored-by: Dsmith07 <[email protected]>
1 parent 581722d commit a39c9ab

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lesson_04/davidsmith/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Java Method
2+
```java
3+
public static boolean isPrime(int n) {
4+
if (n <= 1) return false;
5+
for (int i = 2; i <= Math.sqrt(n); i++) {
6+
if (n % i == 0) {
7+
return false;
8+
}
9+
}
10+
return true;
11+
}
12+
```
13+
## Python Method
14+
```python
15+
def is_prime(n):
16+
if n <= 1:
17+
return False
18+
for i in range(2, int(n**0.5) + 1):
19+
if n % i == 0:
20+
return False
21+
return True
22+
```
23+
24+
## Understanding The Code
25+
26+
Java and Python are similar in some ways. They have the same function structures. The `isPrime` method is in Java. The `is_prime` function is in Python. Both check if \( n \) is less than or equal to 1. They return false if this is true. Then, they use a for-loop. The for-loop finds factors from 2 to the square root of \( n \). Java needs explicit type declarations. Python does not need explicit type declarations. However, the logical flow is similar. This shows their focus on clarity. It makes both languages friendly for users. It also makes them easy for developers with different skills.
27+
28+
## Differences
29+
30+
Java and Python have big differences in syntax. They also have differences in structure. The `isPrime` method shows this. The `is_prime` function also shows this. Java is statically typed. It needs explicit type declarations like `int n`. Python is dynamically typed. It allows defining variables without a type. Java uses the `Math.sqrt` method. This method is for calculating square roots. Python uses the expression `n**0.5` for this. The for-loop syntax is different too. Java has a traditional for-loop with an initializer. Python uses the `range` function for loops. These differences show the different design philosophies. They also show different approaches to readability for each language.

0 commit comments

Comments
 (0)