Skip to content

Commit 0e5b3d4

Browse files
feat: adds Karens updated and finalized lesson_04 (#273)
1 parent ca7e75c commit 0e5b3d4

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

lesson_04/karenalabi/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
Lesson 04
2+
3+
## Python Implementation
4+
5+
```python
6+
# Defines a function named (primenumcheck) that calls a parameter (numbers)
7+
def primenumcheck (number):
8+
# Conditional statements that determine if the number being returned is prime
9+
if number <= 1:
10+
return False
11+
if number == 2:
12+
return True
13+
if number % 2 == 0:
14+
return False
15+
# For loop to check if numbers greater than 2 are prime
16+
for i in range(3, int(number ** 0.5) + 1, 2):
17+
if number % i == 0:
18+
return False
19+
return True
20+
```
21+
22+
## Java Implementation
23+
24+
```java
25+
// Declaring the class in program (PrimeNumberChecker)
26+
public class PrimeNumberChecker
27+
// boolean method to check if integer is Prime
28+
// access modifier to start the program
29+
public static boolean checkIfPrime (int number) {
30+
// Conditional statements to determine if number is prime
31+
if (number <= 1) {
32+
return false;
33+
}
34+
if (number == 2) {
35+
return true;
36+
}
37+
if (number % 2 == 0) {
38+
return false;
39+
}
40+
// For loop to check if mumbers greater than 2 are prime
41+
for (int i = 3; i <= Math.sqrt(number); i += 2) {
42+
if (number % i == 0) {
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
```
49+
50+
51+
## Explanation
52+
The Python implementation defines a function named with the `def` keyword. The function is called `primenumcheck` and it calls a parameter `number` and determines whether it is prime. The python implementation then uses condiitonal if - statements to return false if the number is less than or equal to 1, or if the number is divisible by 2 becuase prime numbers can only be divisible by themselves and are greater than 1. The function will return true if the number returned is equal to 2. The python implementation also uses a `for` loop to iterate through the odd numbers up to the squareoot of that given number. If any of these numbers evenly divides `number`, it is not prime, and will return false. The loop increments by 2 (`i += 2`) to rule out even numbers.
53+
54+
The Java implementation follows a similar logic to the python implementation. It defines a public class named `PrimeNumberChecker`. Within this class, a `boolean` method called `checkIfPrime` is implemented to determine if an integer is prime or not. The method takes an `int` (integer) parameter called `number` and uses conditional statements (if-statements) to check if the input number is prime. Similar to the python inplementation, the java implementation also used a for-loop to iterate through numbers not evenly divisible by the given number.
55+
56+
### Differences
57+
58+
1. **Syntax**:
59+
- In Python, the function is defined using the `def` keyword. The fucntion can be defined by directly calling the name `primenumcheck`. Whereas, in Java, the proper term is a method and methods are defined within a class using a return type `boolean`, in this case. The Java implementation will return true or fase based on the int parameter called `number`.
60+
61+
- Python uses indentation to define the beginning and end of that portion of code, while Java uses curly braces `{}`.
62+
63+
- Python capitalizes its boolean values. It uses `True` and `False`. Java does not capitalize the boolean values.
64+
65+
- Python uses hashes to comment out the code while java uses back slashes
66+
67+
2. **Type System**:
68+
- Java requires the type of method (`boolean` or `int`) to be declared within an access modifier: `public static boolean checkIfPrime` , whereas the Python function can be called by its name: `def primenumcheck`
69+
70+
- Java uses `boolean` to return `checkIfPrime` method, whereas the Python implementation returns True or False based on the if-statement conditions
71+
3. **Function Calls**:
72+
- the python implementation uses a range within the for loop
73+
74+
- the java implementation uses the math sqroot
75+

0 commit comments

Comments
 (0)