wheather the no is prime or not


tried to write a logic to check weather no is prime or not without loops

because loops have running time O(n)

on the other hands algorithms without loops have running time O(1) which is much more efficient

so here is my little try
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
#include <iostream>

using namespace std;

int main()

{

long n;

cout << "enter a number : " << endl;

cin >> n;

if (n == 2 || n == 3 || n == 5 || n == 7)

{

cout << "The no is prime " << endl;
return 0;

}

if (n != 2 && n % 2 == 0 || n==1)

{

cout << "no is not a prime no " << endl;

return 0;

}

if (n % 3 == 0 || n % 4 == 0 || n % 5 == 0 || n % 6 == 0 || n % 7 == 0 || n % 8 == 0 || n % 9 == 0)

{

cout << "no is not a prime no " << endl;

return 0;

}

else

{

cout << "The no is prime " << endl;

return 0;

}

return 0;

}
If you actually did create a generic algorithm to figure out how to check for a prime number you would probably be a billionaire and inducted into the Computer Science Hall of Fame.. This is O(1) time, but won't work when you get to larger numbers.
tried to write a logic to check weather no is prime or not without loops

because loops have running time O(n)

on the other hands algorithms without loops have running time O(1) which is much more efficient

So.. pre-generate a table of prime numbers. Check to see if the number tendered is in the list.

Kind of misses the point, though, doesn't it?
Topic archived. No new replies allowed.