-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
check_T_prime.cpp
84 lines (73 loc) · 1.75 KB
/
check_T_prime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Given a number and you need to check if a number is T-prime or not.
* A number is called T-prime if it has exactly three factors.
* Input:
* First line of input contains a number of integer data type.
* Output:
* A single line telling whether a number is T-prime or not.
*
*/
#include <bits/stdc++.h>
using namespace std;
int Check_T_Prime(int n)
{
if (n <= 3)
{
return 0;
}
else
{
long long int square_root = sqrt(n);
int perfect_square_prime = 0, i;
//to check if number is perfect square or not.
if (square_root * square_root == n)
{
for (i = 2; i <= sqrt(square_root); i++)
{
/* If the square root of the
user input number (num) is prime then it's T-prime otherwise it's not.
Because for T-prime number should have 3 factors(one the number itself)
and other 2 from square root as it prime number.
*/
if (square_root % i == 0)
{
perfect_square_prime = 1;
break;
}
}
if (perfect_square_prime == 0)
return 1;
else
return 0;
}
else
return 0;
}
}
int main()
{
long long int num;
cout << "Enter the number: " << endl;
cin >> num;
int result = Check_T_Prime(num);
if (result == 1)
cout << num << " is T-prime. " << endl;
else
cout << num << " is not T-prime. " << endl;
}
/*
* Example:
* Input:
* 3
* Output:
* 3 is not T-prime.
*
* Input:
* 49
* Output:
* 49 is T-prime;
*/
/*
*Time complexity : O(sqrt(n))
*Space complexity : O(1)
*/