Prime Numbers

Jan 3, 2010 at 10:27pm
A prime number is defined as an odd integer that is not divisible by any odd integer less than or equal to the square root of the number. So to translate that to C++, do I start with the square root of the number after determining it is odd? Any suggestions?
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
#include <iostream>
#include <cmath>

using namespace std;

int main() 
{
	int number;
	cout<<"Enter number, to exit enter -1: "<<endl;
	cin>>number;
	do
	{
		if(number < 0)
			number = number * -1;
	    if(number == 2)
		    cout<<number<<" is a prime number."<<endl;
	    else if((number % 2) == 0)
		    cout<<number<<" is not a prime number."<<endl;
	    else if((number % 2) > 0)
	    {
			            // an odd integer is prime if it is not
                                     //divisible by any odd integer
	    }               // less than or equal to the square root of number
		cout<<"Enter number, to exit enter -1: "<<endl;
		cin>>number;
	}while(number != -1);
	return 0;
}
Last edited on Jan 3, 2010 at 10:28pm
Jan 3, 2010 at 10:40pm
loop from 3 to sqrt(number)
Jan 3, 2010 at 10:43pm
is 2 not a prime number?, a prime number can only be divided by 1 or itself


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





#include <iostream>
#include <cmath>
using namespace std;
int main()

{

  int c, d;

  c=d=0;

  bool isprime;

    for(c=1; c <= 100; c= c+1){
    isprime = true;
    for(d=2; d<=c/2; d=d+1)

    if ((c % d) ==0) isprime = false;

    if (isprime)
    cout << c << " is prime\n";}

  return 0;
}







,,,
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is,,,




Last edited on Jan 3, 2010 at 10:53pm
Jan 3, 2010 at 10:45pm
I think you meant 1?
Jan 3, 2010 at 10:52pm
yepp your right, i edited it, thanks for pointing that out
Last edited on Jan 3, 2010 at 10:54pm
Jan 3, 2010 at 10:52pm
Part of the problem for me is really understanding what a prime number is lol. My book says 2 is a prime number? When I try to use sqrt(number) I get the following errors:

ocuments\visual studio 2008\projects\5.7\5.7\main.cpp(21) : error C2668: 'sqrt' : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(581): could be 'long double sqrt(long double)'
1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(533): or 'float sqrt(float)'
1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(128): or 'double sqrt(double)'

And I am not trying to assign sqrt a variable either. Here is where I am at atm:
1
2
3
4
5
6
7
8
9
10
else if((number % 2) > 0)
{
        for(i = 0; i < sqrt(number); i++)
        {
             if((i % 2) != 0)
                cout<<number<<" is a prime number."<<endl;
             else
                 cout<<number<<" is not a prime number."<<endl;
        }
}
Jan 3, 2010 at 10:54pm
You will have to cast "number" to the correct type (since number could be changed into a float, a double, or a long double, the compiler doesn't know which one). e.g.:

for(i = 0; i < sqrt(double(number)); ++i)
Jan 3, 2010 at 10:59pm
Prime Number:
–noun
Mathematics. a positive integer that is not divisible without remainder by any integer except itself and 1, with 1 often excluded: The integers 2, 3, 5, and 7 are prime numbers

Last edited on Jan 3, 2010 at 11:04pm
Jan 3, 2010 at 11:04pm
ha I read about problems with ambiguity with otherwise correct code firedraco, was wondering when i would see an instance of it, kool beans
Jan 3, 2010 at 11:46pm
I have semantic errors in the while loop, but other than that the program is somewhat functional. Thank you all for the help and input, hopefully I can get this going correctly without using the sqrt function.

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
#include <iostream>

using namespace std;

int main() 
{
	int number, temp, i = 1;
	cout<<"Enter number, to exit enter -1: "<<endl;
	cin>>number;
	do
	{
		if(number < 0)
			number = number * -1;
		if(number == 1)
			cout<<number<<" is a prime number."<<endl;
		switch(number % 2)
		{
		case 0:       //the number is even
			if(number == 2)
				cout<<number<<" is a prime number."<<endl;
			else
				cout<<number<<" is not prime."<<endl;
			break;
		default:      //the number is odd
			temp = number;
			while(temp > 0)
			{
				if(number % i == 0)
					cout<<number<<" is a prime number."<<endl;
				i = i + 2;
				temp--;
			}
		}
        cout<<"Enter number, to exit enter -1: "<<endl;
		cin>>number;
	}while(number != -1);
	return 0;
}
Jan 4, 2010 at 10:50am
closed account (z05DSL3A)
1 is NOT a Prime number.

A natural number is called a prime, a prime number or just prime if it has exactly two distinct natural number divisors. As 1 has only one distinct natural number divisor it is NOT prime.

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
#include <iostream>
#include <cmath>


bool isPrime (int num)
{
    if (num <=1)
        return false;
    else if (num == 2)         
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;
        double num_d = static_cast<double>(num);
        int upperLimit = static_cast<int>(sqrt(num_d) +1);
        
        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                prime = false;
            divisor +=2;
        }
        return prime;
    }
}



int main()
{
    for(int i = 0; i < 20; i++)
    {
        if (isPrime(i))
        {
            std::cout << i << " is prime." << std::endl;
        }
        else
        {
            std::cout << i << " is NOT prime." << std::endl;
        }
    }
    return 0;
}

Last edited on Jan 4, 2010 at 10:57am
Jan 4, 2010 at 12:32pm
with 1 often excluded

Not all consider 1 a prime , but just as many do because it meets with the criteria of a prime
Last edited on Jan 4, 2010 at 12:33pm
Jan 4, 2010 at 12:39pm
It does not, from Wikipedia:
In mathematics, a prime number (or a prime) is a natural number which has exactly two distinct natural number divisors: 1 and itself.
Jan 4, 2010 at 12:43pm
did you write that wikipedia entry bazzy? j/k ;) thanks for the reference
Jan 4, 2010 at 1:03pm
I guess the definition was put to rest in the 1970's from what i am reading, 1977? I was 15 at the time and HATIN ' on math. I was all about sneaking off duing lunch and engaging in some sort of nepharious act of civil disobedience or another,,,
Topic archived. No new replies allowed.