Program to find the prime(s) of 2 numbers.

Ok, so what I have here is a program where a user enters a beginning and end number, then the program uses a for loop to shoot through and checks to see if the numbers inbetween the entered two are prime, but I can not seem to get it to work, so I was looking for some help.

publicstaticvoid pnumber(){

int x, y;

Scanner console =new Scanner(System.in);

System.out.println("Please enter the beginning number;");

x = console.nextInt();

System.out.println("Please enter the ending number;");

y = console.nextInt();

int t;

for ( t = 2;t<y;t++)

if ( x % t == 0 ){

x++;

}elseif (x % t != 0){

System.out.println(x);

x++;

}

}

>

[1278 byte] By [jereNsa] at [2007-11-27 3:21:30]
# 1
What I am trying to do here is have the var(t) start at 2, and it will see if it devides into var(x) evenly, if it does, it will move on to the next X number, "x++". But if it does not devide evenly in, it prints out X. I do not see where I went wrong, yet Im still new
jereNsa at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 2

for ( t = 2;t<y;t++)// your loop should start at x, not 2

if ( x % t == 0 ){

x++;

} else if (x % t != 0) {// there's no need for the if statement here, it's the inverse of the first if

System.out.println(x);

x++;// you add one to x no matter which branch you take, so there's absolutely no point to even having the if/else.

}

}

You should start looping at x, and end at y, since you want to evaluate the numbers between those two. You're trying to find the number of primes between x and y right? If so, then t is the number that you want to check for prime. If it's prime, then increment a counter for primes. Have I understood your assignment properly?>

hunter9000a at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 3

Ok our assignment is to have a user enter 2 numbers, say 10/20. And our program is suppost to find the primes inbetween those to numbers, is this what i need?

[code] public static void pnumber(){

int x, y;

Scanner console = new Scanner(System.in);

System.out.println("Please enter the beginning number;");

x = console.nextInt();

System.out.println("Please enter the ending number;");

y = console.nextInt();

int t;

for ( t = x;t<y;t++)

if ( x % t == 0 ){

x++;

} else if {

System.out.println(x);

}

}

/code]>

jereNsa at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 4

public static void pnumber(){

int x, y;

Scanner console = new Scanner(System.in);

System.out.println("Please enter the beginning number;");

x = console.nextInt();

System.out.println("Please enter the ending number;");

y = console.nextInt();

int t;

for ( t = x;t<y;t++)

if ( x % t == 0 ){

x++;

} else {

System.out.println(x);

}

}

>

jereNsa at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 5

public class Test {

public static void main(String[] args) {

Scanner console = new Scanner(System.in);

System.out.println("Please enter the beginning number;");

int start = console.nextInt();

System.out.println("Please enter the ending number;");

int end = console.nextInt();

for (int number = start; number <= end; number++) {

if (isPrime(number)) {

System.out.println("This is a prime: " + number);

}

}

}

}

Now all you need to do is write the isPrime function...

EvilBroa at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 6

May I suggest a slightly different approach?

import java.util.Scanner;

public class PrimeFinder {

private static void printPrimesBetween(int m, int n) {

// Loop from 'm' to 'n' and check every number

// for primality. If it's prime, print it.

}

private static boolean isPrime(int n) {

// return true iff 'n' is prime

}

public static void main(String[] args) {

Scanner console = new Scanner(System.in);

System.out.println("Please enter the beginning number;");

int x = console.nextInt();

System.out.println("Please enter the ending number;");

int y = console.nextInt();

printPrimesBetween(x, y);

}

}

Edit: this was a reply to post #4 from the OP.

prometheuzza at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 7
#5, you mean the FOR loop?#6, I write where the // is?
jereNsa at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 8

> #5, you mean the FOR loop?

No, he means a method called isPrime(...) which takes an int as a parameter, and returns a boolean (if the number as the parameter is prime, just as in reply #6).

> #6, I write where the // is?

That is correct. Do you understand the logic of my comments?

prometheuzza at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 9

Well for #6, this program is already inside of a huge program, a list of assignments,

import java.io.*;

import java.util.*;

public class newprogs

{

public static void main(String[]args) throws IOException{

int num;

Scanner console = new Scanner(System.in);

System.out.println("Please enter the number of\nthe FOR Loop program you\nwant to run.");

System.out.println("(1)Payday\n(2)Finding Factorials\n(3)Diamonds\n(4)Factors\n(5)Triangles\n(6)Prime Numbers");

num = console.nextInt();

if (num == 1){

payday();

} else if (num == 2) {

ffactorials();

} else if (num == 3) {

diamonds();

} else if (num == 4) {

factors();

} else if (num == 5) {

tri();

} else if (num == 6) {

pnumber();

}

}

public static void payday(){

System.out.print("Rate of pay is $5.25 Per hour!");

for (int x=1;x<=40;x++)

System.out.printf("%-10d%-1s%-10.2f",x,"$",(x*5.25),"\n");

System.out.println("");

}

public static void ffactorials(){

int numf, fact;

Scanner console = new Scanner(System.in);

fact = 1;

System.out.println("Please enter the number you would like to have tested;");

numf = console.nextInt();

for(int x=2;x<=numf;x++)

fact = fact * x;

System.out.println(fact);

}

public static void diamonds(){

// NOT DONE!!!!

int numd;

Scanner console = new Scanner(System.in);

System.out.println("Please enter the number of diamonds;");

numd = console.nextInt();

for(int x=1;x<=numd;x+=2)

System.out.printf("%-10s", x);

for(int xx=1;xx<=numd;xx-=2)

System.out.printf("%-10s", xx);

// NOT DONE!!!!

}

public static void factors(){

int nume;

Scanner console = new Scanner(System.in);

System.out.println("Please enter the number you would like to have factored;");

nume = console.nextInt();

for (int x=1;x<=nume;x++)

if(nume%x==0)

System.out.println(x);

System.out.println("");

}

public static void tri(){

int numr;

Scanner console = new Scanner(System.in);

System.out.println("Please enter the number of rows;");

numr = console.nextInt();

}

public static void pnumber(){

int x, y;

Scanner console = new Scanner(System.in);

System.out.println("Please enter the beginning number;");

x = console.nextInt();

System.out.println("Please enter the ending number;");

y = console.nextInt();

int t;

for ( t = x;t<y;t++)

if ( x % t == 0 ){

x++;

} else {

System.out.println(x);

}

}

}

>

jereNsa at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 10

> Well for #6, this program is already inside of a huge

> program, a list of assignments,

I suggest creating different classes for those things: not your current solution: it's a mess!

Here's a start:

import java.io.*;

import java.util.*;

public class newprogs {

public static void main(String[]args) throws IOException{

int num;

Scanner console = new Scanner(System.in);

System.out.println("Please enter the number of\nthe FOR Loop program you\nwant to run.");

System.out.println("(1)Payday\n(2)Finding Factorials\n(3)Diamonds\n(4)Factors\n(5)Triangles\n(6)Prime Numbers");

num = console.nextInt();

if (num == 1){

// ...

} else if (num == 6) {

new PrimeFinder(); // <- create a new class here!

}

}

}

public class PrimeFinder {

public PrimeFinder() {

Scanner console = new Scanner(System.in);

System.out.println("Please enter the beginning number;");

int x = console.nextInt();

System.out.println("Please enter the ending number;");

int y = console.nextInt();

printPrimesBetween(x, y);

}

private void printPrimesBetween(int m, int n) {

// Loop from 'm' to 'n' and check every number

// for primality. If it's prime, print it.

}

private boolean isPrime(int n) {

// return true iff 'n' is prime

}

}

prometheuzza at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 11
See, im still new to this. So like, what you mean when you say create a new class? Just "new prime();"or?
jereNsa at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 12
> See, im still new to this. So like, what you mean> when you say create a new class? Just "new prime();" > or?Read the code I have just posted.
prometheuzza at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 13

Ok is this what you mean,

public class PrimeFinder {

public PrimeFinder() {

Scanner console = new Scanner(System.in);

System.out.println("Please enter the beginning number;");

int m = console.nextInt();

System.out.println("Please enter the ending number;");

int n = console.nextInt();

printPrimesBetween(m, n);

}

private void printPrimesBetween(int m, int n) {

for (int i=m;i<=n;i++) // i is the checker. starts with m the

// beginning number

if (m%i==0)

m++;

else

System.out.println(m);

}

private boolean isPrime(int n) {

// return true iff 'n' is prime // I have not idea what this is for

}

}

jereNsa at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 14
> Ok is this what you mean,> > I don't know: does it produce the desired output?Don't you think you should use (and implement) the isPrime(...) method?
prometheuzza at 2007-7-12 8:24:14 > top of Java-index,Java Essentials,Java Programming...
# 15
Im not quite sure what you mean
jereNsa at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 16
> Im not quite sure what you meanWhich part of my reply is not clear?
prometheuzza at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 17
Most of it, the primeFucntion, i do not know what to do with that? And the new PrimeFinder, then I make a new class, it gives me errors
jereNsa at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 18
> Most of it, the primeFucntion, i do not know what to do with that?Which part of my reply is not clear?> And the new PrimeFinder, then I make a new class, it gives me errorsYou're not being specific enough.
prometheuzza at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 19

> Most of it, the primeFucntion, i do not know what to

> do with that?

You mean you don't understand this method?

private boolean isPrime(int n) {

// return true iff 'n' is prime

}

Well, return true if 'n' is a prime number. Return false if 'n' is not a prime number.

I don't know what else to say about it...

prometheuzza at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 20

Ok i know im being kinda stubborn, but this is what I have,

public static void PrimeFinder (){

int b, e ;

Scanner console = new Scanner(System.in);

System.out.println("Please enter the number beginning number;");

b = console.nextInt();

System.out.println("Please enter the ending number;");

e = console.nextInt();

for (int x=2;x<=b;x++)

for (int y=b;y<=e;y++)

if(y%x!=0)

System.out.println(x);

}

Now, on this part.

for (int x=2;x<=b;x++)

for (int y=b;y<=e;y++)

if(y%x!=0)

System.out.println(x);

I declare 2 variables, x and y. x is the checker number, such as 2,3,4,5 so on so on. and Y is basically b(beginning number). I have it so x always stays <= y, but How do I get it to check lets say 2-9, before y jumps to 11, so then it would check 2-10. Im having a problem there, with this code I get the following output;

10

10

10

10

10

Message was edited by:

jereNs

jereNsa at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 21
> Ok i know im being kinda stubborn, ...Yes, you are.Good luck with it.
prometheuzza at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 22

for (int x=2;x<=b;x++) {// loop x from beginning number to ending number

for (int y=b;y<=e;y++) {// loop y from 2 to x

if(y%x!=0) {// check if x mod y is 0, if it is, then x is not prime.

System.out.println(x);

}

}

}

hunter9000a at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 23
Dear jereNs,could you please tell me how you would check whether 104743 is a prime number or not? So don't give code, but tell me in words what steps you would take to determine whether 104743 is prime or not...
EvilBroa at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 24

This,

for (int x=b;x<=e;x++)

for (int y=2;y<=x;y++)

if(x%y!=0)

System.out.println(x);

}

}

Got me,

10

10

10

10

10

10

11

11

11

11

11

11

11

11

11

12

12

12

12

12

12

13

13

13

13

13

13

13

13

13

13

13

14

14

14

14

14

14

14

14

14

14

15

15

15

15

15

15

15

15

15

15

15

16

16

16

16

16

16

16

16

16

16

16

17

17

17

17

17

17

17

17

17

17

17

17

17

17

17

18

18

18

18

18

18

18

18

18

18

18

18

19

19

19

19

19

19

19

19

19

19

19

19

19

19

19

19

19

20

20

20

20

20

20

20

20

20

20

20

20

20

20

jereNsa at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 25
Dear jereNs,could you please tell me how you would check whether 104743 is a prime number or not? So don't give code, but tell me in words what steps you would take to determine whether 104743 is prime or not...
EvilBroa at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 26

for (int x=b;x<=e;x++)

for (int y=2;y<=x;y++)

if(x%y!=0)

System.out.println(x);

}

}

Alright, now that the loops are fixed, try actually printing something meaningful. Think about it: if the if statement is true, that means the x value is divisible by something that isn't 1 or x. That means x is not prime! Once you enter the if block, you know that x isn't prime. Use that fact! Keep a boolean flag that tracks if the current x value is prime or not. Set it to false if you find out that x is not prime. Then, when you've checked all the y values for that x, you know it's prime if the flag is still true.

hunter9000a at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 27

> Then, when you've checked all the y values for that x,

> you know it's prime if the flag is still true.

Why would anyone continue checking when the flag is set to false? Why would anyone use a flag?

I applaud your willingness to help, but I think your method is ill-guided. You are addressing the symptom. I think the OP is better off if he learns to address the problem in a more systematic way. So (IMO) he first needs to understand the steps he needs to take to determine whether a number is a prime or not (instead of simply guessing what a prime number is which is what he seems to be doing now...)

Message was edited by:

EvilBro

EvilBroa at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...
# 28

> > Then, when you've checked all the y values for that

> x,

> > you know it's prime if the flag is still true.

> Why would anyone continue checking when the flag is

> set to false? Why would anyone use a flag?

Yes, there are much better ways to do this, as have already been explained in this thread, but I'm trying to keep it as simple as possible for the OP.

> I applaud your willingness to help, but I think your

> method is ill-guided. You are addressing the symptom.

> I think the OP is better off if he learns to address

> the problem in a more systematic way. So (IMO) he

> first needs to understand the steps he needs to take

> to determine whether a number is a prime or not

> (instead of simply guessing what a prime number is

> which is what he seems to be doing now...)

I agree that the OP is trying to go about this problem all wrong, but I'm hoping that if I help him understand the logic of the problem, maybe he'll learn something and be able to figure out the right way to do it. Maybe that's where I'm misguided. :)

hunter9000a at 2007-7-21 20:45:05 > top of Java-index,Java Essentials,Java Programming...