Given a positive integer n. Find whether a number is amazing or not. Print True If number is amazing else False. An amazing number is a natural number that is a product of two prime numbers.
Input Format:
The first line of the input contains a single integer T, denoting the number of test cases.
The T test case follows, a single line of the input containing a positive integer N.
Sample Input:
2 6 8
Constraints:
1<=T<=100
1<=N<=100000
Output Format:
Print ‘True’ if it is amazing, otherwise print ‘False’
Sample Output:
True
False
Explanation:
6 is a semiprime number as it is a product of two prime numbers 2 and 3. 8 is not a semiprime number.
Solution
Time complexity: O(root n)
Auxiliary space: O(1)
Reference: https://en.wikipedia.org/wiki/Semiprime
import java.util.*;
public class Sauravhathi {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
if (isAmazing(n) != 0)
//sauravhathi
System.out.println("True");
else
System.out.println("False");
}
}
public static int isAmazing(int n) {
int count = 0;
for (int i = 2; count < 2 && i * i <= n; ++i){
// sauravhathi
while (n % i == 0) {
n /= i;
++count;
}
}
if (n > 1){
++count;
}
return count == 2 ? 1 : 0;
}
}
def isAmazing(n):
count = 0
for i in range(2, n + 1):
while n % i == 0:
n /= i
count += 1
#sauravhathi
return count == 2
N = int(input())
for i in range(N):
n = int(input())
if isAmazing(n):
print("True")
else:
print("False")#include <bits/stdc++.h>
using namespace std;
int isAmazing(int n) {
int count = 0;
for (int i = 2; count < 2 && i * i <= n; ++i) {
while (n % i == 0) {
n /= i;
// sauravhathi
++count;
}
}
if (n > 1) {
++count;
}
return count == 2 ? 1 : 0;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (isAmazing(n))
cout << "True" << endl;
else
cout << "False" << endl;
}
return 0;
}Happy Learning – If you require any further information, feel free to contact me.
![[Solved] Given a positive integer n. Find whether a number is amazing or not [Solved] Given a positive integer n. Find whether a number is amazing or not](https://realcoder.techss24.com/wp-content/uploads/2022/08/Solved-Given-a-positive-integer-n.-Find-whether-a-number-is-amazing-or-not.png)

![[Solved] Reverse the digits with Java](https://realcoder.techss24.com/wp-content/uploads/2022/07/Solved-Reverse-the-digits-with-Java-300x200.png)
![[Solved] Robert is given a string that ends with a character or a number](https://realcoder.techss24.com/wp-content/uploads/2023/02/Solved-Robert-is-given-a-string-that-ends-with-a-character-or-a-number-300x169.png)