Semiprimes Numbers in JavaIn this tutorial, we are going to learn about the semiprimes numbers in Java. A n is said to be the semiprime number if the number n can be written as the product of two prime numbers. We will be exploring in this tutorial how to identify the semiprimes number and also how to filter semiprimes numbers within a range. Example 1: Input: 6 Output: Yes, 6 is the semiprime number. Explanation: 6 is a semiprime number because the number 6 can be written as the product of two prime numbers 2 and 3. Example 2: Input: 10 Output: Yes, 10 is the semiprime number. Explanation: 10 is a semiprime number because the number 10 can be written as the product of two prime numbers 2 and 5. Example 3: Input: 20 Output: No, 20 is not the semiprime number. Explanation: The number 20 is not a semiprime number because it cannot be written as the product of two prime numbers. 20 can written as 2 x 10, or 4 x 5. In 2 x 10, 10 is not the prime number, and in 4 x 5, 4 is not the prime number. Naïve ApproachIf we do the observation, we find that a semiprime number can have exactly four factors, out of which two are prime numbers. For example, 6 is a semiprime number because it has 4 factors as 1, 2, 3, and 6, out of which there are two prime factors 2 and 3. 8 is not a semiprime number because 8 has 4 factors, 1, 2, 4, and 8 out of which only one factor is a prime number, which is number 2. In order to find the factors, we can run a loop starting from 2 to the square root of the number n and compute the factors. The loop will exclude the number itself and 1. After that, we can check whether the computed factor is a prime number or not. If we find, any one of the compute factors is not a prime number we can say that the number n is not a semiprime number. Now comes the implementation part. Filename: SemiPrimeNumbers.java Output: 6 is a Semiprime Number. 10 is a Semiprime Number. 20 is not a Semiprime Number. Complexity Analysis: The time complexity of the program is O(sqrt(n) x log(log(n)). The space complexity of the program is O(1). Another ApproachIn this approach, we will keep dividing the number with the divisor of that number to remove the composite number. Side by side, we will also keep incrementing the variable cnt in order to keep track of the count of the prime numbers. Filename: SemiPrimeNumbers.java Output: 6 is a Semiprime Number. 10 is a Semiprime Number. 20 is not a Semiprime Number. Complexity Analysis: The time complexity of the program is O(sqrt(n)). The space complexity of the program is O(1). Finding Semiprimes Numbers Within a RangeThe following program illustrates how one can find the Semiprime number within a range. Filename: SemiPrimeNumbersRange.java Output: Semiprime Numbers from 1 to 100 are: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95 Next Topic12 Tips to Improve Java Code Performance |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India