Skip to content

Commit 98b227d

Browse files
committed
Merge branch 'lesson_07-homework' of https://github.com/jxwatson251/code-differently-25-q1 into lesson_07-homework
2 parents 76dd422 + 0e5b3d4 commit 98b227d

File tree

23 files changed

+1179
-0
lines changed

23 files changed

+1179
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## Swift implementation
2+
3+
```Swift
4+
func isPrime(_ num: Int) -> Bool {
5+
if num < 2 { return false }
6+
if num == 2 { return true }
7+
if num % 2 == 0 { return false } // Eliminate even numbers
8+
9+
for i in 3...Int(Double(num).squareRoot()) where i % 2 != 0 {
10+
if num % i == 0 {
11+
return false
12+
}
13+
}
14+
return true
15+
}
16+
```
17+
18+
## C++ implementation
19+
20+
```C++
21+
#include <iostream>
22+
#include <cmath>
23+
24+
bool isPrime(int num) {
25+
if (num < 2) return false;
26+
if (num == 2) return true;
27+
if (num % 2 == 0) return false; // Eliminate even numbers
28+
29+
for (int i = 3; i <= sqrt(num); i += 2) { // Check only odd numbers
30+
if (num % i == 0) {
31+
return false;
32+
}
33+
}
34+
return true;
35+
}
36+
```
37+
38+
## Explanation
39+
Some of the observations I've made are that both code implementations had an `isPrime` function.
40+
41+
For both, if the number is less than 2 it returns false (`if num < 2 { return false }`). If the number is even to 2 it returns true (`if num == 2 { return true }`). If the number is divided by 2 and equals 0 it returns false ( `if num % 2 == 0 { return false }`).
42+
43+
I noticed both languages have `bool` in code but in different spaces.
44+
45+
Swift and C++ have the same loop variable (i)
46+
47+
Both codes include If (`num % i == 0`). Taking the number divided by `i = 0`.
48+
49+
### Differences
50+
51+
In C++ the code starts off with `#include <iostream> #include <cmath>`.
52+
53+
Before the function `isPrime` Swift starts with `func` but C++ starts with `bool`.
54+
55+
Even though both codes have square root it is formatted differently (`.squareRoot()` and `sqrt()`) in the code.
56+
57+
Swift has `Double(num)` in the code where C++ does not include this.
58+
59+
In Swift the code does not declare the `i` but C++ declares the `i`.
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+

lesson_04/JBey/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Python
2+
3+
```python
4+
def primeNumber(num):
5+
if num <= 1:
6+
return False
7+
8+
for i in range(2, int(num**0.5) + 1):
9+
if num % i == 0:
10+
return False
11+
12+
return True
13+
14+
print(is_prime(11))
15+
print(is_prime(4))
16+
print(is_prime(1))
17+
print(is_prime(13))
18+
```
19+
20+
## JavaScript
21+
22+
```javascript
23+
function primeNumber(num) {
24+
if (num <= 1) {
25+
return false;
26+
}
27+
28+
for (let i = 2; i <= Math.sqrt(num); i++) {
29+
if (num % i === 0) {
30+
return false;
31+
}
32+
}
33+
34+
return true;
35+
}
36+
37+
console.log(isPrime(11));
38+
console.log(isPrime(4));
39+
console.log(isPrime(1));
40+
console.log(isPrime(13));
41+
```
42+
43+
## Explanation
44+
45+
Both languages use a function that is called called "PrimeNumber" with an argument called "num" that a number in the code compares itself in order to see if it is an even number or not. They both use a .log system where you put in your arguments (or numbers) in order to see if they're a prime number or not.
46+
47+
JavaScript uses console.log() in order to output files to the console, while python uses print() instead of console.log(). JavaScript uses the word "function" to create a function, while python uses the word "def" to create a function. Python create another function called "range" when creating a for loop, whereas JavaScript does not create another function.

lesson_04/JasonWatson/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## C
2+
3+
```C
4+
#include <stdio.h>
5+
#include <stdbool.h>
6+
7+
bool isPrime(int n) {
8+
if (n <= 1) return false;
9+
for (int i = 2; i * i <= n; i++) {
10+
if (n % i == 0) return false;
11+
}
12+
return true;
13+
}
14+
15+
int main() {
16+
int number = 29;
17+
if (isPrime(number))
18+
printf("%d is a prime number . \n", number );
19+
else
20+
printf("%d is not a prime number . \n", number );
21+
return 0;
22+
}
23+
```
24+
25+
## C++
26+
27+
```Cpp
28+
#include <iostream>
29+
#include <cmath>
30+
31+
bool isPrime(int n) {
32+
if (n <= 1) return false;
33+
for (int i = 2; i <= sqrt(n); i++) {
34+
if (n % i == 0) return false;
35+
}
36+
return true;
37+
}
38+
39+
int main() {
40+
int number = 29;
41+
std::cout << std::boolalpha <<
42+
isPrime(number) << std::end1;
43+
return 0;
44+
}
45+
```
46+
47+
## Explanation
48+
This C program checks if a given nember is a prime number.It uses a function isPrime() to then determine whether a number is a prime number and the gives the output.
49+
50+
This C++ program checks when a given number is a prime number by using the simple function isPrime() and the gives an out. It does so by using the <iostream> which is a library for input/output and the <cmath> for calculation.
51+
52+
53+
### DIFFERENCES
54+
55+
1. **Syntax**:
56+
- In C it uses a standard input-output library which gives us the ability to use functions such as print() to give output.
57+
- In C++ it uses a standard input-output stream library which makes us be able to use std::cout and std::endl to display the output.
58+
59+
2. **How they carry out calculation**:
60+
- C uses the bool data type to which provides true and false values for logical operation whilst the C++ use a math library to do mathematical functions such as sqrt() that calculkates square root numbers.
61+
62+
63+
64+
### SIMILARITIES
65+
66+
1. **Codes**:
67+
- Both C and C++ use the function if (n <= 1) return false to determine if the number is a prime number.
68+
- They both have the ability to do loop for Checking Divisibility.
69+
- the function return true; is use by both programs to return true if No Divisors found.
70+

lesson_04/ananatawamcintyre/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
# Python Implementation
3+
4+
---
5+
6+
```python
7+
def is_prime(n):
8+
"""Check if a number is prime."""
9+
if n < 2:
10+
return False
11+
for i in range(2, int(n ** 0.5) + 1):
12+
if n % i == 0:
13+
return False
14+
return True
15+
16+
# Example usage
17+
if is_prime(num):
18+
print(f"{3} is a prime number.")
19+
else:
20+
print(f"{4} is not a prime number.")
21+
22+
```
23+
24+
# JavaScript Implementation
25+
26+
---
27+
28+
```JavaScript
29+
30+
function isPrime(n) {
31+
if (n < 2) return false;
32+
for (let i = 2; i <= Math.sqrt(n); i++) {
33+
if (n % i === 0) return false;
34+
}
35+
return true;
36+
}
37+
38+
// Example usage
39+
if (isPrime(num)) {
40+
console.log(`${3} is a prime number.`);
41+
} else {
42+
console.log(`${4} is not a prime number.`);
43+
}
44+
45+
46+
```
47+
48+
# Explanation
49+
50+
---
51+
52+
As seen above, I have created a code that helps the user determine whether a number is a prime number or not. I utilized both Python and Javascript to do so and this was my first time writing out my own code with those languages. The sample numbers **(3)** and **(4)** were used to show the reader, **you**, what an example of a prime number looks like. **Is_prime** stands for the command that determines whether a number **(num or n)** is prime or not.
53+
54+
## Similarities
55+
56+
- Both implementations display the number **(3)** as the prime number and **(4)** as the non-prime number.
57+
- Both use **"if"** and **"is_prime"** to point out if a number is prime or not.
58+
- Both implementations return **"true"** or **"false"** depending on whether the number is a prime number or not.
59+
60+
## Differences
61+
62+
- The codeword used to determine the answer for the Java Code implementation is **console.log**.
63+
- The codeword used to determine the answer for the Python implementation is **print**.
64+
- Python uses **def** before is_prime and JavaScript uses **function** before is_prime.
65+
66+
---
67+
68+
### Bonus message from the creator:
69+
Hello World! Again, this was my first time creating my own code. I had lots of fun working in JavaScript and Python, but would love to hear some good feedback or even complaints that you, the reader, may have. I spent a good **2 hours** trying to figure out how to get both Python and Javascript to work in a Markdown file. If any part of my code was confusing or uneasy to read, let a sista know! Happy coding! - A'nanatawa
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Java implementation
2+
3+
```java
4+
public class PrimeCheck {
5+
public static boolean isPrime(int num) {
6+
if (num <= 1) return false;
7+
for (int i = 2; i<= Math.sqrt(num); i++) {
8+
if (num % i == 0) return false;
9+
}
10+
return true;
11+
}
12+
# Example usage:
13+
public static void main(String[] args) {
14+
int num = 29;
15+
System.out.println(isPrime(num)); // Output: true
16+
System.out.println(isPrime(4)); // Output: false
17+
}
18+
}
19+
```
20+
21+
## C++ implementation
22+
23+
24+
```C++
25+
26+
#include<iostream>
27+
#include <cmath>
28+
using namespace std;
29+
30+
bool isPrime(int num) {
31+
if (num <= 1) return false;
32+
for (int i = 2; i <= sqrt(num); i++) {
33+
if (num % i == 0) return false;
34+
}
35+
return true;
36+
}
37+
int main() {
38+
int num = 29;
39+
# Example usage:
40+
std::cout << isPrime(num) << std::endl;
41+
std::cout << isPrime(4) << std::endl;
42+
return 0;
43+
}
44+
45+
```
46+
47+
## Explanation
48+
The Java implementation uses a function named `isPrime`. Similiar to C++, the formula checks to see if a `num` varaible is less than or equal to 1, if so it will render a `false` result. If you have no variable that divides into your number (same logic as C++), it will render a `true` result.
49+
50+
The C++ implementation uses a function named `isPrime` that could give a `False` result if the `num` variable is less than or equal to 1 or if it divides equally or exactly (i.e., num % i == 0). If you have no variable that divides into your number it will render a `true` result.
51+
52+
53+
54+
55+
### Differences
56+
57+
In Java the text `public` means the method will be accessible and `PrimeCheck` is a method to check for prime or non prime numbers. `Boolean` in Java, is the code is an indicator method that will give an true or false value.
58+
59+
C++ includes "<iostream>" is an input-output stream that allows the function `std:: cout and cin` to be used for input-output operations. The `#include<cmath>` provides C++ math library that will include functions like square root; `std` is a prefix function that gives access to the C++ library. The `std` is a standard point for either input or output.
60+

0 commit comments

Comments
 (0)