Skip to content

Commit 2383acb

Browse files
authored
Merge branch 'code-differently:main' into feature/lesson_06
2 parents e75597b + 8d30a82 commit 2383acb

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

lesson_04/chigazograham/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## Python implementation
2+
3+
```python
4+
def is_prime(num):
5+
# uses the function is_prime and takes the argument num
6+
if num > 1:
7+
# gets rid of numbers 1 and lower as the first prime number is 2
8+
for i in range(2, (num // 2) + 1):
9+
# tells the computer to look at all numbers between 2 and half of the value of num rounded up to the next nearest value using floor division
10+
if(num % i) == 0:
11+
# then divides the num value, using modulo division, by every number in the range of two to +infinity
12+
return False
13+
# if the remainder in equivalent to 0 then the number is not prime and returns False
14+
else:
15+
return True
16+
# if the remaineder is not equivalent to 0 then the terminal will return True
17+
else:
18+
return False
19+
# for all other real numbers that arent prime the terminal will reurn False
20+
21+
print(is_prime(19)) #Output: True
22+
print(is_prime(9)) #Output: False
23+
```
24+
25+
## Ruby implementation
26+
27+
```ruby
28+
def isPrime(n)
29+
#uses the function isPrime and takes the argument n
30+
return false if n <= 1
31+
#the computer will reurn false for all numbers less than or equal to zero
32+
return true if n == 2 || n == 3
33+
#tells the computer to return true if the n is equivalent to 2 or 3
34+
35+
return false if n % 2 == 0 || n % 3 == 0
36+
#returns false if n is divisible by two or 3
37+
38+
i = 5
39+
#defines i starting value as 5
40+
while i * i <= n
41+
#while n is greater than or equal to 25
42+
return false if n % i == 0 || n % (i + 2) == 0
43+
#return false if n is divisible by 5 or 7
44+
i += 6
45+
#adds 6 to the value of i if n is not divisible by 5 or 7 and runs the loop again
46+
end
47+
true
48+
#returns true for all numbers that arent divisible by 2, 3, 5, or 7
49+
end
50+
puts isPrime(17) #Output: true
51+
puts isPrime(24) #Output: false
52+
```
53+
54+
## Explanation
55+
56+
The Python implementation uses a function named `is_prime` that takes a single argument `num`. It returns `False` if the number is 1 or less or divisible by any number from two to half the value of `num` (i.e., when the remainder of the division of the number by 2 is zero), otherwise, it returns `True` and returns any possible other values as false.
57+
58+
The Ruby implementation uses a function named `isPrime` that also takes a single argument `n`. It returns `true` if the number is equivalent to 2 or 3 and if the number is not divisible by 2 or 3 and 5 or 7 if the number is over 25 and `false` if otherwise.
59+
60+
### Differences
61+
62+
1. **Syntax**:
63+
- Python uses `True` and `False` for boolean values, while Ruby uses `true` and `false`.
64+
- The formatting for `if` statements are also different between the two. In Python, `if` statements have the initial statement and then on the next line the command to run if the variable falls under the `if` statement. In Ruby the formatting is completely different, the command comes first and after that the `if` statement comes in on the same line
65+
- In Python, a colon(`:`) is used to close function statements, whereas in Ruby there is nothing closing the function statements.
66+
- Ruby can return true or false without a return statement. In contrast Python requires a return statement or will return with `None`.
67+
- Ruby has to be closed in `end` after loops or an error will appear and the code won't run. Python, on the other hand doesn't need anything and will return with `None` instead of an error if not given further instructions.
68+
- The syntax for calling functions and printing to the console/output is different. Python uses `print()`, while Ruby uses `puts()`.
69+
70+
<!-- Used code from geeksforgeeks.org to assist in creating assignment -->

lesson_04/tommytran/lesson_4.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class lesson_4 {
2+
3+
// Function to check if a number is prime
4+
public static boolean isPrime(int n) {
5+
if (n < 2) {
6+
return false; // Numbers less than 2 are not prime
7+
}
8+
if (n == 2) {
9+
return true; // 2 is the only even prime number
10+
}
11+
if (n % 2 == 0) {
12+
return false; // Numbers divisible by 2 are not prime
13+
}
14+
// Loop that checks every odd number up to the square root of n
15+
for (int i = 3; i <= Math.sqrt(n); i += 2) {
16+
if (n % i == 0) {
17+
return false; // n is divisible by i, so it's not prime
18+
}
19+
}
20+
return true; // n is prime
21+
}
22+
23+
public static void main(String[] args) {
24+
// Declare integers in main method
25+
int a = 1;
26+
int b = 9;
27+
int c = 17;
28+
29+
// Return string of whether or not int is a prime number
30+
System.out.println("Is " + a + " a prime number? " + isPrime(a));
31+
System.out.println("Is " + b + " a prime number? " + isPrime(b));
32+
System.out.println("Is " + c + " a prime number? " + isPrime(c));
33+
}
34+
}
35+
// code sourced from chat-gpt after converting my JS file.

lesson_04/tommytran/lesson_4.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
//declare integers in function: sample numbers
3+
let a = 1;
4+
let b = 9;
5+
let c = 17;
6+
{
7+
function Prime(n) {
8+
if (n < 2) {
9+
return false; // Numbers less than 2 are not prime
10+
}
11+
if (n === 2) {
12+
return true; // 2 is the only even prime number
13+
}
14+
if (n % 2 === 0) {
15+
return false; // numbers divisible by 2 are not prime
16+
}
17+
// loop that checks every odd number up until square root of n
18+
for (let i = 3; i <= Math.sqrt(n);i +=2) {
19+
if (n % i === 0) {
20+
return false; // n is divisible by i, so it's not prime
21+
}
22+
}
23+
return true; // n is prime
24+
}
25+
//return string of whether or not int is a prime number
26+
console.log(`Is ${a} a prime number? ${Prime(a)}`);
27+
console.log(`Is ${b} a prime number? ${Prime(b)}`);
28+
console.log(`Is ${c} a prime number? ${Prime(c)}`);
29+
}

lesson_04/tommytran/readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Explanation
2+
3+
4+
First I declared 3 variables to use to test my function a,b, and c. I then assigned 3 integers to these variables to test this function. I created a function by the name of prime that contained an if else statement and a for loop. I then created several rules for the the if statement. The first rule determined whether the number was less than 2 then it was considered not a prime number. The second rule was that the number 2 would return true as it is the only even prime number. the third rule was that any number that was divided by 2 would not leave a remainder a 0. If none of those conditions were satisfied then a for loop would activate starting from the number 3. The loop would check all numbers starting at number #3 and must be less than or equal to the square root of (n). The loop would then increase integer by 2 to check every odd number between 3 and the square root. If any number that was determined (n) to be divisible by (i) then it was false and not a prime number. If there was no odd divisble numbers then the statement would return true. Afterwards I would test the function by inputting a console.log command in the terminal to print a string that determined whether or not the number in each variable was a prime number or not.
5+
6+
7+
### Similiarities and Differences
8+
9+
10+
1. **Syntax**:
11+
- to print messages in the console in javascript you would do console.log
12+
- to print messages in the console in Java you would use the System.out.println command
13+
- although they are different they ultimately perform the same action and the function's are built in a very similar order/pattern.
14+
15+
2. ** Variables
16+
- In javascript you can declare a variablle using let and achieve a more broader scope of defined objects whereas in Java you have to be more specific with the data type as an int,double,string,etc.
17+
18+
3. ** Testing
19+
- to run the java you would name the file with a .java at the end and type "java [filename.java]" to run the program
20+
- to run the javascrip you would name the file with a .js at the end and type node [filename.js] to run the program

0 commit comments

Comments
 (0)