| | |
i do not understand this Q
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
hello ;
here is a quastion . i understand it until the red senence , i do not know what dose it mean mathematically ?
Write a program that prompts the user to input a positive integer. It should then output indicating whether the number is a prime.
Note: an even number is a prime if it is 2. And an odd integer is prime if it is not divisible by any odd integer less than or equal to its square root.
here is a quastion . i understand it until the red senence , i do not know what dose it mean mathematically ?
Write a program that prompts the user to input a positive integer. It should then output indicating whether the number is a prime.
Note: an even number is a prime if it is 2. And an odd integer is prime if it is not divisible by any odd integer less than or equal to its square root.
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
0
#2 Nov 8th, 2009
•
•
•
•
hello ;
here is a quastion . i understand it until the red senence , i do not know what dose it mean mathematically ?
Write a program that prompts the user to input a positive integer. It should then output indicating whether the number is a prime.
Note: an even number is a prime if it is 2. And an odd integer is prime if it is not divisible by any odd integer less than or equal to its square root.
Mathematically, any non-prime number will have a factor less than or equal to its square root. That means that if you have established that a number has no factor less than or equal to its square root, you have established that it is a prime number. Take 103 for example. The square root is about 10.15. Since factors must be integers, round down to 10. So to show that 103 is prime, you show that it has no factors less than or equal to 10. Further, you only have to test prime numbers. Hence, prove that 2, 3, 5, and 7 are not factors and you have proven that 103 is not prime. Thus you don't need to bother testing 4, 6, 8, and 10 since they are multiples of 2. You also don't need to test 9 since it's a multiple of 3.
This should help you,zay:
Please, if this helped you , mark this thread as solved.
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int u=0,x=0; int main() { int n=0; cout<<"Enter a number..."; cin>>n; if(n==0|n==1){ cout<<"You entered a 0 or a 1. You know that 0 is not a prime and 1 is a prime!!"<<endl; return 0; } if(n==2){ cout<<"This is the only even prime number!!"<<endl; return 0; } if(n%2==0){ cout<<"The number entered is not a prime number!!"<<endl; return 0; } while(u<n*n){ if(u%2!=0){ if(n%u!=0) x=1; } else{ x=0; } u++; } if(x==1) cout<<"The number entered is not a prime number!!"<<endl; else cout<<"This is a prime number!!"<<endl; return 0; }
Please, if this helped you , mark this thread as solved.
Last edited by tkud; Nov 8th, 2009 at 11:13 am. Reason: Show me your code and I will tell you who you are
0
#4 Nov 9th, 2009
see this simple code , there is an error , but i can not solve it !
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<iomanip> #include<string> #include<cmath> using namespace std; void main() { int num, i=1; cout<<"Enter num:"; cin>>num; if (num==1||num==2) cout<<"Prime , you enter 1 or 2"; else if (num%2==0) cout<<"Not prime"; else {int t=sqrt(num); while (i<=t) { if (num%i==0) cout<<"Not prime";break; else i++; } } }
Last edited by Zay; Nov 9th, 2009 at 9:02 am.
0
#5 Nov 9th, 2009
You might want to take a look at this thread:
http://www.daniweb.com/forums/thread230230.html
The first few posts are irrelevant, but the later posts are probably more or less exactly what you're looking for!
Cheers for now,
Jas.
http://www.daniweb.com/forums/thread230230.html
The first few posts are irrelevant, but the later posts are probably more or less exactly what you're looking for!
Cheers for now,
Jas.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Those who understand binary .....
And those who don't!
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
0
#6 Nov 9th, 2009
•
•
•
•
see this simple code , there is an error , but i can not solve it !
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<iomanip> #include<string> #include<cmath> using namespace std; void main() { int num, i=1; cout<<"Enter num:"; cin>>num; if (num==1||num==2) cout<<"Prime , you enter 1 or 2"; else if (num%2==0) cout<<"Not prime"; else {int t=sqrt(num); while (i<=t) { if (num%i==0) cout<<"Not prime";break; else i++; } } }
The formatting/indentation makes this code very hard to read. Compare with this:
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<iomanip> #include<string> #include<cmath> using namespace std; void main() { int num, i=1; cout<<"Enter num:"; cin>>num; if (num==1||num==2) { cout<<"Prime , you enter 1 or 2"; } else if (num%2==0) { cout<<"Not prime"; } else { int t=sqrt(num); while (i<=t) { if (num%i==0) cout<<"Not prime"; break; else i++; } } }
Notice that there's no cout statement of "prime" except for 1 and 2 (1 is NOT prime). How do you know whether 11 is prime? There's no display of "prime" when all tests are passed.
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
0
#7 Nov 9th, 2009
•
•
•
•
see this simple code , there is an error , but i can not solve it !
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<iomanip> #include<string> #include<cmath> using namespace std; void main() { int num, i=1; cout<<"Enter num:"; cin>>num; if (num==1||num==2) cout<<"Prime , you enter 1 or 2"; else if (num%2==0) cout<<"Not prime"; else {int t=sqrt(num); while (i<=t) { if (num%i==0) cout<<"Not prime";break; else i++; } } }
The formatting/indentation makes this code very hard to read. Compare with this:
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<iomanip> #include<string> #include<cmath> using namespace std; void main() { int num, i=1; cout<<"Enter num:"; cin>>num; if (num==1||num==2) { cout<<"Prime , you enter 1 or 2"; } else if (num%2==0) { cout<<"Not prime"; } else { int t=sqrt(num); while (i<=t) { if (num%i==0) cout<<"Not prime"; break; else i++; } } }
Look closely at line 32 It now stands out more clearly. Brackets?
On another note, notice that there's no cout statement of "prime" except for 1 and 2 (1 is NOT prime). How do you know whether 11 is prime? There's no display of "prime" when all tests are passed successfully.
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
0
#9 Nov 9th, 2009
•
•
•
•
^^^^^
ok , i will bt such statement for printing the primes ,
but it still has an error saying :
'sqrt' : ambiguous call to overloaded function
I don't get that error. I get a conversion warning that went away when I did this:
C++ Syntax (Toggle Plain Text)
int t = (int) (ceil(sqrt(num)));
http://www.cplusplus.com/reference/clibrary/cmath/sqrt/
It might possibly not like the fact that num is an integer. I suppose if you still have a problem, typecast it to a double.
C++ Syntax (Toggle Plain Text)
int t = (int) (ceil(sqrt((double) num)));
![]() |
Similar Threads
- Code for Call the program in Visual Basic (Visual Basic 4 / 5 / 6)
- Help me understand function in c++ (C++)
- Help me understand Pointers (C++)
- help me understand end of do while loop (C++)
- file sharing. help!!! dont understand!! (Networking Hardware Configuration)
- don't understand (C++)
- Creating a locking program (C)
Other Threads in the C++ Forum
- Previous Thread: Programming Google - expressive, concurrent and garbage-collected
- Next Thread: inheritance and composition
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






