473,320 Members | 1,802 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,320 software developers and data experts.

Wrong result

When I debug this isn’t mistakes.
When I start it this is one mistake.
Give me somebody advises please

public static void main(String[] args) {
int limit=1000;
boolean []prime=new boolean[limit+1];
prime[2]=true;

for(int i=2;i<=limit;i++)
{

prime=true;


if(i<2||(i%2==0))
prime=false;

for(int j=2;j<=limit-1;j++){

if(i%j==0)

prime=false;
break;
}

if(prime==true)

System.out.println(i+"isPrime");
else
System.out.println(i+"isn'tPrime");
}
}
}
Nov 6 '06 #1
4 1035
horace1
1,510 Expert 1GB
prime is an array so you need to specify the element you are addressing, e.g. to access element i
prime[i]=true;

code now looks like
Expand|Select|Wrap|Line Numbers
  1. public class t{
  2.  
  3. public static void main(String[] args) {
  4. int limit=1000;
  5. boolean []prime=new boolean[limit+1];
  6. prime[2]=true;
  7.  
  8. for(int i=2;i<=limit;i++)
  9. {
  10.  
  11. prime[i]=true;        // ** needs index [i]
  12.  
  13.  
  14. if(i<2||(i%2==0))
  15. prime[i]=false;
  16.  
  17. for(int j=2;j<=limit-1;j++){
  18.  
  19. if(i%j==0)
  20.  
  21. prime[j]=false;
  22. break;
  23. }
  24.  
  25. if(prime[i]==true)
  26.  
  27. System.out.println(i+"isPrime");
  28. else
  29. System.out.println(i+"isn'tPrime");
  30. }
  31. }
  32. }
  33.  
Nov 6 '06 #2
Thank for your advice.
Nov 6 '06 #3
r035198x
13,262 8TB
There are some things in your codes that I find not neccessary. Compare for example with this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class t{
  3.  public static void main(String[] args) {
  4.   int limit = 1000;
  5.   boolean []prime=new boolean[limit+1];
  6.   for(int i=2;i<=limit;i++)
  7.   {
  8.    prime[i]=true;
  9.    for(int j=2;j<i;j++){
  10.     if(i%j==0)
  11.     prime[i]=false;
  12.     continue;
  13.    }
  14.    if(prime[i]==true)
  15.     System.out.println(i+" isPrime**");
  16.    else
  17.     System.out.println(i+" isn'tPrime");
  18.   }
  19.  }
  20. }
  21.  
Nov 6 '06 #4
Thank for your advice very much.
Nov 6 '06 #5

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

Similar topics

9
by: Bartosz Wegrzyn | last post by:
I need help with sessions. I createt set of web site for nav with authorization. first I go into main.php which looks like this: <?php //common functions include_once '../login/common.php';...
5
by: Krisnamourt Correia via SQLMonster.com | last post by:
I have one query that executes many times in a week. I created one Maintenances plan that Rebuild all index in my Database that has been executed at 23:40 Saturday until stop finished at Sunday. ...
2
by: Abby | last post by:
I need to do 8 bits 2's complement checksum, so I wrote code in order to see if the checksum calculation is correct. ===========================================================================...
10
by: andrew browning | last post by:
i have overlaoded all of my arithmetic operators but all are functioning as multiplication. below is a sample of the addition operator: Rational operator + (const Rational& r1, const Rational&...
3
by: Peter | last post by:
Hello all, I have the following t-sql batch: create procedure stp_test ( @p_date1 as datetime = null, @p_date2 as datetime = null )
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
7
by: fl | last post by:
Hi, I am learning C++ with C++ primer written by Lippman. The first output line: cout << s1 << endl; works right. The second is wrong. I find the problem is that s1 is destroyed in the call from...
27
by: Dave | last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU compilers. I then decided to try to use Sun's compiler. The Sun Studio 12 compiler reports the following code, which is in...
4
by: hirsh.dan | last post by:
i have a functions that writes information to a file. inside that function i have a line in which i call another function. if this line is executed, nothing is written to the file, but if i remark...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.