+ 1

Prime numbers

How do I find prime numbers less than a known number?

13th Feb 2020, 9:06 PM
salah
salah - avatar
5 Answers
+ 4
Roman J I haven't considered time complexity. If that's so, then for loop to sqrt(n) will be even more efficient This will give time complexity of O(sqrt(n))
13th Feb 2020, 9:41 PM
Deepraj Baidya
Deepraj Baidya - avatar
+ 3
Look here, this should work perfectly: https://www.sololearn.com/learn/969/?ref=app
13th Feb 2020, 9:11 PM
HonFu
HonFu - avatar
+ 2
#include <iostream> using namespace std; int main() { int n, i; bool isPrime = true; cout << "Enter a positive integer: "; cin >> n; for(i = 2; i <= n / 2; ++i) { if(n % i == 0) { isPrime = false; break; } } if (isPrime) cout << "This is a prime number"; else cout << "This is not a prime number"; return 0; }
18th Feb 2020, 11:10 AM
acb
acb - avatar
+ 1
Make a Prime() function which contains a for loop i in the range between 2 and a num (say n). It will return False if n%i = = 0. and then return True. // will execute only if the above for loop doesn't return anything // In the main function use another for loop in the range 2 to the input num (say p) . so primes are present between 2 and p. int main() { for (i= 2 ; i<p;i++) { if( Prime(i) ==True) cout<
13th Feb 2020, 9:20 PM
Deepraj Baidya
Deepraj Baidya - avatar
0
1 is noy a prime number , but in ur code the opposit
18th Feb 2020, 4:28 PM
salah
salah - avatar