Prime test, 2 digit numbers

I want to print all primes 2 digits long. Here is my code:

for(int input = 11; input <= 99; input += 2){ for(int x = 2; x < (int)Math.sqrt(input) + 1; x++){ if(input%x != 0){ System.out.println(input); break; }else{ break; } } } 

The problem is that it prints numbers, such as 35 or 49, which are not prime numbers.

+4
source share
9 answers

It works. You can see the exit here .

 public class Main { public static void main(String[] args) { for(int input = 11; input <= 99; input += 2){ boolean found = false; for(int x = 2; x < (int)Math.sqrt(input) + 1; x++){ if(input%x == 0){ found = true; break; } } if(!found) { System.out.println(input); } } } } 
+2
source

Your rudeness test is incorrect. You stop testing the number ( input ) as soon as you find a divisor that input not divisible by.

This is not a prime definition - you need to check that the input number is not divisible by any divisors less than it - in other words, you need to check all the x values โ€‹โ€‹before you can declare the number as a prime number.

You can exit the loop that checks input % x != 0 when input is divided by x , but not when it is not divided - you need to continue checking when this condition is true!

+7
source

I always like questions where the shortest and most obvious answer is to simply burn a hard drive:

 System.out.println("11\n13\n17\n19\n23\n29\n31\n37\n41\n43" + "\n47\n53\n59\n61\n67\n71\n73\n79\n83\n89\n97"); 
+6
source

To find a prime, you do not need to check every number under it to its square root; you just need to test every other number under it. Here is a demo:

 ArrayList<Integer> primes = new ArrayList<Integer>(); // hold every other prime numbers that we find boolean isPrime; // add first base prime number primes.add(2); for (int x = 3; x < max; x+=2) { // start from 3 and skip multiples of 2 isPrime = true; // prove it not prime for (Integer i : primes) { if (x % i == 0) { isPrime = false; // x is divisible by a prime number... break; // exit loop, we proved it not a prime } } if (isPrime) { if (x >= 10) { System.out.println(x); // print only two digits prime numbers } primes.add(x); // add x to our prime our number list for future checks } } 

** EDIT **

Even more flexible and reusable implementation in the static method (not optimized for listing large primes):

 public static List<Integer> findPrimes(int min, int max) { LinkedList<Integer> primes = new LinkedList<Integer>(); boolean isPrime; double square; // add first base prime number primes.add(2); for (int x = 3; x < max; x+=2) { // start from 3 and skip multiples of 2 isPrime = true; // prove it not prime square = Math.sqrt(x); for (Integer i : primes) { isPrime = x % i != 0; // x is not prime if it is divisible by i if (!isPrime || i > square) { break; } } if (isPrime) { primes.add(x); // add x to our prime numbers } } // remove all numbers below min while (!primes.isEmpty() && primes.getFirst() < min) { primes.removeFirst(); } return primes; } 

Using the method:

 List<Integer> primes = findPrimes(10, 100); System.out.println("Listing " + primes.size() + " prime numbers"); for (Integer prime : primes) { System.out.println(prime); } 
+4
source

Wikipedia provides a good algorithm for finding primes. It also contains a list of up to 100.

If you are looking for debugging help, I would just skip your code until you see which of your tests are incorrect. This is a simple and simple program that does not take you much time.

+3
source

The problem is in this block:

 if(input%x != 0) { System.out.println(input); break; } 

It will print 'input' if it is not divided by the current value of 'x', but it can be divided by the following values โ€‹โ€‹of 'x'. In this case, "enter" is not simple, but is printed.

The correct code is:

 boolean flag; for(int input = 11; input <= 99; input += 2) { flag = true; for(int x = 2; x < (int)Math.sqrt(input) + 1; x++) { if(input%x == 0) { flag = false; break; } } if(flag == true) System.out.println(input); } 
+3
source
 for(int input = 11; input <= 99; input += 2){ int found = 0; for(int x = 2; x < (int)Math.sqrt(input) + 1; x++) if(input%x == 0){ found = 1; break; } if(found == 0) System.out.println(input); } 

this should do the job

+2
source

You print all the odd numbers. You always exit the loop, so x never exceeds 2.

 if(input%x != 0){ System.out.println(input); break; // <<-- break here }else{ break; // <<-- and break here } 

Also, carefully study the definition of a prime number:

  • If any of the numbers you are testing is an exact divisor, the number is not prime.
  • If all the numbers you are testing are not exact divisors, then the number is prime.

You should exit the loop only if you find a divisor. If you have not found it, you need to continue until you try all the possibilities. And you donโ€™t have to print anything until you finish the cycle and know the result.

+2
source

Yes, because it prints out all the cases when a non-factor is found. You need ONLY print cases when there are no factors.

0
source

All Articles