|
| 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 --> |
0 commit comments