944,066 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 836
  • C++ RSS
Nov 8th, 2009
0

i do not understand this Q

Expand Post »
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.
Similar Threads
Zay
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
Zay is offline Offline
92 posts
since Apr 2007
Nov 8th, 2009
0
Re: i do not understand this Q
Click to Expand / Collapse  Quote originally posted by Zay ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 8th, 2009
0

re

This should help you,zay:

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3. int u=0,x=0;
  4. int main()
  5. {
  6. int n=0;
  7. cout<<"Enter a number...";
  8. cin>>n;
  9. if(n==0|n==1){
  10. cout<<"You entered a 0 or a 1. You know that 0 is not a prime and 1 is a prime!!"<<endl;
  11. return 0;
  12. }
  13. if(n==2){
  14. cout<<"This is the only even prime number!!"<<endl;
  15. return 0;
  16. }
  17. if(n%2==0){
  18. cout<<"The number entered is not a prime number!!"<<endl;
  19. return 0;
  20. }
  21. while(u<n*n){
  22. if(u%2!=0){
  23. if(n%u!=0)
  24. x=1;
  25. }
  26. else{
  27. x=0;
  28. }
  29. u++;
  30. }
  31. if(x==1)
  32. cout<<"The number entered is not a prime number!!"<<endl;
  33. else
  34. cout<<"This is a prime number!!"<<endl;
  35. return 0;
  36. }

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
Reputation Points: 13
Solved Threads: 46
Posting Whiz in Training
tkud is offline Offline
235 posts
since Sep 2009
Nov 9th, 2009
0
Re: i do not understand this Q
see this simple code , there is an error , but i can not solve it !


C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. #include<iomanip>
  4. #include<string>
  5. #include<cmath>
  6. using namespace std;
  7. void main()
  8. {
  9. int num, i=1;
  10. cout<<"Enter num:";
  11. cin>>num;
  12. if (num==1||num==2)
  13. cout<<"Prime , you enter 1 or 2";
  14. else if (num%2==0)
  15. cout<<"Not prime";
  16. else
  17. {int t=sqrt(num);
  18. while (i<=t)
  19. { if (num%i==0)
  20. cout<<"Not prime";break;
  21. else
  22. i++;
  23. }
  24. }
  25.  
  26.  
  27.  
  28. }
Last edited by Zay; Nov 9th, 2009 at 9:02 am.
Zay
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
Zay is offline Offline
92 posts
since Apr 2007
Nov 9th, 2009
0
Re: i do not understand this Q
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.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Nov 9th, 2009
0
Re: i do not understand this Q
Click to Expand / Collapse  Quote originally posted by Zay ...
see this simple code , there is an error , but i can not solve it !


C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. #include<iomanip>
  4. #include<string>
  5. #include<cmath>
  6. using namespace std;
  7. void main()
  8. {
  9. int num, i=1;
  10. cout<<"Enter num:";
  11. cin>>num;
  12. if (num==1||num==2)
  13. cout<<"Prime , you enter 1 or 2";
  14. else if (num%2==0)
  15. cout<<"Not prime";
  16. else
  17. {int t=sqrt(num);
  18. while (i<=t)
  19. { if (num%i==0)
  20. cout<<"Not prime";break;
  21. else
  22. i++;
  23. }
  24. }
  25.  
  26.  
  27.  
  28. }

The formatting/indentation makes this code very hard to read. Compare with this:

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. #include<iomanip>
  4. #include<string>
  5. #include<cmath>
  6. using namespace std;
  7.  
  8.  
  9. void main()
  10. {
  11. int num, i=1;
  12.  
  13. cout<<"Enter num:";
  14. cin>>num;
  15.  
  16. if (num==1||num==2)
  17. {
  18. cout<<"Prime , you enter 1 or 2";
  19. }
  20. else if (num%2==0)
  21. {
  22. cout<<"Not prime";
  23. }
  24. else
  25. {
  26. int t=sqrt(num);
  27.  
  28. while (i<=t)
  29. {
  30. if (num%i==0)
  31. cout<<"Not prime";
  32. break;
  33. else
  34. i++;
  35. }
  36. }
  37. }

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 9th, 2009
0
Re: i do not understand this Q
Click to Expand / Collapse  Quote originally posted by Zay ...
see this simple code , there is an error , but i can not solve it !


C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. #include<iomanip>
  4. #include<string>
  5. #include<cmath>
  6. using namespace std;
  7. void main()
  8. {
  9. int num, i=1;
  10. cout<<"Enter num:";
  11. cin>>num;
  12. if (num==1||num==2)
  13. cout<<"Prime , you enter 1 or 2";
  14. else if (num%2==0)
  15. cout<<"Not prime";
  16. else
  17. {int t=sqrt(num);
  18. while (i<=t)
  19. { if (num%i==0)
  20. cout<<"Not prime";break;
  21. else
  22. i++;
  23. }
  24. }
  25.  
  26.  
  27.  
  28. }

The formatting/indentation makes this code very hard to read. Compare with this:

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. #include<iomanip>
  4. #include<string>
  5. #include<cmath>
  6. using namespace std;
  7.  
  8.  
  9. void main()
  10. {
  11. int num, i=1;
  12.  
  13. cout<<"Enter num:";
  14. cin>>num;
  15.  
  16. if (num==1||num==2)
  17. {
  18. cout<<"Prime , you enter 1 or 2";
  19. }
  20. else if (num%2==0)
  21. {
  22. cout<<"Not prime";
  23. }
  24. else
  25. {
  26. int t=sqrt(num);
  27.  
  28. while (i<=t)
  29. {
  30. if (num%i==0)
  31. cout<<"Not prime";
  32. break;
  33. else
  34. i++;
  35. }
  36. }
  37. }

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 9th, 2009
0
Re: i do not understand this Q
^^^^^
ok , i will bt such statement for printing the primes ,
but it still has an error saying :
'sqrt' : ambiguous call to overloaded function
Zay
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
Zay is offline Offline
92 posts
since Apr 2007
Nov 9th, 2009
0
Re: i do not understand this Q
Click to Expand / Collapse  Quote originally posted by Zay ...
^^^^^
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)
  1. 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)
  1. int t = (int) (ceil(sqrt((double) num)));
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 12th, 2009
0
Re: i do not understand this Q
thank U very much , it is work now
Zay
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
Zay is offline Offline
92 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Programming Google - expressive, concurrent and garbage-collected
Next Thread in C++ Forum Timeline: inheritance and composition





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC