Update appNew update is available. Click here to update.
About
My Stats
EXP gained
yellow-spark
559
Level
4 (Scholar)
Community stats
Discussions
0
Upvotes
1
Know more
12
Total problems solved
11
Easy
1
Moderate
0
Hard
0
Ninja
Jan Jan Feb Feb Mar Mar Apr Apr May May Jun Jun Jul Jul Aug Aug Sep Sep Oct Oct Nov Nov Dec Dec

Current streak:

0 days

Longest streak:

3 days

Less

More

Discussions
Solution in time complexity: O(sqrt(n))
Interview problems

/*we can find all the divisors of n if we run a loop till the square root of n as we can find two of its divisors at a given i (if n divisible by i) ie i and n/i. 

so time complexity: O(sqrt(n))

space complexity O(1)

*/

#include<bits/stdc++.h> using namespace std;

bool checkPrime(int n){ bool flag = true; for(int i=2; i<= sqrt(n); i++){  if(n%i==0 || n%(n/i)==0){   flag = false;   break;  } } return flag; }

int main() { int n; cin >> n; if(checkPrime(n)) cout << "true"; else cout << "false"; }  

profile
adesh ghadage
Published On 04-Aug-2023
60 views
0 replies
1 upvotes