# Prime Number I'd like to learn how to figure the prime numbers out

Page 1 of 1

## 10 Replies - 1698 Views - Last Post: 31 March 2009 - 11:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=96450&amp;s=b250ca203bb9e123493050f9e6c91a51&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 SovbodMark

Reputation: 0
• Posts: 7
• Joined: 31-March 09

# Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 10:29 AM

This program should accepts a postive integer n (2<n<500) and uses the Sieve of Eratosthenes to determine all prime numbers less than or equal to n.
This program take me forever finally I found your website
I hope I can learn
```public class PrimeNumbers
{

public final static int MAX = 500;

public void initialize(int[] primeArr)
{
primeArr[0] = 0;
primeArr[1] = 0;
for (int i = 2; i < primeArr.length; i++)
{
primeArr[i] = i;
}
}

public int next(int[] primeArr, int k)
{
for (int i = k; i < primeArr.length; i++)
{
if (primeArr[i] != 0)
return i;
}

return primeArr.length - 1;
}

public void getPrimes(int[] primeArr)
{

initialize(primeArr);

for (int i = 2; i < primeArr.length; i++)
{
int next = next(primeArr, i);

for (int j = next + 1; j < primeArr.length; j++)
{

if (primeArr[j] % primeArr[next] == 0)
{
primeArr[j] = 0;
}

}
}

}

public static void main(String[] args)
{
int[] primeArr = new int[MAX];

primes.getPrimes(primeArr);

for (int k = 2; k < primeArr.length; k++)
{
if (primeArr[k] != 0)
System.out.print(primeArr[k] + "  ");
}

}

}

```

This post has been edited by SovbodMark: 31 March 2009 - 10:34 AM

Is This A Good Question/Topic? 0

## Replies To: Prime Number I'd like to learn how to figure the prime numbers out

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 10:44 AM

```public static void primes(int n)
{
boolean [] a = new boolean[n+1];
for(int i = 2; i <= n; i++)
{
a[i] = true;
}

for(int i = 2; i*i <= n; i++)//crosses out the multiples of prime numbers
{
for(int j = i; j*i <= n; j++)
{
a[j*i] = false;
}
}
for(int i = 1; i < a.length; i++)
{
if(a[i])
System.out.println(i);
}
}

```

this method should do it for you

### #3 SovbodMark

Reputation: 0
• Posts: 7
• Joined: 31-March 09

## Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 10:49 AM

I really appreciate your , but where should I put this in my code and I should get a message ask the user to enter number between 2<n<500 and give all the prime number less than or == n.

mostyfriedman, on 31 Mar, 2009 - 09:44 AM, said:

```public static void primes(int n)
{
boolean [] a = new boolean[n+1];
for(int i = 2; i <= n; i++)
{
a[i] = true;
}

for(int i = 2; i*i <= n; i++)//crosses out the multiples of prime numbers
{
for(int j = i; j*i <= n; j++)
{
a[j*i] = false;
}
}
for(int i = 1; i < a.length; i++)
{
if(a[i])
System.out.println(i);
}
}

```

this method should do it for you

### #4 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:00 AM

Quote

I really appreciate your icon_up.gif , but where should I put this in my code and I should get a message ask the user to enter number between 2<n<500 and give all the prime number less than or == n.

you can do that in your main method, print the message and then call the method with 500

just make sure that you understand the code and how it works, you better trace it on a piece of paper to see what's going on

### #5 SovbodMark

Reputation: 0
• Posts: 7
• Joined: 31-March 09

## Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:00 AM

See what I did, but it still gives me an error
I don't know where these error comes from.
Can anybody do all the changes needed in my code
here is my final code
```public class PrimeNumbers
{

public final static int MAX = 500;

public void initialize(int[] primeArr)
{
primeArr[0] = 0;
primeArr[1] = 0;
for (int i = 2; i < primeArr.length; i++)
{
primeArr[i] = i;
}
}

public int next(int[] primeArr, int k)
{
for (int i = k; i < primeArr.length; i++)
{
if (primeArr[i] != 0)
return i;
}

return primeArr.length - 1;
}

public void getPrimes(int[] primeArr)
{

initialize(primeArr);

for (int i = 2; i < primeArr.length; i++)
{
int next = next(primeArr, i);

for (int j = next + 1; j < primeArr.length; j++)
{

if (primeArr[j] % primeArr[next] == 0)
{
primeArr[j] = 0;
}

}
}

}
public static void primes(int n)
{
boolean [] a = new boolean[n+1];
for(int i = 2; i <= n; i++)
{
a[i] = true;
}

for(int i = 2; i*i <= n; i++) //crosses out the multiples of prime numbers
{
for(int j = i; j*i <= n; j++)
{
a[j*i] = false;
}
}
for(int i = 1; i < a.length; i++)
{
if(a[i])
System.out.println(i);
}
}
public static void main(String [] args)
{
int number;

number = getInteger();

if(isPrime(number))
{
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " isn't a prime number.");
System.out.println("All the prime number before " + number + " is " +						   getNextPrime(number) + ".");
}
}

private static int getInteger()
{
Scanner keyboard = new Scanner(System.in);

System.out.print("Enter number between 2 and 500? ");

return keyboard.nextInt();
}

public static void main(String[] args)
{
int[] primeArr = new int[MAX];

primes.getPrimes(primeArr);

for (int k = 2; k < primeArr.length; k++)
{
if (primeArr[k] != 0)
System.out.print(primeArr[k] + "  ");
}

}

}

```

Thanks Laudies and Gentlmen

### #6 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:03 AM

you cant have 2 main methods in the same class, you have to remove one

btw you also dont need all those methods to generate prime numbers, the method i provided you is all you need

### #7 SovbodMark

Reputation: 0
• Posts: 7
• Joined: 31-March 09

## Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:14 AM

I know what you're talking about, but your gives me all the prime numbers btw 2 - 500, I need a message says hey you should enter, n number btw 2<n<500 and run all the prime numbers less or equal to that n.

### #8 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:17 AM

ah i see, then
```Scanner sc = new Scanner(System.in);
System.out.println("enter a number between 2 and 500);
int n = sc.nextInt();
System.out.println(primes(n));

```

all this in the main method

### #9 SovbodMark

Reputation: 0
• Posts: 7
• Joined: 31-March 09

## Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:29 AM

mostyfriedman, on 31 Mar, 2009 - 10:17 AM, said:

ah i see, then
```Scanner sc = new Scanner(System.in);
System.out.println("enter a number between 2 and 500);
int n = sc.nextInt();
System.out.println(primes(n));

```

all this in the main method

I know I should just use one method in class, but Which main method should it be.

### #10 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:37 AM

just keep one of them and add to it the functionality you want

### #11 SovbodMark

Reputation: 0
• Posts: 7
• Joined: 31-March 09

## Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:55 AM

It didnt like it this way, I might put it somewhere elso or change it.
what do u think?
```public class PrimeNumbers
{

public static void main(String[] args)
{
int[] primeArr = new int[MAX];

primes.getPrimes(primeArr);

for (int k = 2; k < primeArr.length; k++)
{
if (primeArr[k] != 0)
System.out.print(primeArr[k] + "  ");
}

}
Scanner sc = new Scanner(System.in);
System.out.println("enter a number between 2 and 500");
int n = sc.nextInt();
System.out.println(primes(n));

public static void primes(int n)
{
boolean [] a = new boolean[n+1];
for(int i = 2; i <= n; i++)
{
a[i] = true;
}

for(int i = 2; i*i <= n; i++)//crosses out the multiples of prime numbers
{
for(int j = i; j*i <= n; j++)
{
a[j*i] = false;
}
}
for(int i = 1; i < a.length; i++)
{
if(a[i])
System.out.println(i);
}

}
public final static int MAX = 500;

public void initialize(int[] primeArr)
{
primeArr[0] = 0;
primeArr[1] = 0;
for (int i = 2; i < primeArr.length; i++)
{
primeArr[i] = i;
}
}

public int next(int[] primeArr, int k)
{
for (int i = k; i < primeArr.length; i++)
{
if (primeArr[i] != 0)
return i;
}

return primeArr.length - 1;
}

public void getPrimes(int[] primeArr)
{

initialize(primeArr);

for (int i = 2; i < primeArr.length; i++)
{
int next = next(primeArr, i);

for (int j = next + 1; j < primeArr.length; j++)
{

if (primeArr[j] % primeArr[next] == 0)
{
primeArr[j] = 0;
}

}
}

}
}

```