|
| 1 | +## Java Implementation |
| 2 | + |
| 3 | +```java |
| 4 | +if (n <= 1) { |
| 5 | + System.out.println(n + " is not a prime number."); |
| 6 | +} else if (n == 2) { |
| 7 | + System.out.println(n + " is a prime number."); |
| 8 | +} else if (n % 2 == 0) { |
| 9 | + System.out.println(n + " is not a prime number."); |
| 10 | +//ChatGPT helped me to complete the code from this point.// |
| 11 | +} else { |
| 12 | + boolean isPrime= true; |
| 13 | + for (int i = 3; i <= Math.sqrt(n); i += 2) { |
| 14 | + if (n % i == 0) { |
| 15 | + isPrime = false; |
| 16 | + break; |
| 17 | + } |
| 18 | + } |
| 19 | + if (isPrim) { |
| 20 | + System.out.println(n + " is a prime number."); |
| 21 | + } else { |
| 22 | + System.out.println(n + " is not a prime number."); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +# Example Usage: |
| 27 | +Input: n = 7 |
| 28 | +Output: if (7 <= 1) // Result: No, 7 is not less than or equal to 1 |
| 29 | + else if (7 == 2) // Result: No, 7 is not equal to 2 |
| 30 | + else if (7 % 2 == 0) // Result: No, 7 mod 2=1, 7 is not even |
| 31 | + else |
| 32 | + boolean isPrime = true // Initialize a boolean variable to true to check for odd factors |
| 33 | + for (int i = 3; i <= Math.sqrt(7; i += 2) // Result: √7 ≈ 2.645, 3<=2.645, Answer: No, so isPrime is true |
| 34 | + Print Result: 7 is a prime number. |
| 35 | +``` |
| 36 | + |
| 37 | +## C++ Implementation |
| 38 | + |
| 39 | +```c++ |
| 40 | +// C++ code created and explained with the help of ChatGPT and Google Search |
| 41 | +bool isPrime(int number) { |
| 42 | + // Handle special cases |
| 43 | + if (number <= 1) return false; // 0 and 1 are not prime numbers |
| 44 | + if (number <= 3) return true; // 2 and 3 are prime numbers |
| 45 | + |
| 46 | + // Check for even numbers greater than 2 |
| 47 | + if (number % 2 == 0) return false; |
| 48 | + |
| 49 | + // Check for factors from 3 to the square root of the number |
| 50 | + for (int i = 3; i * i <= number; i += 2) { |
| 51 | + if (number % == 0) return false; // Found a factor |
| 52 | + } |
| 53 | + |
| 54 | + return true; // No factors found, number is prime |
| 55 | +} |
| 56 | + |
| 57 | +int main() { |
| 58 | + int num; |
| 59 | + std::cout << "Enter a number: "; |
| 60 | + std::cin >> num; |
| 61 | + |
| 62 | + if (isPrime(num)) { |
| 63 | + std::cout << num << " is a prime number." << std::endl; |
| 64 | + } else { |
| 65 | + std::cout << num << " is not a prime number." << std::endl; |
| 66 | + } |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +# Example Usage: |
| 72 | +Input: isPrime(7) |
| 73 | +Output: if (7 <= 1) return false // Result: 7 is not less than or equal to 1 |
| 74 | + if (7 <= 3) return true // Result: 7 is greater than 3 |
| 75 | + if (7 % 2 == 0) return false // Result: 7%2=1 |
| 76 | + for (int i = 3; i * i <= 7; i += 2) // Result: i starts at 3, 3 * 3 <= 7, 9 <= 7, false |
| 77 | + return true // Result: No factors found |
| 78 | +Print Result: Since isPrime(7) returned true: 7 is a prime number. |
| 79 | +``` |
| 80 | + |
| 81 | + |
| 82 | +## Explanation |
| 83 | + |
| 84 | +### Thought Process |
| 85 | + |
| 86 | +I chose to use the programming languages Java and C++. I chose Java because I have been introduced to it before through Codio and have an introductory level understanding of some concepts. I chose C++ because after understanding what a prime number is and its uses, I found that it is a crucial part of cryptography. The difficulty of factoring large prime numbers makes it ideal for creating secure encryption algorithms. Due to its high performance, control over memory management, and ability to optimize code for speed, especially for complex algorithms and time sensitive situations, C++ has the title of being one of the best programming languages for creating encryption algorithms. (Source: Google search, AI) |
| 87 | + |
| 88 | +### Implementation |
| 89 | + |
| 90 | +The Java implementation determines if a number is a prime number by: |
| 91 | + |
| 92 | +- Understanding what a prime number is (a number that is greater than 1 and is only divisible by 1 and itself), then setting up the logic. |
| 93 | + |
| 94 | +- Use a boolean variable `isPrime` in an if/else conditional statement. If `isPrime` returns false, n is not a prime number. If `isPrime` returns true, n is a prime number. |
| 95 | + |
| 96 | +The C++ implementation determines if a number is a prime number by: |
| 97 | + |
| 98 | +- Using a conditional statement within a function. |
| 99 | + |
| 100 | +- The `main` function takes user input and uses the `isPrime` function to determine if a number is a prime number. If `isPrime` returns true, n is a prime number. If `isPrime` returns false, n is not a prime number. |
| 101 | + |
| 102 | +### Similarities |
| 103 | + |
| 104 | +- Both programming languages used conditional statements with true/false values to determine if a number is a prime number. |
| 105 | + |
| 106 | +- Similar syntax: `isPrime` |
| 107 | + |
| 108 | +- Similar code operation: Start with special cases `n<=1, n<=3`, then check for even numbers greater than 2, loop to check factors before coming to the output. |
| 109 | + |
| 110 | +### Differences |
| 111 | + |
| 112 | +1. **Syntax**: |
| 113 | +- In Java, the true/false value is called `boolean`, whereas in C++ the true/false value is called `bool`. |
| 114 | +- C++ uses a `main` function to take user input during the output process, while in Java it is assumed that the `isPrime` starts off as true until proved false. |
| 115 | + |
| 116 | +2. **Function Calls**: |
| 117 | +- The syntax for printing to the console/output is different. Java uses `System.out.println()`, while C++ uses `std::cout << "insert string" << std::endl;`. |
| 118 | +`(Source: ChatGPT, Google Search, Codio notes)` |
0 commit comments