473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Please help my.Insorrect result.

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
4 1136
r035198x
13,262 8TB
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
Thant you I try it
Dec 1 '06 #3
r035198x
13,262 8TB
Thant you I try it
Is it working?
Dec 1 '06 #4
Ganon11
3,652 Expert 2GB
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

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

Similar topics

6
by: Kurt A. Kaylor | last post by:
Hey, I am trying to get some code I have written to work. Runs well until I make a request. The I get some problems with PHP related to an SQL statement. Here are the errors :Warning:...
2
by: andrew browning | last post by:
if i write a function to find the max value of an array and want to write it recursively, how do i decide on the base case? i want to say: if (index 0 is the only index then it is max)...
4
by: Mel | last post by:
I have: <head><title>JUNK</title></head><body >HELLO</body> i need to strip off all tags <> and end up with the content "HELLO". can someone show me how ? thanks a whole bunch
0
by: Control Freq | last post by:
Dear all, Below is my first C# program. Please be gentle with me. I was asked to produce a simple Windows Forms program to save Key/Value pairs, along with the a couple of sorting choices. ...
4
by: rajesh619 | last post by:
I'm new to programming. I have created a servlet which retrieves values from the database after a value is put into the HTML page to which it is attached. But during compilation, it shows two errors....
3
by: shror | last post by:
I was testing to post a form and echo message according to the submitted box using else if and then mail() the box result to myself just for testing the procedure but i got some problems: 1# the...
3
by: Alami | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
2
by: pargat.singh | last post by:
Hi Everyone: Please help as i need to write some rounding function in C#. i Wrote one but does not give me correct result. Can some one please correct me C# Codes ...
16
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.