looping problem

Hey all,

i am trying to sort out the numbers which are not prime number from 1 to 100 and just don't get the right output. I guess I need to know why I get the output by the code , both posted below. It looks like every x greater than 3 can pass the condition test in if statement . Thanks for the help.

code:

public class Number {

static String a;

public static void main(String args[ ]) {

for (int x=2;x<101;x++){

for (int y=2;y<100;y++){

if((y<x)&&(x%y)==0){

a="not prime ";

}

}

System.out.println(x+" "+a);

}

}

}

output

2 null

3 null

4 not prime

5 not prime

6 not prime

7 not prime

8 not prime

9 not prime

10 not prime

11 not prime

12 not prime

13 not prime

14 not prime

15 not prime

16 not prime

17 not prime

18 not prime

19 not prime

20 not prime

21 not prime

22 not prime

23 not prime

24 not prime

25 not prime

26 not prime

27 not prime

28 not prime

29 not prime

30 not prime

31 not prime

32 not prime

33 not prime

34 not prime

35 not prime

36 not prime

37 not prime

38 not prime

39 not prime

40 not prime

41 not prime

42 not prime

43 not prime

44 not prime

45 not prime

46 not prime

47 not prime

48 not prime

49 not prime

50 not prime

51 not prime

52 not prime

53 not prime

54 not prime

55 not prime

56 not prime

57 not prime

58 not prime

59 not prime

60 not prime

61 not prime

62 not prime

63 not prime

64 not prime

65 not prime

66 not prime

67 not prime

68 not prime

69 not prime

70 not prime

71 not prime

72 not prime

73 not prime

74 not prime

75 not prime

76 not prime

77 not prime

78 not prime

79 not prime

80 not prime

81 not prime

82 not prime

83 not prime

84 not prime

85 not prime

86 not prime

87 not prime

88 not prime

89 not prime

90 not prime

91 not prime

92 not prime

93 not prime

94 not prime

95 not prime

96 not prime

97 not prime

98 not prime

99 not prime

100 not prime>

[2517 byte] By [kevinchanga] at [2007-11-27 5:55:31]
# 1
Hmmm... do you ever assign anything to a besides "not prime"? Are you surprised it never writes "is prime"?Hmmm... I wonder if Hunter9000 will post something similar...Message was edited by: Hippolyte
Hippolytea at 2007-7-12 16:24:26 > top of Java-index,Java Essentials,Java Programming...
# 2
You never reset a to anything other than "not prime" when you change x values, so that's all that ever gets printed.
hunter9000a at 2007-7-12 16:24:26 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks both of you for the replies.

Back to my last post, I still don't understand why some of x values that are prime number can match the condition set for if statement and make the computer execute this if statement. Any idea?

I am not sure what exactly is the another thing supposed to be assigned to a for solving this looping problem. My try(posted below) did not wok it out.

Thanks again.

public class Number {

static String a;

public static void main(String args[ ]) {

for (int x=2;x<101;x++){

for (int y=2;y<100;y++){

if((y<x)&&(x%y)==0){

a="not prime";

}

else {a="is prime";}

}

System.out.println(x+" "+a);

}

}

}

--

Output

:2 is prime

3 is prime

4 is prime

5 is prime

6 is prime

7 is prime

8 is prime

9 is prime

10 is prime

11 is prime

12 is prime

13 is prime

14 is prime

15 is prime

16 is prime

17 is prime

18 is prime

19 is prime

20 is prime

21 is prime

22 is prime

23 is prime

24 is prime

25 is prime

26 is prime

27 is prime

28 is prime

29 is prime

30 is prime

31 is prime

32 is prime

33 is prime

34 is prime

35 is prime

36 is prime

37 is prime

38 is prime

39 is prime

40 is prime

41 is prime

42 is prime

43 is prime

44 is prime

45 is prime

46 is prime

47 is prime

48 is prime

49 is prime

50 is prime

51 is prime

52 is prime

53 is prime

54 is prime

55 is prime

56 is prime

57 is prime

58 is prime

59 is prime

60 is prime

61 is prime

62 is prime

63 is prime

64 is prime

65 is prime

66 is prime

67 is prime

68 is prime

69 is prime

70 is prime

71 is prime

72 is prime

73 is prime

74 is prime

75 is prime

76 is prime

77 is prime

78 is prime

79 is prime

80 is prime

81 is prime

82 is prime

83 is prime

84 is prime

85 is prime

86 is prime

87 is prime

88 is prime

89 is prime

90 is prime

91 is prime

92 is prime

93 is prime

94 is prime

95 is prime

96 is prime

97 is prime

98 is prime

99 is prime

100 is prime>

kevinchanga at 2007-7-12 16:24:26 > top of Java-index,Java Essentials,Java Programming...
# 4

Did someone delete the other 1,000+ forum threads on finding

the prime numbers between 1 and 100, lol? ; )

Do a forum search.

http://forum.java.sun.com/thread.jspa?forumID=54&threadID=653503

http://forum.java.sun.com/thread.jspa?forumID=31&threadID=788706

http://forum.java.sun.com/thread.jspa?forumID=31&threadID=613411

http://forum.java.sun.com/thread.jspa?forumID=256&threadID=260595

http://forum.java.sun.com/thread.jspa?forumID=513&threadID=566203

http://forum.java.sun.com/thread.jspa?forumID=54&threadID=592457

http://forum.java.sun.com/thread.jspa?forumID=54&threadID=671365

TuringPesta at 2007-7-12 16:24:26 > top of Java-index,Java Essentials,Java Programming...
# 5

for (int x=2;x<101;x++){// x is the value you want to check for prime

for (int y=2;y< x ;y++){// y is used to check x for primeosity (primeitude?)

// loop y from 2 to x, and you don't need to do the y<x check in the if statement.

if(( x%y == 0) {// as you loop from 2 to x, if any y value divides x evenly, then you know x is not prime.

a="not prime";

}

/* else {// don't do this here, if you do, then only the last y value you check is used to determine prime or not

a="is prime";

}

*/

}

// if you get to the end of the y loop without setting a to "not prime", then it will be "prime". Either way, print it out, then reset it for the next loop.

System.out.println(x+" "+a);

a = "prime";// use "prime" as the default value since you're assuming it is prime going into the loop. This only gets changed if the x value if found to be not prime.

}

I hope that all made sense :)>

hunter9000a at 2007-7-12 16:24:26 > top of Java-index,Java Essentials,Java Programming...
# 6

Note:

(1) For loop variables have meaningful names

(2) Checking If(1, 2, EVEN) conditions to simplify algorithm

(3) Divisor loop only has to check up to SQRT(number)

(4) Divisor loop can increment by 2 because number is odd

(5) used break to exit for loop when necessary

public class PrimeNumberFinder{

public static void main(String[] args){

new PrimeNumberFinder();

}

public PrimeNumberFinder(){

for(int number = 1; number <= 20; number++){

boolean isPrime = true;

// 1 is ignored

if(number == 1){

System.out.println("1 is ignored");

continue;

} else

// check if even and not 2

if(number % 2 == 0 && number > 2){

isPrime = false;

} else {

int limit = (int)Math.sqrt((double)number);

for(int divisor = 3; divisor <= limit; divisor += 2){

if((number % divisor) == 0){

isPrime = false;

break;

}

}

}

System.out.println("" + number + (isPrime ? " is PRIME" : " is COMPOSITE"));

}

}

}

TuringPesta at 2007-7-12 16:24:26 > top of Java-index,Java Essentials,Java Programming...
# 7

The output u r getting is not correct. 5 is prime but ur output is otherwise

For 2,3 u r getting null since value of a is not assigned (the if condn can't be met).For values 4,5 ... the if condition becomes true at some point of looping and since a is static so u are getting not prime with rest

of the numbers till 100.My advice is

use for (int y=2;y<x;y++) in inner loop and use (x%y) only to check for prime>

javaj2eeguya at 2007-7-12 16:24:26 > top of Java-index,Java Essentials,Java Programming...
# 8

My suggestion to the OP is that he should use more structure.

public class Number {

public static void main(String args[ ]) {

for (int x = 2; x < 101; x++) {

if (isPrime(x)) {

System.out.println("The number " + x + " is a prime number.");

} else {

System.out.println("The number " + x + " is not a prime number.");

}

}

}

private static boolean isPrime(int n) {

if (n < 2) {

return false;

}

if (n == 2) {

return true;

}

for (int p = 2; p < n; p++) {

if (n % p == 0) {

// the number has a proper divisor, so it cannot be a prime.

// no point in checking the rest.

return false;

}

}

// The number had no proper divisors, so it is prime.

return true;

}

}

EvilBroa at 2007-7-12 16:24:26 > top of Java-index,Java Essentials,Java Programming...
# 9

That's it! Running on Siemens TC65 (J2ME), to prevent to many for loops it is better to extract the root of the number.

****

public class Calculation {

public static boolean prime(long num) {

long sq_root = (long) Math.sqrt(num);

for (long a = 2; a <= sq_root; a++) {

if (num % a == 0) {

return false;

}

}

return true;

}

}

TC65_Insidera at 2007-7-12 16:24:26 > top of Java-index,Java Essentials,Java Programming...