package primenumbers; /** * * @author TM2011 */ public class PrimeNumbers { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here java.util.Scanner input = new java.util.Scanner(System.in); final int NUMBER_OF_PRIMES = 50; final int NUMBER_OF_PRIMES_PER_LINE = 10; int count = 0; int number = 2; System.out.println("The first 50 primes numbers are \n"); while (count < NUMBER_OF_PRIMES) { boolean isPrime = true; for(int divisor = 2; divisor <= number / 2; divisor ++){ if (number % divisor == 0) { isPrime = false; break; } } if (isPrime) { count++; if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { System.out.println(number); } else System.out.println(number + " "); } number++; } } }

## 15 Replies - 9034 Views - Last Post: 29 November 2011 - 04:47 PM

### #1

# Prime Numbers - two approaches

Posted 27 November 2011 - 01:12 PM

##
**Replies To:** Prime Numbers - two approaches

### #2

## Re: Prime Numbers - two approaches

Posted 27 November 2011 - 01:40 PM

Your current for loop tests from 2 to number / 2. I think what you want to do is change that loop from 2 to Math.sqrt( number ).

You should be able to modify the for loop condition without help, but ask if you need it.

### #3

## Re: Prime Numbers - two approaches

Posted 27 November 2011 - 07:13 PM

### #4

## Re: Prime Numbers - two approaches

Posted 27 November 2011 - 07:16 PM

tmotley12, on 27 November 2011 - 10:13 PM, said:

squareroot() is just there to determine up to what number you should check

nothing to do in the actual calculation of if the number is prime or not

### #5

## Re: Prime Numbers - two approaches

Posted 27 November 2011 - 07:24 PM

can anyone show my the actual math.. such as sqrt9=3 9/3=3 therefore 9 is not a prime?? (Is this what they are asking me to do?)

This post has been edited by **tmotley12**: 27 November 2011 - 07:28 PM

### #6

## Re: Prime Numbers - two approaches

Posted 27 November 2011 - 07:25 PM

int lowestprime = 1; int highestprime = 51; // check each number, skipping over even numbers for (int i = lowestprime; i < highestprime; i+=2) { // check for divisors of i for (int j = 1; j < THESQUAREROOTOFI; j++) { if (i % j == 0) { // number is not prime. } } }

This post has been edited by **Mylo**: 27 November 2011 - 07:29 PM

### #7

## Re: Prime Numbers - two approaches

Posted 27 November 2011 - 07:34 PM

Mylo, on 27 November 2011 - 09:25 PM, said:

int lowestprime = 1; int highestprime = 51; // check each number, skipping over even numbers for (int i = lowestprime; i < highestprime; i+=2) { // check for divisors of i for (int j = 1; j < THESQUAREROOTOFI; j++) { if (i % j == 0) { // number is not prime. } } }

with this approach I could use an array to store the numbers I want to run through the sqrt I am assuming?

### #8

## Re: Prime Numbers - two approaches

Posted 27 November 2011 - 07:38 PM

The old Greeks had a better algorithm

http://www.dreaminco...-prime-numbers/

This link will let you compute Prime up to 2^64 in a few milli seconds, but may be you still want to iterate

### #9

## Re: Prime Numbers - two approaches

Posted 27 November 2011 - 07:48 PM

### #10

## Re: Prime Numbers - two approaches

Posted 27 November 2011 - 07:58 PM

### #11

## Re: Prime Numbers - two approaches

Posted 29 November 2011 - 03:48 PM

public static void main(String[] args) { // TODO code application logic here java.util.Scanner input = new java.util.Scanner(System.in); final int Primes = 50; int count = 0; int number = 2; System.out.println("The first 50 primes numbers are \n"); public static boolean isPrime(int [] tab) { int[] prime = new int[51]; for (int i = 3; i <= Math.sqrt(tab[i]); i += 2) if (tab[i] % i == 0) { prime = false; break; } for(int i=0; i<tab.length; i++) if (( tab[i]%2 !=0 && prime && tab[i] > 2) || tab[i] == 2) {return true;} else {return false;} } while (count < Primes) { boolean prime = true; if (isPrime) { count++; } else System.out.println(number + " "); } number++; } }

I know the code is a mess, I am just trying to see if I got the math concept down..

### #12

## Re: Prime Numbers - two approaches

Posted 29 November 2011 - 04:03 PM

int[] prime = new int[51]; prime = false; //?

But other than that

for (int i = 3; i <= Math.sqrt(tab[i]); i += 2) { if (tab[i] % i == 0) { // This is not checking all values, only one is each element. prime = false; break; } }

I really can't read your code, too broken sorry. But remember, you need to check ALL values (up to sqrt(number)), for EACH number. Your code just checks tab[1] % 1, tab[2] % 2... and son on.

### #13

## Re: Prime Numbers - two approaches

Posted 29 November 2011 - 04:04 PM

Fix the code, run it, and then if you're unsure if the results are correct, come back.

### #14

## Re: Prime Numbers - two approaches

Posted 29 November 2011 - 04:15 PM

prime = false; //assign boolean to array of ints

In short isPrime() is incorrect, I am not sure how you are going to call it, what are these two for loops doing there, the one is trying to assign false but the the second has returns in it either true or false!

### #15

## Re: Prime Numbers - two approaches

Posted 29 November 2011 - 04:40 PM

smohd, on 29 November 2011 - 06:15 PM, said:

prime = false; //assign boolean to array of ints

In short isPrime() is incorrect, I am not sure how you are going to call it, what are these two for loops doing there, the one is trying to assign false but the the second has returns in it either true or false!

I think I am overthinking this and confusing myself in the process. I am going to start with a clean slate and get rid of this mess. *sigh*