|
| 1 | +# Lesson 04: |
| 2 | + |
| 3 | +## Python code |
| 4 | + |
| 5 | +```python |
| 6 | +def is_prime(n): |
| 7 | + """Check if a number is prime.""" |
| 8 | + if n < 2: |
| 9 | + return False |
| 10 | + if n in (2, 3): |
| 11 | + return True |
| 12 | + if n % 2 == 0 or n % 3 == 0: |
| 13 | + return False |
| 14 | + |
| 15 | + # Check divisibility using 6k ± 1 rule |
| 16 | + i = 5 |
| 17 | + while i * i <= n: |
| 18 | + if n % i == 0 or n % (i + 2) == 0: |
| 19 | + return False |
| 20 | + i += 6 |
| 21 | + return True |
| 22 | + |
| 23 | +def main(): |
| 24 | + try: |
| 25 | + num = int(input("Enter a number: ")) |
| 26 | + if is_prime(num): |
| 27 | + print(f"{num} is a prime number.") |
| 28 | + else: |
| 29 | + print(f"{num} is not a prime number.") |
| 30 | + except ValueError: |
| 31 | + print("Invalid input! Please enter an integer.") |
| 32 | + |
| 33 | + Run the program |
| 34 | +if __name__ == "__main__": |
| 35 | + main() |
| 36 | +``` |
| 37 | + |
| 38 | +## Java Code |
| 39 | + |
| 40 | +```java |
| 41 | +import java.util.Scanner; |
| 42 | + |
| 43 | +public class PrimeChecker { |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + Scanner scanner = new Scanner(System.in); |
| 47 | + System.out.print("Enter a number: "); |
| 48 | + int number = scanner.nextInt(); |
| 49 | + |
| 50 | + if (isPrime(number)) { |
| 51 | + System.out.println(number + " is a prime number"); |
| 52 | + } else { |
| 53 | + System.out.println(number + " is not a prime number"); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static boolean isPrime(int number) { |
| 58 | + // Numbers less than or equal to 1 are not prime |
| 59 | + if (number <= 1) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + // Check from 2 up to the square root of the number |
| 64 | + for (int i = 2; i <= Math.sqrt(number); i++) { |
| 65 | + // If the number is divisible by any number in the range, it's not prime |
| 66 | + if (number % i == 0) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // If no divisors were found, the number is prime |
| 72 | + return true; |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## Compare and Contrast Java and Python |
| 78 | + |
| 79 | + In this assignment I am comparing and contrasting python’s code to Java’s code. These codes are about selecting prime numbers and the differences I see between them. The first thing I notice with these codes is Java’s code is three times bigger than the code in Python. With that being said it seems like Java needs more written code in it to achieve the same goal Python has. Another thing I have noticed is that Java you have to import java.util.Scanner; this just means that the scanner class is available for our program. Java also has public static void main(String[] args) which is the entry point for the java program. One thing that I noticed was that Python did not have any else statements. In python it seems as though you don’t have to declare n as a number while in Java you have to. Some similarities that Java and Python have in common are that they both use return, int, if, and for statements to complete the code. |
0 commit comments