i do not understand this Q

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 85
Reputation: Zay is an unknown quantity at this point 
Solved Threads: 0
Zay's Avatar
Zay Zay is offline Offline
Junior Poster in Training

i do not understand this Q

 
0
  #1
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,822
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #2
27 Days Ago
Originally Posted by Zay View 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.

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 52
Reputation: tkud is an unknown quantity at this point 
Solved Threads: 12
tkud tkud is offline Offline
Junior Poster in Training

re

 
0
  #3
27 Days Ago
This should help you,zay:

  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; 27 Days Ago at 11:13 am. Reason: Show me your code and I will tell you who you are
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 85
Reputation: Zay is an unknown quantity at this point 
Solved Threads: 0
Zay's Avatar
Zay Zay is offline Offline
Junior Poster in Training
 
0
  #4
26 Days Ago
see this simple code , there is an error , but i can not solve it !


  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; 26 Days Ago at 9:02 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 328
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 59
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
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.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,822
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #6
26 Days Ago
Originally Posted by Zay View Post
see this simple code , there is an error , but i can not solve it !


  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,822
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #7
26 Days Ago
Originally Posted by Zay View Post
see this simple code , there is an error , but i can not solve it !


  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 85
Reputation: Zay is an unknown quantity at this point 
Solved Threads: 0
Zay's Avatar
Zay Zay is offline Offline
Junior Poster in Training
 
0
  #8
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,822
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #9
26 Days Ago
Originally Posted by Zay View Post
^^^^^
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:

  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.

  1. int t = (int) (ceil(sqrt((double) num)));
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 85
Reputation: Zay is an unknown quantity at this point 
Solved Threads: 0
Zay's Avatar
Zay Zay is offline Offline
Junior Poster in Training
 
0
  #10
23 Days Ago
thank U very much , it is work now
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC