| | |
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,822
Reputation:
Solved Threads: 501
0
#2 27 Days Ago
•
•
•
•
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.
•
•
Join Date: Sep 2009
Posts: 52
Reputation:
Solved Threads: 12
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; 27 Days Ago at 11:13 am. Reason: Show me your code and I will tell you who you are
0
#4 26 Days Ago
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; 26 Days Ago at 9:02 am.
0
#5 26 Days Ago
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,822
Reputation:
Solved Threads: 501
0
#6 26 Days Ago
•
•
•
•
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,822
Reputation:
Solved Threads: 501
0
#7 26 Days Ago
•
•
•
•
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,822
Reputation:
Solved Threads: 501
0
#9 26 Days Ago
•
•
•
•
^^^^^
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 |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






