|
| 1 | +## Java Implementation |
| 2 | + |
| 3 | +```java |
| 4 | + |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +public class PrimeCheck { |
| 8 | + static boolean isPrime(int n) { |
| 9 | + if (n <= 1) return false; |
| 10 | + if (n <= 3) return true; |
| 11 | + if (n % 2 == 0 || n % 3 == 0) return false; |
| 12 | + |
| 13 | + for (int i = 5; i * i <= n; i += 6) { |
| 14 | + if (n % i == 0 || n % (i + 2) == 0) return false; |
| 15 | + } |
| 16 | + return true; |
| 17 | + } |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + Scanner scanner = new Scanner(System.in); |
| 21 | + System.out.print("Enter a number: "); |
| 22 | + int num = scanner.nextInt(); |
| 23 | + System.out.println(isPrime(num) ? "Prime" : "Not Prime"); |
| 24 | + scanner.close(); |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## JavaScript implementation |
| 30 | + |
| 31 | +```javascript |
| 32 | + |
| 33 | +function isPrime(n) { |
| 34 | + if (n <= 1) return false; |
| 35 | + if (n <= 3) return true; |
| 36 | + if (n % 2 === 0 || n % 3 === 0) return false; |
| 37 | + |
| 38 | + for (let i = 5; i * i <= n; i += 6) { |
| 39 | + if (n % i === 0 || n % (i + 2) === 0) return false; |
| 40 | + } |
| 41 | + return true; |
| 42 | +} |
| 43 | + |
| 44 | +let num = parseInt(prompt("Enter a number: ")); |
| 45 | +console.log(isPrime(num) ? "Prime" : "Not Prime"); |
| 46 | +``` |
| 47 | + |
| 48 | +## Explanation |
| 49 | + |
| 50 | +It is common for all programming languages to be able to perform the same task. Still, for this assignment, after analyzing the two programming languages above, Java and JavaScript, I realized the significance of efficiency. The following assessment examines Java and JavaScripts similarities, differences, and performance characteristics when identifying a prime number. |
| 51 | + |
| 52 | +Mr. Mays often reminds us that "programming is not for computers, but for humans. "Thus, humans rely on the information within Java and JavaScript to communicate. Both programming languages depend on algorithms, various logic structures, return values, and code readability, allowing for a more streamlined approach when programmers communicate. |
| 53 | + |
| 54 | +For this context, the codes have the same internal optimization (6k ± 1 optimization) [^1]; it is just a matter of when they are performed. Java first defines conditions (in lines 9-11). These conditional statements are instructed to generate a "true" value if it is prime and "false" if not. This technique allows data entered within the code to be easily separated. The rules established within the code can be viewed as a pre-test to ensure that numbers less than or equal to one and integers divisible by two/three are discarded early to improve efficiency. Alternatively, JavaScript employs a function (isPrime) to eliminate numbers that are not prime. While both languages have their respective processes to assess the prime values, each version has clear instructions. |
| 55 | +Moreover, when producing these lines of code, it is essential to consider the intended usage of each program because it influences how data is processed and produced. For instance, Java is statically typed—int n (relies on the variable n being declared as an integer before executing the command) [2]—juxtaposed to JavaScript, which is dynamically typed (meaning it is associated with a runtime variable, within this code using the function isPrime(n), to compile and run after assigning a numerical value to n to be processed) [3]. With a dynamic interface, JavaScript allows the programmer to write code faster because they do not need to declare every command. However, this flexibility can lead to more significant issues within the code later, whereas Java has checks and balances every time. |
| 56 | + |
| 57 | +Java uses a scanner for this examination—not just for prime numbers but also for handling user input—so its lengthier, more restrictive code is better suited for this operation. Why? JavaScript, which uses an internal compiler, does not require explicit user input handling as Java does. However, JavaScript still includes a pre-test, meaning it filters out numbers that are not prime before executing the complete algorithm. |
| 58 | + |
| 59 | +Footnotes |
| 60 | + |
| 61 | +1. “All primes more significant than three are of the form 6k ± 1, where k is any integer greater than 0. All integers can be expressed as (6k + i), where i = −1, 0, 1, 2, 3, or 4. And note that 2 divides (6k + 0), (6k + 2), and (6k + 4) and three divides (6k + 3)” Kartik, “Introduction to Primality Test and School Method,” GeeksforGeeks, November 21, 2015, https://www.geeksforgeeks.org/introduction-to-primality-test-and-school-method/. |
| 62 | + |
| 63 | +Bibliography |
| 64 | +Kartik, “Introduction to Primality Test and School Method,” GeeksforGeeks, November 21, 2015, https://www.geeksforgeeks.org/introduction-to-primality-test-and-school-method/., “JavaScript | MDN,” MDN Web Docs, March 6, 2025, https://developer.mozilla.org/en-US/docs/Web/JavaScript#reference., TechGatha, “Statically vs Dynamically Typed Languages - YouTube,” www.youtube.com, April 18, 2020, https://www.youtube.com/watch?v=jlUZw8-6ljw. |
0 commit comments