Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Josephca
Browse files Browse the repository at this point in the history
bs/code-differently-24-q4 into feat/lesson06
  • Loading branch information
Josephcabs committed Oct 8, 2024
2 parents 0b0d1fc + 556b095 commit d43614d
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lesson_04/amiyahjones/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## Javascript
``` javascript

function isPrime(num) {
if (num < 2) {
return false; // Numbers less than 2 are not prime
}

for (let i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}

return true;
}

console.log(isPrime(5)); //true
console.log(isPrime(1)); //false

```
## Ruby
###### Tutorial used : https://www.youtube.com/watch?v=33pLqGvk-PM
``` ruby
def prime?(n)
# '2..n-1' checks all the numbers from 2 up to less than 'n'.
# 'none' -> sees if none of these numbers can divide 'n' evenly.
(2..n-1).none? {|divisor| n % divisor == 0}
# If none of these numbers divide 'n' without a remainder,
# then 'n' is a prime number. Otherwise, it's not.
end

p prime? 5 # prints 'true'
p prime? 6 # prints 'false'
p prime? 7 # also 'true'
```


## Explanation

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.

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!).

### Differences
1. Instead of ```function``` used to define what your function is in javascript, Ruby use ```def```
2. Ruby is much more simple - where tthe ```if``` and ```else``` statement is simply in one line
3. Ruby simply 'prints' the result of its functions rather than ```console.log();```


70 changes: 70 additions & 0 deletions lesson_04/chigazograham/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
## Python implementation

```python
def is_prime(num):
# uses the function is_prime and takes the argument num
if num > 1:
# gets rid of numbers 1 and lower as the first prime number is 2
for i in range(2, (num // 2) + 1):
# 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
if(num % i) == 0:
# then divides the num value, using modulo division, by every number in the range of two to +infinity
return False
# if the remainder in equivalent to 0 then the number is not prime and returns False
else:
return True
# if the remaineder is not equivalent to 0 then the terminal will return True
else:
return False
# for all other real numbers that arent prime the terminal will reurn False

print(is_prime(19)) #Output: True
print(is_prime(9)) #Output: False
```

## Ruby implementation

```ruby
def isPrime(n)
#uses the function isPrime and takes the argument n
return false if n <= 1
#the computer will reurn false for all numbers less than or equal to zero
return true if n == 2 || n == 3
#tells the computer to return true if the n is equivalent to 2 or 3

return false if n % 2 == 0 || n % 3 == 0
#returns false if n is divisible by two or 3

i = 5
#defines i starting value as 5
while i * i <= n
#while n is greater than or equal to 25
return false if n % i == 0 || n % (i + 2) == 0
#return false if n is divisible by 5 or 7
i += 6
#adds 6 to the value of i if n is not divisible by 5 or 7 and runs the loop again
end
true
#returns true for all numbers that arent divisible by 2, 3, 5, or 7
end
puts isPrime(17) #Output: true
puts isPrime(24) #Output: false
```

## Explanation

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.

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.

### Differences

1. **Syntax**:
- Python uses `True` and `False` for boolean values, while Ruby uses `true` and `false`.
- 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
- In Python, a colon(`:`) is used to close function statements, whereas in Ruby there is nothing closing the function statements.
- Ruby can return true or false without a return statement. In contrast Python requires a return statement or will return with `None`.
- 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.
- The syntax for calling functions and printing to the console/output is different. Python uses `print()`, while Ruby uses `puts()`.

<!-- Used code from geeksforgeeks.org to assist in creating assignment -->
30 changes: 30 additions & 0 deletions lesson_04/davidsmith/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Java Method
```java
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
```
## Python Method
```python
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
```

## Understanding The Code

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.

## Differences

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.
55 changes: 55 additions & 0 deletions lesson_04/jimoye244/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Java implementation

```Java
static boolean isPrimeNumber(int number){
int zeroRemainderCount=0;
for(int i=1; i <= number; i++){
if(number % i == 0) zeroRemainderCount++;
}

return zeroRemainderCount == 2 ? true : false;
}

# Example usage:
System.out.println(isPrimeNumber(7)) # Output: true
System.out.println(isPrimeNumber(12)) # Output: false
```

## JavaScript implementation

```javascript
function isPrimeNumber(number){
let zeroRemainderCount = 0;
for(i=0; i<=number; i++){
if(number % i === 0) zeroRemainderCount++;
}
return (zeroRemainderCount === 2) ? true : false;
}

// Example usage:
console.log(isPrimeNumber(11)); // Output: true
console.log(isPrimeNumber(81)); // Output: false
```

## Explanation

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`.

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.

### Improvement in the Function Implementation:
For large numbers, it will be reasonable to exit the loop once the zero remainder count reaches a value of 3.

### Differences

1. **Syntax**:
- 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`.

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.

2. **Equality Check**:
- Java uses `double equal sign (==)` to test equality whereas JavaScript uses `tripple equal sign (===)` to test equality.

3. **Function Calls**:
- 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()`.

76 changes: 76 additions & 0 deletions lesson_04/josephcaballero/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
## Javascript Implementation
```javascript
const prime = (num) => {
if(num <2){console.log(num + " is not a prime number")}
else{
for(let i = 2; i<=10; i++){
if(num%i !== 0){
if(i ===10){
console.log(num + " is a prime number")
break;
}
else{
continue;
}
}
else if(num%i===0 && num !==i){
console.log(num + " is not a prime number")
break;
}
else if(num===2){
console.log(num + " is a prime number")
break;
}
}
}
}
prime(2) // output: 2 is a prime number
prime(11) // output: 11 is a prime number
prime(12) // output: 12 is not a prime number
```
## Python Implementation
```python

def prime(num):
if(num <2):
print(num , " is not a prime number")
else:
for i in range(2,11):
if num % i != 0:
if i ==10:
print(num , " is a prime number")
break

else:
continue


elif num%i==0 and num !=i:
print(num , " is not a prime number")
break

elif num==2:
print(num , " is a prime number")
break
else:
continue

prime(2) # output: 2 is a prime number
prime(11) # output: 11 is a prime number
prime(113) # output: 113 is a prime number
```

## Explanation
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.

In python I did this using the def prime(): code "prime being the function name"

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}

### Difference
1. **Syntax**:
- 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
- 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
2. **Different Signs**:
- 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
- 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
56 changes: 56 additions & 0 deletions lesson_04/yafiahabdullah/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
## Javascript isPrimeNumber

```javascript
function isPrimeNumber(number){
let result= "not prime";
if(number <= 1){
return result;
}
for(let i = 2; i < number; i++ ){
if(number % i === 0){
return result;
}
}
return "prime number";
}
# Example usage:
console.log(isPrimeNumber(4)) # Output: false
console.log(isPrimeNumber(7)) # Output: true
```

## Explanation

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"


## Java isPrimeNumber implementation

```java
public class PrimeChecker {
public static String isPrimeNumber(int number) {
if (number <= 1) {
return "not prime number";
}
for (int i = 2; i < number ; i++) {
if (number % i == 0) {
return "not prime number";
}
}
return "prime number";
}
}
# Example usage:
System.out.println(isPrimeNumber(4)) # Output: false
System.out.println(isPrimeNumber(7)) # Output: true
```

## Explanation

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.

### Differences

1.
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.


0 comments on commit d43614d

Please sign in to comment.