Not Yet Answered # prime numbers

Taywin 297 apines 116 Im trying to bind my custom vertical scrollbar to datagridview is this possible.. Please help Thnak you..

0

for(int j = 2; j <= i; j++) should be

for(int j = 2; j <i; j++)

or you will keep getting a number which is divisible by itself.

Also...

else {

System.out.println(i + " ");

}

will keep printing 'i' even though the number is NOT a prime number. You need to keep a flag checking if the number is or is not a prime number in the 2nd loop (j). Then if the flag indicates that it is still a prime number, you can display the message; otherwise, don't.

0

`package cls; public class main34 { public static void main(String[] args) { for(int i = 2; i <= 100; i++) { for(int j = 2; j <= i; j++) { if(i % j == 0) { break; } else { System.out.println(i + " "); } } } } }`

What am I doing wrong here ?

I think that you are trying to use something that is similar to the Sieve of Eratosthenes I think you can improve your algorithm's complexity:

```
public static void findPrimeNumbers(int bound)
{
int sqUB = (int)Math.sqrt(bound); //We don't need to search further then the sqrt.
boolean[] isNotPrime = new boolean[bound + 1];
for (int i = 2; i <= sqUB; ++i)
{
if(!isNotPrime[i]) //if we reached a number that has false, it must be a prime number
{
System.out.print(i + " ");
for (int j = i * i; j <= bound; j += i)
{
isNotPrime[j] = true; // go over all the multiplications of this number, and mark them as not prime.
}
}
}
// The isNotPrime array now contains all the prime numbers, simply print them.
for (int i = sqUB; i <= bound; i++)
{
if(!isNotPrime[i])
{
System.out.print(i + " ");
}
}
}
```

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

I'm trying to figure out how do i print a distinct enum value by using a conditional statement in my construction of array.

I have a enum declaration

`enum Animal {Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig};`

And i construct my array using a voidptr ...

I'm new to python, for my project I need to send mail from python.. I'm getting the following error can someone please help me how to get out of it..

```
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for ...
```