Skip to content

Commit e75597b

Browse files
authored
Merge branch 'code-differently:main' into feature/lesson_06
2 parents 289f57e + a39c9ab commit e75597b

File tree

5 files changed

+267
-0
lines changed

5 files changed

+267
-0
lines changed

lesson_04/amiyahjones/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Javascript
2+
``` javascript
3+
4+
function isPrime(num) {
5+
if (num < 2) {
6+
return false; // Numbers less than 2 are not prime
7+
}
8+
9+
for (let i = 2; i < num; i++) {
10+
if (num % i === 0) {
11+
return false;
12+
}
13+
}
14+
15+
return true;
16+
}
17+
18+
console.log(isPrime(5)); //true
19+
console.log(isPrime(1)); //false
20+
21+
```
22+
## Ruby
23+
###### Tutorial used : https://www.youtube.com/watch?v=33pLqGvk-PM
24+
``` ruby
25+
def prime?(n)
26+
# '2..n-1' checks all the numbers from 2 up to less than 'n'.
27+
# 'none' -> sees if none of these numbers can divide 'n' evenly.
28+
(2..n-1).none? {|divisor| n % divisor == 0}
29+
# If none of these numbers divide 'n' without a remainder,
30+
# then 'n' is a prime number. Otherwise, it's not.
31+
end
32+
33+
p prime? 5 # prints 'true'
34+
p prime? 6 # prints 'false'
35+
p prime? 7 # also 'true'
36+
```
37+
38+
39+
## Explanation
40+
41+
My JavaScript implementation uses a function named ```isPrime``` that checks if a number is prime by first returning `false` if the number is less than 2, since prime numbers are greater than 1. Then, it tests every number from 2 up to one less than the given number to see if any of them can divide it without leaving a remainder. If it finds one that can divide it evenly, it returns `false` ; if not, it returns `true`, meaning the number is prime.
42+
43+
The ruby implementation has a function named ```prime?``` that also looks at all the numbers from 2 up to one less than the number given. Same rules apply: If none of those numbers can divide it evenly, then it prints ```true```. For example, it tells us if 5 and 7 are prime (they are!) and if 6 is prime (it’s not!).
44+
45+
### Differences
46+
1. Instead of ```function``` used to define what your function is in javascript, Ruby use ```def```
47+
2. Ruby is much more simple - where tthe ```if``` and ```else``` statement is simply in one line
48+
3. Ruby simply 'prints' the result of its functions rather than ```console.log();```
49+
50+

lesson_04/davidsmith/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Java Method
2+
```java
3+
public static boolean isPrime(int n) {
4+
if (n <= 1) return false;
5+
for (int i = 2; i <= Math.sqrt(n); i++) {
6+
if (n % i == 0) {
7+
return false;
8+
}
9+
}
10+
return true;
11+
}
12+
```
13+
## Python Method
14+
```python
15+
def is_prime(n):
16+
if n <= 1:
17+
return False
18+
for i in range(2, int(n**0.5) + 1):
19+
if n % i == 0:
20+
return False
21+
return True
22+
```
23+
24+
## Understanding The Code
25+
26+
Java and Python are similar in some ways. They have the same function structures. The `isPrime` method is in Java. The `is_prime` function is in Python. Both check if \( n \) is less than or equal to 1. They return false if this is true. Then, they use a for-loop. The for-loop finds factors from 2 to the square root of \( n \). Java needs explicit type declarations. Python does not need explicit type declarations. However, the logical flow is similar. This shows their focus on clarity. It makes both languages friendly for users. It also makes them easy for developers with different skills.
27+
28+
## Differences
29+
30+
Java and Python have big differences in syntax. They also have differences in structure. The `isPrime` method shows this. The `is_prime` function also shows this. Java is statically typed. It needs explicit type declarations like `int n`. Python is dynamically typed. It allows defining variables without a type. Java uses the `Math.sqrt` method. This method is for calculating square roots. Python uses the expression `n**0.5` for this. The for-loop syntax is different too. Java has a traditional for-loop with an initializer. Python uses the `range` function for loops. These differences show the different design philosophies. They also show different approaches to readability for each language.

lesson_04/jimoye244/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## Java implementation
2+
3+
```Java
4+
static boolean isPrimeNumber(int number){
5+
int zeroRemainderCount=0;
6+
for(int i=1; i <= number; i++){
7+
if(number % i == 0) zeroRemainderCount++;
8+
}
9+
10+
return zeroRemainderCount == 2 ? true : false;
11+
}
12+
13+
# Example usage:
14+
System.out.println(isPrimeNumber(7)) # Output: true
15+
System.out.println(isPrimeNumber(12)) # Output: false
16+
```
17+
18+
## JavaScript implementation
19+
20+
```javascript
21+
function isPrimeNumber(number){
22+
let zeroRemainderCount = 0;
23+
for(i=0; i<=number; i++){
24+
if(number % i === 0) zeroRemainderCount++;
25+
}
26+
return (zeroRemainderCount === 2) ? true : false;
27+
}
28+
29+
// Example usage:
30+
console.log(isPrimeNumber(11)); // Output: true
31+
console.log(isPrimeNumber(81)); // Output: false
32+
```
33+
34+
## Explanation
35+
36+
The Java implementation uses a function named `isPrimeNumber` that takes a single argument `number`. It returns `true` if the number is prime (e.g, when the zero remainder count of the division of the number by numbers between 1 and itself is two), otherwise, it returns `false`.
37+
38+
The JavaScript implementation uses a function named `isPrimeNumber` that also takes a single argument `number`. It returns `true` if the number is prime (using the same logic as the Java function) and `false` otherwise.
39+
40+
### Improvement in the Function Implementation:
41+
For large numbers, it will be reasonable to exit the loop once the zero remainder count reaches a value of 3.
42+
43+
### Differences
44+
45+
1. **Syntax**:
46+
- In Java, functions are defined by specifying the function name which is preceeded by the return type i.e `boolean isPrimeNumber`, whereas in JavaScript, the `function` keyword preeceds the function name i.e `boolean isPrimeNumber`.
47+
48+
The loop variable `i` was initialize with a value 0 that is type-annotated `int` in Java implementation. However, in JavaScript implementation, the variable `i` was not type-annotated.
49+
50+
2. **Equality Check**:
51+
- Java uses `double equal sign (==)` to test equality whereas JavaScript uses `tripple equal sign (===)` to test equality.
52+
53+
3. **Function Calls**:
54+
- The syntax for calling functions and printing to the console/output is slightly different. Java uses `System.out.println() or System.out.print()`, while JavaScript uses `console.log()`.
55+

lesson_04/josephcaballero/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## Javascript Implementation
2+
```javascript
3+
const prime = (num) => {
4+
if(num <2){console.log(num + " is not a prime number")}
5+
else{
6+
for(let i = 2; i<=10; i++){
7+
if(num%i !== 0){
8+
if(i ===10){
9+
console.log(num + " is a prime number")
10+
break;
11+
}
12+
else{
13+
continue;
14+
}
15+
}
16+
else if(num%i===0 && num !==i){
17+
console.log(num + " is not a prime number")
18+
break;
19+
}
20+
else if(num===2){
21+
console.log(num + " is a prime number")
22+
break;
23+
}
24+
}
25+
}
26+
}
27+
prime(2) // output: 2 is a prime number
28+
prime(11) // output: 11 is a prime number
29+
prime(12) // output: 12 is not a prime number
30+
```
31+
## Python Implementation
32+
```python
33+
34+
def prime(num):
35+
if(num <2):
36+
print(num , " is not a prime number")
37+
else:
38+
for i in range(2,11):
39+
if num % i != 0:
40+
if i ==10:
41+
print(num , " is a prime number")
42+
break
43+
44+
else:
45+
continue
46+
47+
48+
elif num%i==0 and num !=i:
49+
print(num , " is not a prime number")
50+
break
51+
52+
elif num==2:
53+
print(num , " is a prime number")
54+
break
55+
else:
56+
continue
57+
58+
prime(2) # output: 2 is a prime number
59+
prime(11) # output: 11 is a prime number
60+
prime(113) # output: 113 is a prime number
61+
```
62+
63+
## Explanation
64+
My codes both use a function called prime with a parameter of num (num is to be filled with a number). The function is comprised of an if statement first with a condition that checks that the number entered isnt a number less than 2 (due to the fact that any number below 2 is not prime). Next is an else statement for when the number that was given in the parameter is not less than 2. After that is a for loop nested (meaning inside) in the else statement to iterate over a set group of numbers which in my case are 2-10 (because if a number is divided by these numbers and reaches 0 it is not a prime number, in the code it is shown as 2,11 however the digit on the right does not get reached and thus goes up to 10). Then if loops to meet certain conditions, in these if loops I use the percentage sign % which is modulator (mod for short) which checks if there is a remainder after division, be it 10/2 "which is 0" or 13/2 "which is 1" if there is a remainder it is prime and if there is not it is not prime(after all iterations). I used the code to take the number given to me and then checked if every number between 2-10 that was %(modded) by the given number did not meet 0 because if it did that would not be a prime number. To bypass the question of "what if the number you input gets iterated over in the for loop", i set a parameter that skips over that iteration by simply making use of the else loop so that any other iteration would be looked at and not that duplicate numbered one.
65+
66+
In python I did this using the def prime(): code "prime being the function name"
67+
68+
In javascript i did the exact same thing however I made a constant function which is just easier to debug. by doing const prime = (condition) => {code}
69+
70+
### Difference
71+
1. **Syntax**:
72+
- In python the syntax feels much more friendly by looking at it however is is alot scarier when you delve deep into it as you can make loops and function just by using if(condition): or for i in range(): as long as you are matching line indents, not to mention no ; which is alot more responsibility on the programmer
73+
- In javascript it feels much more safer but cluttered, the {} help show where exactly everything is in a more contained way but takes up space, and the ; are nice however they are an extra character, it is a give some take some scenario between these two languages
74+
2. **Different Signs**:
75+
- In Python you use different signs , like the print statement itself, how the statement is structured with a comma, and no parenthesis between most things that javascript would have
76+
- In Javascript the equivalent of print is console.log(), also in the log you put + to combine two values even if they arent the same to yield a log displayed in the console, and there are many more parenthesis like in the conditional statements

lesson_04/yafiahabdullah/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Javascript isPrimeNumber
2+
3+
```javascript
4+
function isPrimeNumber(number){
5+
let result= "not prime";
6+
if(number <= 1){
7+
return result;
8+
}
9+
for(let i = 2; i < number; i++ ){
10+
if(number % i === 0){
11+
return result;
12+
}
13+
}
14+
return "prime number";
15+
}
16+
# Example usage:
17+
console.log(isPrimeNumber(4)) # Output: false
18+
console.log(isPrimeNumber(7)) # Output: true
19+
```
20+
21+
## Explanation
22+
23+
The JavaScript function isPrimeNumber checks if a given number is prime. It initializes a result variable with "not prime" and first checks if the number is less than or equal to 1, returning this result if true. Then, it loops from 2 up to the number, checking for divisibility. If the number can be evenly divided by any value in this range, it returns "not prime". If no divisors are found, the function concludes that the number is prime and returns "prime number"
24+
25+
26+
## Java isPrimeNumber implementation
27+
28+
```java
29+
public class PrimeChecker {
30+
public static String isPrimeNumber(int number) {
31+
if (number <= 1) {
32+
return "not prime number";
33+
}
34+
for (int i = 2; i < number ; i++) {
35+
if (number % i == 0) {
36+
return "not prime number";
37+
}
38+
}
39+
return "prime number";
40+
}
41+
}
42+
# Example usage:
43+
System.out.println(isPrimeNumber(4)) # Output: false
44+
System.out.println(isPrimeNumber(7)) # Output: true
45+
```
46+
47+
## Explanation
48+
49+
The Java implementation uses a function named `isPrimeNumber` This function effectively determines if a number is prime by checking its divisibility with all integers from 2 up to that number, providing clear output based on the result.
50+
51+
### Differences
52+
53+
1.
54+
Overall, while both languages share some similar structural elements (like curly braces for blocks), Java has stricter syntax rules regarding types and class structure, whereas JavaScript is more flexible with its dynamic typing and function definitions.
55+
56+

0 commit comments

Comments
 (0)