Skip to content

Commit 40e6e29

Browse files
authored
feat: adds Darius' code samples (#161)
Co-authored-by: DavisD. <[email protected]>
1 parent 0e5b3d4 commit 40e6e29

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

lesson_04/dariusd/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Prime Time
2+
3+
## Composite # Check Python
4+
### Requirements
5+
- Python 3.x
6+
7+
```python
8+
# Function to check if a number is composite
9+
def is_composite(number):
10+
if number < 2:
11+
return f"{number} is neither prime nor composite."
12+
13+
# Check for factors from 2 to sqrt(number)
14+
for i in range(2, int(number**0.5) + 1):
15+
if number % i == 0:
16+
return f"{number} is a composite number."
17+
18+
return f"{number} is a prime number."
19+
```
20+
21+
## Prime # Check Bash
22+
23+
```bash
24+
# Function to check if a number is prime
25+
is_prime() {
26+
local num=$1
27+
if ((num <= 1)); then
28+
echo "$num is not prime."
29+
return 1
30+
fi
31+
# Function to check factors from 2 to square root(number)
32+
for ((i = 2; i <= num / 2; i++)); do
33+
if ((num % i == 0)); then
34+
echo "$num is not prime."
35+
return 1
36+
fi
37+
done
38+
echo "$num is prime."
39+
}
40+
```
41+
42+
## Explanation
43+
44+
The Python Script isn't your conventional prime number detector. Instead I took the approach of using a composite number detector that uses the function `is_composite` to show weather the `number` you input is composite. If inputted number is less than 2 then `it's neither prime nor composite`. If previous rule is false(greater than 2) then we run factors of 2 to the square root to determine `results`. Any factor larger than the square root would already have a corresponding smaller factor so will know if our `number is composite` or not.
45+
46+
Next, the Bash Script is a classic prime numbers check that uses the function `is_prime` to... you've guessed it check if users number is a prime. Said function will first check if the inputted `number` is greater than or equal to 1; If said previous rule was true then the we stop. If false, then it will run sequence to determine if said number is has factors from 2 to square root of number. When done if 2 factors are found it `isn't a prime number` and `is a prime number` if 2 aren't found. System will `echo`(notify user) of final verdict.
47+
48+
## Differences
49+
50+
51+
1. **Syntax**:
52+
- In Python, we use `def` as our keyword and `int` as a way to describe the inputted number.
53+
- In Bash we use `local` as our keyword and `num`/`$num` as a way to describe inputted number from users.
54+
55+
2. **Function Calls**:
56+
- `Print` is used to call functions and output information for Python while `Echo` is used for Bash, in which both give info to the the user.
57+
58+
3. **Composite vs Prime**:
59+
- The Python script is used to determine weather the inputted number `is a Composite number`; In doing so we also get the reciprocal on if it's a prime number as well.
60+
- The Bash Script is the opposite in which where looking for if the inputted number `is prime` and in doing so we get the reciprocal if it isn't.

0 commit comments

Comments
 (0)