# Prime numbers

• (2 Pages)
• 1
• 2

## 16 Replies - 1039 Views - Last Post: 17 October 2014 - 08:18 PMRate 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=356008&amp;s=6141667554cb40c282c4f411f3c4b8dd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Khaly_21

Reputation: 0
• Posts: 8
• Joined: 17-October 14

# Prime numbers

Posted 17 October 2014 - 06:30 PM

Need help, I'm so obviously lost,

Program is to list all prime numbers between two entered numbers.

```import java.util.Scanner;
public class question6 {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter lower int:");
int x = input.nextInt();
System.out.println("Enter larger int:");
int y = input.nextInt();

boolean prime = false ;
int i = x;

for ( i = x; i <= y; i++) {  // tested works returns 2
if (i == 2) {
prime = false;
System.out.println( i + " Prime");

}

for (i = x; i <= y; i++){	   // tested works returns anything divisible by two and not two itself.
if (i % 2 == 0) {
prime = true;

}
for (int j = x; j<=y; j++) {
if ((i % j) == 0) {
prime = true;

}
}

/*while (i <= y)  {
int j = i;
if (((Math.sqrt(i)) %j) == 0){
System.out.println(i + " Not Prime"); }
pFlag = true;
j++;

} */

if (!prime)
System.out.println(i);

```

Is This A Good Question/Topic? 0

## Replies To: Prime numbers

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: Prime numbers

Posted 17 October 2014 - 06:32 PM

What specific problems or errors are you encountering?

### #3 Khaly_21

Reputation: 0
• Posts: 8
• Joined: 17-October 14

## Re: Prime numbers

Posted 17 October 2014 - 06:35 PM

Nothing actually seems to be working, I'm not even sure anymore,
At first i had it print out under each for statement, and the only one that worked was the even numbers
I had a long list of even numbers, I wanted to pull all the numbers that were not prime, and then print out the other numbers.

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: Prime numbers

Posted 17 October 2014 - 06:39 PM

```for(int i = 2; i <= n; i++){
if(isPrime(i)){
//print i
}
}

```

This isn't too hard to understand, is it? So how do you check if an individual number is prime?

Based on this, I would restructure your program like so:
```public class YourClass{

public static void main(String[] args){

for(int i = 2; i <= n; i++){
if(isPrime(i)){
//print i
}
}
}

public static boolean isPrime(int x){
//test if x is prime
//return true iff x is prime
}
}

```

### #5 Khaly_21

Reputation: 0
• Posts: 8
• Joined: 17-October 14

## Re: Prime numbers

Posted 17 October 2014 - 06:45 PM

Sorry, I don't quite understand.... Maybe I've been at it to long, Sad thing is I only have one hour to hand it in.... Lord help me....

So what I should do is make the boolean flag true only if it is prime ?

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: Prime numbers

Posted 17 October 2014 - 06:46 PM

What is the definition of a prime number? Its only divisors are 1 and itself. Really, you only have to check if it has a divisor from to to sqrt(n) (assuming n > 2).

### #7 Khaly_21

Reputation: 0
• Posts: 8
• Joined: 17-October 14

## Re: Prime numbers

Posted 17 October 2014 - 06:48 PM

I think the biggest problem I'm having has to do with the prime algorithm, I've tried to do it a few different ways and none seem to work.

```for (i = 2; i <= n; i ++) // In this case what is n?

```

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: Prime numbers

Posted 17 October 2014 - 06:49 PM

Quote

Program is to list all prime numbers between two entered numbers.

In this case, I am saying your lower bound is 2 and your upper bound is n. Of course, if you can get it for this specific case, it should be easy to modify.

I'm trying to get you to break down the problem some. I've given you some structure to do it. So let's answer the most important question first. If I give you an arbitrary integer, how do you determine if it is prime? Let's focus on the case of a single integer, which is what we care about for the isPrime() method.

### #9 Khaly_21

Reputation: 0
• Posts: 8
• Joined: 17-October 14

## Re: Prime numbers

Posted 17 October 2014 - 06:56 PM

First I'm going to say Thank-you for being patient with me....

So in my case i would be my x value entered, and n would be my y value entered
So i need Math.sqrt(i) (I had this in my while statement) and divisible by the count ?

Lets say the number is 21, I would divide it by 2, then by 3, to find that it is divisible by 7
if it was 25 i would do the same all the way up to 5.

### #10 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: Prime numbers

Posted 17 October 2014 - 06:58 PM

Focus only on this method right now:
```public static boolean isPrime(int x){

}

```

Yes- you need the square root of x. Now how do you check if each integer in the range [2, sqrt{x}] is a divisor of x? This is something you should be able to take from here.

### #11 Khaly_21

Reputation: 0
• Posts: 8
• Joined: 17-October 14

## Re: Prime numbers

Posted 17 October 2014 - 07:03 PM

Something like ....

for (j=2; j<=y; j++);
if (Math.sqrt(i)/j == 0){ (isPrime = true);
}

???

### #12 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: Prime numbers

Posted 17 October 2014 - 07:06 PM

No. The divisor operator does this with ints: given 5/2, it returns 2. You want the modulus operator: 5 % 2 == 1 (the integer remainder of 5/2).

You also are not focusing on the isPrime() method. Seriously- write code just for the isPrime() method for now. You aren't using the Math.sqrt() method either. Use the template I gave you and test your code.

Also, if you find a divisor of the input number other than 1 or the number itself, you want to indicate that the number isn't prime. So just return false and be done.

I'm happy to point you in the right direction, but you still need to play around with the code, test it, fix it, and try again.

### #13 Khaly_21

Reputation: 0
• Posts: 8
• Joined: 17-October 14

## Re: Prime numbers

Posted 17 October 2014 - 07:08 PM

```import java.util.Scanner;
public class question6 {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter lower int:");
int x = input.nextInt();
System.out.println("Enter larger int:");
int y = input.nextInt();

boolean isPrime = false;

for (int i = x; i <=y; i++) {
if Math.pow(x)/i == 0;
isPrime = true;
```

I'm going to try to figure this out for the next 10 minutes, do you stick around here, I think i'm rushing in case I loose the help that I'm getting. I'm sorry, I'm usually not this dense.

### #14 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: Prime numbers

Posted 17 October 2014 - 07:09 PM

I offered a lot of advice that would make your life a lot easier and you've flat out ignored it. I encourage you to take the time to read my posts, use the template provided, and take my advice.

### #15 Khaly_21

Reputation: 0
• Posts: 8
• Joined: 17-October 14

## Re: Prime numbers

Posted 17 October 2014 - 07:23 PM

Sorry we haven't covered methods yet, I just realized what "public static boolean" is by looking through my textbook.