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?
#include <iostream>
#include <cmath>
usingnamespace 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;
elseif((number % 2) == 0)
cout<<number<<" is not a prime number."<<endl;
elseif((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;
}
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
elseif((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;
}
}
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.:
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
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.
#include <iostream>
usingnamespace 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;
}
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.
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,,,