By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,739 Members | 2,014 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,739 IT Pros & Developers. It's quick & easy.

Please help my.Insorrect result.

P: 17
Please help my.My program is running but print in
"System.out.println(firstprime.findPrime ());" isn't
correct.It prin except string primenumbers some other
number for example 16 or 64(it isn't primenumbers).
I thynk that problem is in method "testIsPrime"
but I cannot find it.


/**
*PrimeNumber.java
*
*
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
//package TestPrime;
/**
*
* @author lm
*/
public class PrimeNumber{

private int numberOf = 1;
private int numberTo = 100;

public PrimeNumber() {
}

public void setOf(int number) {
numberOf = number;
}

public void setTo(int number) {
numberTo = number;
}

private boolean testIsPrime(int testprime) {
boolean prime = true;
for(int k = 3; k < testprime; k++) {//changed here
if (prime){
for(int j=2;j<=Math.sqrt(testprime);j++) {
if(testprime%k==0){
prime=false;
break;
}
}
}
}
return prime;
}

public String isPrime(int testprime) {
if ( testIsPrime(testprime)) {
return testprime+" is prime";
}else{
return testprime+" isn't prime";
}
}

public String findPrime(){
String s = "";
for(int j=numberOf;j<= numberTo;j++) {
if ( testIsPrime(j)) {
s = s + j+"is primenumber\n";
}
}
return s;

}

public static void main(String[] args) {

PrimeNumber firstprime = new PrimeNumber();

firstprime.numberOf = 10;
firstprime.numberTo = 40;
System.out.println(firstprime.findPrime ());
System.out.println(firstprime.isPrime(4272));
System.out.println(firstprime.isPrime(3517));
}
}
Dec 1 '06 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
Check again. When I run the program below it prints all the primes form 11 to 37 correctly


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. /**
  4. *PrimeNumber.java
  5. *
  6. *
  7. *
  8. * To change this template, choose Tools | Template Manager
  9. * and open the template in the editor.
  10. */
  11. //package TestPrime;
  12. /**
  13. *
  14. * @author lm
  15. */
  16. public class PrimeNumber2{
  17.  
  18. private int numberOf = 1;
  19. private int numberTo = 100; 
  20.  
  21.  
  22.  
  23. public void setOf(int number) {
  24. numberOf = number;
  25. }
  26.  
  27. public void setTo(int number) {
  28. numberTo = number;
  29. }
  30.  
  31. private boolean testIsPrime(int testprime) {
  32. boolean prime = true;
  33. for(int k = 3; k < testprime; k++) {//changed here
  34. if (prime){
  35. for(int j=2;j<=Math.sqrt(testprime);j++) {
  36. if(testprime%k==0){
  37. prime=false;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. return prime;
  44. }
  45.  
  46. public String isPrime(int testprime) {
  47. if ( testIsPrime(testprime)) {
  48. return testprime+" is prime";
  49. }else{
  50. return testprime+" isn't prime";
  51. }
  52. }
  53.  
  54. public String findPrime(){
  55. String s = "";
  56. for(int j=numberOf;j<= numberTo;j++) {
  57. if ( testIsPrime(j)) {
  58. s = s + j+"is primenumber\n";
  59. }
  60. }
  61. return s;
  62.  
  63. }
  64.  
  65. public static void main(String[] args) {
  66.  
  67. PrimeNumber2 firstprime = new PrimeNumber2();
  68.  
  69. firstprime.numberOf = 10;
  70. firstprime.numberTo = 40;
  71. System.out.println(firstprime.findPrime ());
  72. System.out.println(firstprime.isPrime(4272));
  73. System.out.println(firstprime.isPrime(3517));
  74. }
  75. }
  76.  
  77.  
  78.  
Dec 1 '06 #2

P: 17
Thant you I try it
Dec 1 '06 #3

10K+
P: 13,264
Thant you I try it
Is it working?
Dec 1 '06 #4

Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. private boolean testIsPrime(int testprime) {
  2.     boolean prime = true;
  3.     for(int k = 3; k < testprime; k++) {//changed here
  4.         if (prime) {
  5.             for(int j = 2; j <= Math.sqrt(testprime); j++) {
  6.                 if (testprime % k== 0) {
  7.                     prime=false;
  8.                     break;
  9.                 }
  10.             }
  11.         }
  12.     }
  13.     return prime;
  14. }
In your second if statement, shouldn't you be modding testprime by j, not k? i.e.

Expand|Select|Wrap|Line Numbers
  1. private boolean testIsPrime(int testprime) {
  2.     boolean prime = true;
  3.     for(int k = 3; k < testprime; k++) {//changed here
  4.         if (prime) {
  5.             for(int j = 2; j <= Math.sqrt(testprime); j++) {
  6.                 if (testprime % j == 0) {
  7.                     prime=false;
  8.                     break;
  9.                 }
  10.             }
  11.         }
  12.     }
  13.     return prime;
  14. }
Dec 1 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.