|
| 1 | +## Python Implementation |
| 2 | +``` |
| 3 | +def is_prime(number): |
| 4 | + if number <= 1: |
| 5 | + return False |
| 6 | + for i in range(2, int(number ** 0.5) + 1): |
| 7 | + if number % i == 0: |
| 8 | + return False |
| 9 | + return True |
| 10 | +
|
| 11 | +if __name__ == "__main__": |
| 12 | + test_numbers = [1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19, 21] |
| 13 | + for num in test_numbers: |
| 14 | + print(f"Is {num} prime? {is_prime(num)}") |
| 15 | +``` |
| 16 | +## C# Implementation |
| 17 | +``` |
| 18 | +using System; |
| 19 | +
|
| 20 | +public class PrimeChecker |
| 21 | +{ |
| 22 | + public static bool IsPrime(int number) |
| 23 | + { |
| 24 | + if (number <= 1) return false; |
| 25 | + if (number == 2) return true; |
| 26 | + if (number % 2 == 0) return false; |
| 27 | +
|
| 28 | + int sqrt = (int)Math.Sqrt(number); |
| 29 | + for (int i = 3; i <= sqrt; i += 2) |
| 30 | + { |
| 31 | + if (number % i == 0) |
| 32 | + return false; |
| 33 | + } |
| 34 | + return true; |
| 35 | + } |
| 36 | +
|
| 37 | +public static void Main(string[] args) |
| 38 | + { |
| 39 | + Console.WriteLine($"Is 7 prime? {IsPrime(7)}"); |
| 40 | + Console.WriteLine($"Is 15 prime? {IsPrime(15)}"); |
| 41 | + Console.WriteLine($"Is 23 prime? {IsPrime(23)}"); |
| 42 | + Console.WriteLine($"Is 1 prime? {IsPrime(1)}"); |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | +## Similarities |
| 47 | + - Both implementations use for loops to check if the number was a prime. |
| 48 | + - Both functions use "number" as the input of the function. |
| 49 | + - Both handle edge cases by checking if number ≤ 1. |
| 50 | + - Both return a boolean value. |
| 51 | + - Both return false if number % i and is equal to 0. |
| 52 | +## Differences |
| 53 | + - C# uses more optimized approach by explicitly handling even numbers and only checking odd divisors. |
| 54 | + - Python checks all numbers up to the square root. |
| 55 | + - C# requires more explicit syntax with type declarations and a class structure, while Python's implementation is simpler. |
| 56 | + - C# uses Math.Sqrt() for calculating the square root while Python uses the power operator (**). |
| 57 | + - C# version includes additional optimizations like checking for even numbers separately and only testing odd divisors. |
| 58 | + |
| 59 | + ## Test Cases |
| 60 | +Both implementations were tested with the following test cases: |
| 61 | + |
| 62 | +1. Negative Numbers: |
| 63 | + - Test inputs: -1, -5, -10 |
| 64 | + - Expected result: All should return false (not prime) |
| 65 | + |
| 66 | +2. Edge Cases (0 and 1): |
| 67 | + - Test inputs: 0, 1 |
| 68 | + - Expected result: Both should return false (not prime) |
| 69 | + |
| 70 | +3. Prime Numbers: |
| 71 | + - Test inputs: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 |
| 72 | + - Expected result: All should return true (prime) |
| 73 | + |
| 74 | +4. Non-Prime Numbers: |
| 75 | + - Test inputs: 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20 |
| 76 | + - Expected result: All should return false (not prime) |
| 77 | + |
| 78 | +Both Python and C# unit tests verify these cases using their respective testing frameworks (unittest for Python and MSTest for C#). |
0 commit comments