|
| 1 | +## Python implementation |
| 2 | + |
| 3 | +```python |
| 4 | +def is_prime(number): |
| 5 | + # Numbers less than 2 are not prime |
| 6 | + if number < 2: |
| 7 | + return False |
| 8 | + |
| 9 | + # Check for divisibility from 2 to square root of number |
| 10 | + for i in range(2, int(number ** 0.5) + 1): |
| 11 | + if number % i == 0: |
| 12 | + return False |
| 13 | + |
| 14 | + return True |
| 15 | + |
| 16 | +# Example usage: |
| 17 | +print(is_prime(4)) # Output: False |
| 18 | +print(is_prime(7)) # Output: True |
| 19 | +print(is_prime(13)) # Output: True |
| 20 | +``` |
| 21 | + |
| 22 | +## C++ implementation |
| 23 | +```C++ |
| 24 | +bool isPrime(int number) { |
| 25 | + // Numbers less than 2 are not prime |
| 26 | + if (number < 2) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + // Check for divisibility from 2 to square root of number |
| 31 | + for (int i = 2; i <= sqrt(number); i++) { |
| 32 | + if (number % i == 0) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +// Example usage: |
| 41 | +// #include <iostream> |
| 42 | +// #include <cmath> |
| 43 | +int main() { |
| 44 | + std::cout << std::boolalpha; |
| 45 | + std::cout << isPrime(4) << std::endl; // Output: false |
| 46 | + std::cout << isPrime(7) << std::endl; // Output: true |
| 47 | + std::cout << isPrime(13) << std::endl; // Output: true |
| 48 | + return 0; |
| 49 | +} |
| 50 | +``` |
| 51 | +
|
| 52 | +## Explanation |
| 53 | +In Python, a function called `is_prime` takes a parameter called number. It returns False if the number is less than 2. The second way it would return False is by dividing the number by every single number starting from two until its square root, and if at any time, the number has a remainder that is equal to zero, it shall return False. It will return True if there are no remainders equal to 0. Then it prints out whether the number is True or False. |
| 54 | +
|
| 55 | +In C++ the function called `isPrime` will return false if the parameter called number is less than 2 or if number is divided by any number from 2 to its square root have a remainder that is equal to zero. It will return true if there are no remainders equal to 0. It will then output if the result is true or false. |
| 56 | +
|
| 57 | +## Differences |
| 58 | +1. **Syntax**: |
| 59 | + - In C++, when you define a function, you must declare what is being returned in the function (`bool`) and you have to declare what type the parameter variable is (`int`) (example: `int number`). In Python, all you need to do is give the function a name and don't have to declare the parameter for `number`. |
| 60 | + - Python uses indentations to define code blocks while C++ uses braces `{}`. |
| 61 | + - Python uses `True` and `False` with the first letters capitalized while C++ uses `true` and `false` with the entire words in lowercase. |
| 62 | +
|
| 63 | +2. **Function calls**: |
| 64 | + - Python can call a function and print it using the print function `print(is_prime(4))` and this will print the result. |
| 65 | + - C++ uses `std::cout << std::boolalpha;` to display boolean values as words. Normally it would be 1 for true and 0 for false but this will display true or false instead. |
0 commit comments