-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathctci-big-o.cpp
More file actions
40 lines (36 loc) · 772 Bytes
/
Copy pathctci-big-o.cpp
File metadata and controls
40 lines (36 loc) · 772 Bytes
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
// Time Complexity: Primality
// Determine whether or not a number is prime in optimal time.
//
// https://www.hackerrank.com/challenges/ctci-big-o/problem
//
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool is_prime(int n)
{
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
int d = 3;
while (d * d <= n)
{
if (n % d == 0) return false;
d += 2;
}
return true;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int q;
cin >> q;
while (q--)
{
int n;
cin >> n;
cout << (is_prime(n) ? "Prime" : "Not prime") << endl;
}
return 0;
}