Skip to content

Commit c1a21d9

Browse files
authored
Merge branch 'code-differently:main' into Shawn-lesson07
2 parents 92e7a05 + d12e7a3 commit c1a21d9

File tree

34 files changed

+6938
-14
lines changed

34 files changed

+6938
-14
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Check Lesson 05 Quiz Pull Request
2+
3+
on:
4+
pull_request:
5+
branches: [ "main" ]
6+
paths:
7+
- "lesson_05/quiz/**"
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Use Node.js
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: '20.x'
23+
24+
- name: Build Shared Lib with Node.js
25+
working-directory: ./lib/typescript/codedifferently-instructional
26+
run: npm ci
27+
28+
- name: Build Lesson 06 with Node.js
29+
working-directory: ./lesson_05/quiz
30+
run: |
31+
npm ci
32+
npm run check

README.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

lesson_04/chelseaogbonnia/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Prime Number Finder (Python and JavaScript) #
2+
3+
This project demonstrates how to find prime numbers between 1 and 100 using two programming languages: Python and JavaScript. Each function uses a for loop to check whether a number is prime, and both include inline comments to explain the code step by step. Additionally, there are detailed explanations after each function.
4+
5+
## Prime Number Finder in Python ##
6+
```
7+
# Function to find prime numbers between 1 and 100 in Python
8+
def find_prime_num_py():
9+
10+
# Loop through numbers from 2 to 100
11+
for num in range(2, 101):
12+
is_prime = True # Assume the number is prime
13+
14+
# Check if num is divisible by any number between 2 and its square root
15+
for i in range(2, int(num**0.5) + 1):
16+
if num % i == 0:
17+
is_prime = False # If divisible, not a prime number
18+
break # Exit the loop early
19+
20+
# If is_prime is still true, num is a prime number
21+
if is_prime:
22+
print(num) # Output the prime number
23+
24+
# Call the Python function
25+
find_prime_num_py()
26+
```
27+
### Python Explanation: ###
28+
The Python function, find_prime_num_py, uses a for loop to iterate through the numbers between 2 and 100. The is_prime variable is set to True for each number. Inside the inner loop, we check if the number is divisible by any integer between 2 and the square root of the number. If the number is divisible, it is marked as not prime, and we break out of the inner loop to stop further unnecessary checks. The square root limit ensures better performance. When the number is confirmed to be prime, it is printed to the console.
29+
30+
31+
## Prime Number Finder in JavaScript ##
32+
```
33+
// Function to find prime numbers between 1 and 100 in JavaScript
34+
function findPrimeNumberJS() {
35+
36+
// Loop through numbers from 2 to 100
37+
for (let num = 2; num <= 100; num++) {
38+
let isPrime = true; // Assume the number is prime
39+
40+
// Check if num is divisible by any number between 2 and its square root
41+
for (let i = 2; i <= Math.sqrt(num); i++) {
42+
if (num % i === 0) {
43+
isPrime = false; // If divisible, not a prime number
44+
break; // Exit the loop early
45+
}
46+
}
47+
48+
// If isPrime is still true, num is a prime number
49+
if (isPrime) {
50+
console.log(num); // Output the prime number
51+
}
52+
}
53+
}
54+
55+
// Call the JavaScript function
56+
findPrimeNumberJS();
57+
```
58+
59+
### JavaScript Explanation: ###
60+
In this JavaScript function, findPrimeNumberJS, we use a for loop to iterate over the numbers from 2 to 100. The isPrime variable initially assumes that each number is prime. For each number num, we check if it is divisible by any number from 2 to the square root of num. If the number is divisible, it is marked as not prime by setting isPrime to false, and we break out of the inner loop to save time. If the number remains prime after the check, it is printed to the console. The square root check ensures we minimize the number of iterations for efficiency.
61+
62+
## Similarities and Differences Between Python and JavaScript Prime Number Functions ##
63+
64+
### Similarities
65+
66+
* Logic (Using Square Roots to Check Primes): Both functions in Python and JavaScript use the same fundamental logic to determine if a number is prime. By checking divisors only up to the square root of the number, we can efficiently reduce the number of checks. This optimization works in both languages because if a number has a divisor greater than its square root, it must also have a corresponding smaller divisor, which would have been checked already.
67+
68+
* Looping Structure: Both functions use a for loop to iterate over potential divisors, starting from 2. While the exact syntax differs, the purpose is the same—to test whether the number is divisible by any number within this range. If no divisor is found, the number is prime.
69+
70+
71+
### Differences:
72+
73+
* Syntax: Python uses range() to generate numbers in a loop, while JavaScript directly declares the loop variable and sets its conditions. Python also uses indentation to define code blocks, while JavaScript uses curly braces {}.
74+
75+
* Case Sensitivity: Python commonly uses snake_case for function and variable names (e.g., is_prime), while JavaScript typically uses camelCase (e.g., isPrime).
76+
77+
* Variable Declarations: In Python, variables are declared by simply assigning a value to them (e.g., num = 5). In JavaScript, variables must be explicitly declared using let, const, or var (e.g., let num = 5).

lesson_04/dasiaenglish/lesson_04.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
```Javascript
2+
#Javascript
3+
4+
function findPrimes(numberToCheck) { // A machine that helps find prime numbers
5+
6+
if (numberToCheck <=1){
7+
return `${numberToCheck} is not a prime number.`; //any number that is less or equal to 1 it is NOT a prime number
8+
}
9+
let isPrime = true; //I am start with assuming the number is prime
10+
11+
12+
for (let factor = 2; factor <= Math.floor(numberToCheck / 2); factor++) { //this is another loop but it checks to see if the number is divisible by other numbers.
13+
if (numberToCheck % factor === 0) { // this is checking to see if the number can divide evenly and if so then it is not a prime number
14+
isPrime = false; // states that the number is not prime if it comes out as 0 (should have a remainder)
15+
break; //states that it can STOP the loop once it finds a 0, no need to keep going through
16+
} //this is closing the loop
17+
}
18+
19+
if (isPrime) { //if said number is still true that means that we did not find any number that is divided evenly so it is prime
20+
return `${numberToCheck} is a prime number.`; //if the numbe is prime it will say^^
21+
} else{
22+
return `${numberToCheck} is not a prime number.`; // if it is NOT prime it will say so
23+
}
24+
} //closing the loop of if it is a prime number or not
25+
26+
const input = process.argv[2]; // telling the computer to save the 3rd thing you type
27+
28+
let number = parseInt(input); // if it types a word the computer does the math and makes it a number
29+
30+
if (isNaN(number)) { // make sure you type a number
31+
console.log("Please enter a valid number."); // letting the user know you have to type a number
32+
} else {
33+
console.log(findPrimes(number)); //now the computer can check if it is prime or not
34+
}
35+
36+
// credit from Coding with John youtube video https://www.youtube.com/watch?v=I9-3drnfyp0 and Chatgpt for a explanation of things I still might have been confused about
37+
```
38+
39+
```python
40+
# Python
41+
# this is a function that will help us find all the prime numbers
42+
def find_primes(number_to_check):
43+
if number_to_check <= 1: # this is an empty list for now until we run the test for all the prime numbers we find
44+
return f"{number_to_check} is not a prime number."
45+
46+
is_prime = True # I am saying that it is a prime until I find out it is not
47+
48+
# checks to see if the number can be divided by a smaller number evenly
49+
for factor in range(2, number_to_check // 2 + 1):
50+
if number_to_check % factor == 0: # trying to see if there is a remainder after the divison and if it is equal to zero
51+
is_prime = False # if it is equal to zero it is flase meaning it is not prime
52+
break # again it means STOP
53+
if is_prime: # after checking all
54+
return f"{number_to_check} is a prime number."
55+
else:
56+
return f"{number_to_check} is not a prime number."
57+
58+
number = int(input("Enter a number to check to see if its prime: "))
59+
print(find_primes(number))
60+
61+
```
62+
63+
## Explanation
64+
My first thought was to use Javascript and html because those were the 2 languages that I was familiar with. I did some research and quickly came to the realization that html would not be the most effective. That's when I found out that I should use Python and Javascript.
65+
Python is known for how easy it is to read and how simple it is. But is super space indentation sensitive. Whereas Javascript is a little more complex because it uses different characters, which makes it a little harder to understand.
66+
67+
Similarities: Both Javascript and Python are finding prime numbers between 2 and 100 even though in pyton it says 101. That is becuase we did not plug in 101 we stopped right before it.
68+
69+
Diffreneces: A diffrence that Javascript uses let for declaring variables while python uses simplier words becuase you do not need a keyword
70+

lesson_04/dennislipscomb/README04.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## Javascript
2+
```Javascript
3+
4+
function isPrime (num) {
5+
/* first condtion is checking if 7 is less than or equal to 1. */
6+
if (num <= 1) return false;
7+
/*checking if the number has a remainder or 0 if divided 2 */
8+
for (let i = 2; i < num; i++) {
9+
if (num% i === 0)
10+
return false;
11+
}
12+
return true;}
13+
14+
15+
console.log(isPrime (7))
16+
```
17+
18+
## Java
19+
```Java
20+
21+
public class PrimeChecker {
22+
23+
public static boolean isPrime(int num) {
24+
// First condition: check if num is less than or equal to 1
25+
if (num <= 1) return false;
26+
27+
// Check for factors from 2 to num - 1
28+
for (int i = 2; i < num; i++) {
29+
if (num % i == 0) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+
36+
public static void main(String[] args) {
37+
System.out.println(isPrime(11)); // Test the function
38+
}
39+
}
40+
```
41+
42+
## Explination
43+
44+
In javascript, the function "isPrime" is checking if the number is less than or equal to 1. if so the fuction will fail. If it passes it will run the next fuction to see if the input has a remainder of 0 if divided by 2.
45+
46+
In Java, the function "isPrime(int num)" is checking if the number is less than or equal to 1. if the number is indeed greater than or less than it will return false. if true the function will run through a for loop to determine if divided by 2 will the remainder be 0.
47+
48+
49+
### Differences
50+
51+
Java and Javascript have numerous similarites but arent related. Java runs a more strict ruleset with certain elements such capital vs lower case letters where as javascript is more lenient in its functionality. In javascript the print out to test the fuction is console.log where as in Java the print out is system.out.println
52+
53+
54+
#### Citing
55+
56+
I got my Javascript code from a youtube video https://www.youtube.com/watch?v=ZdoiS_qUOSE I took that code and pasted it into https://playcode.io/ to test if the code worked. I had to make a few changes through trial and error. Once i got the correct code, i asked chatGPT to convert the javascript into java.
57+

lesson_04/hummadtanweer/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
3+
```Java
4+
static boolean checkPrime(int num) {
5+
boolean prime = false;
6+
if (num == 0 || num == 1) {
7+
prime = true;
8+
}
9+
for (int i = 2; i <= num / 2; i++) {
10+
if (num % i == 0) {
11+
prime = true;
12+
break;
13+
}git
14+
}
15+
return !prime;
16+
}
17+
18+
# Example usage:
19+
print(checkPrime(4)) # Output: false
20+
print(checkPrime(7)) # Output: true
21+
22+
## JavaScript implementation
23+
24+
```javascript
25+
function checkPrime(num) {
26+
let prime = false;
27+
if (num === 0 || num === 1) {
28+
prime = true;
29+
}
30+
for (let i = 2; i <= num / 2; i++) {
31+
if (num % i === 0) {
32+
prime = true;
33+
break;
34+
}
35+
}
36+
return !prime;
37+
}
38+
// Example usage:
39+
console.log(checkPrime(4)); // Output: false
40+
console.log(checkPrime(7)); // Output: true
41+
42+
43+
## Explanation
44+
45+
The Javascript implementation uses a function named `checkPrime` that takes a single argument `number`. It returns `True` if the number is prime, otherwise, it returns `False`.
46+
47+
The Java implementation uses a function named `checkPrime` that also takes a single argument `int number`. It returns `true` if the number is Prime and `false` otherwise.
48+
49+
Java uses `true` and `talse` for boolean values and JavaScript uses `true` and `false`.
50+
51+
### Differences
52+
53+
**Function Calls**:
54+
- The syntax for calling functions and printing to the console/output is slightly different. Java uses `print()`, while JavaScript uses `console.log()`.
55+
56+
**Citation
57+
https://www.programiz.com/java-programming/online-compiler/?ref=8039b165git

lesson_04/pablolimonparedes/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Java Implementation
2+
```Java
3+
public class Main {
4+
public static void main(String[]args){
5+
int num = 19;
6+
boolean isPrime = true;
7+
for (int i = 2; i < num; i++){
8+
if (num % i == 0){
9+
isPrime = false;
10+
break;
11+
}
12+
}
13+
if (isPrime){
14+
System.out.println(num + " is a Prime number.");
15+
} else {
16+
System.out.println(num + " is not a Prime number.");
17+
}
18+
}
19+
}
20+
//Had help from Chat GPT to help me understand how to make one and break it down step by step//
21+
```
22+
## Python Implementation
23+
``` Python
24+
def is_prime(num):
25+
if num <= 1:
26+
return False
27+
28+
for i in range(2, num):
29+
if num % i == 0:
30+
return False
31+
32+
return True
33+
num = 19
34+
if is_prime(num):
35+
print(f"{num} is a Prime number.")
36+
else:
37+
print(f"{num} is Not a Prime number.")
38+
39+
## I Had Chat GPT convert this from the Java equation ##
40+
```
41+
## Explanation
42+
Here i have the equations to find prime numbers in both Java and Python, however it is hard coded which means that if you wanted to figure out that another number was a prime number you would need to change the value in the variable num.
43+
44+
## Differences
45+
- Java uses curly braces with `else`, while python uses a colon with `else` to define the blocks of code.
46+
- Java has to use `public static boolean` to define the method, as in this Python equation does not use any class to define its method since it does not have one in this case, but instead uses a function with the keyword `def` to declare it.
47+
- Java and Python both use a `for` loop however Python uses `range` to create the sequence of numbers from 2 till num and in the Java code `for (int i = 2; i < num; i++)` you have to initialize, then have the condition, and lastly increment.
48+
## Similarities
49+
- Both Codes print out whether or not the number is prime, if you would like to find another number you just need to change the value from the variable num.
50+
- They both use some type of print to output what the solution of your value is to know whether or not your number that is inputted is prime.

0 commit comments

Comments
 (0)