# Prime Numbers question

Page 1 of 1

## 5 Replies - 1345 Views - Last Post: 18 April 2012 - 05:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=275268&amp;s=a5b260a7a3462a85bcaa2400e33849f6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 alirezait

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 14-April 12

# Prime Numbers question

Posted 14 April 2012 - 08:21 PM

hi everyone. I have an assignment that asks me to print the first 100 prim numbers and it should be each line 10 numbers and also right justified.
I did everything except that I don't know how to right justify it?.
I would be really thankful if someone could help me.
thanks.

```public class Q1  {

public static void main(String[] args) {

int[] nums = new int[100];

int j = 2, q = 0, num = 2, count = 0;
double m = Math.sqrt(num);
boolean isPrime = true;

while (q < 100)
{
while (j <= m && isPrime)
{
if (num % j == 0)
isPrime = false;
else j++;
}
j = 2;
if (isPrime == true)
{
nums[q] = num;

System.out.print(nums[q] + " ");
count++;
q++;
}

if (count == 10){
System.out.println();
count = 0;
}

isPrime = true;
num++;
m = Math.sqrt(num);

}

}
}

```

*Edited: topic title changed to a more meaninful title "Question" is really useless
Code tags fixed. Please

This post has been edited by pbl: 14 April 2012 - 08:26 PM

Is This A Good Question/Topic? 0

## Replies To: Prime Numbers question

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Prime Numbers question

Posted 14 April 2012 - 08:53 PM

m = Math.sqrt(num);

and what is that supposed to do ?
and why re-setting it ?

and having a variable num and a variable nums is not a good idea at all: quite confusing
maybe numList to replace nums would be more informative

### #3 sepp2k

• D.I.C Lover

Reputation: 2620
• Posts: 4,175
• Joined: 21-June 11

## Re: Prime Numbers question

Posted 15 April 2012 - 04:45 AM

pbl, on 15 April 2012 - 05:53 AM, said:

m = Math.sqrt(num);

and what is that supposed to do ?

He takes the square root of num, so he only has to iterate over sqrt(num) numbers instead of all num items. He stores it in a variable, so he doesn't have to recalculate the square root each time, each time the loop condition is checked.

Yes, he could also just check that j * j <= num, but either he didn't think of that or he though this way was more readable.

Quote

and why re-setting it ?

He only recalculates m when num changed, which he has to because otherwise the program would break.

@OP:

Take a look at String.format and the Formatter class:
http://docs.oracle.c.../Formatter.html

### #4 alirezait

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 14-April 12

## Re: Prime Numbers question

Posted 15 April 2012 - 08:03 AM

thanks, I'll read it. if i could not understand I'll disturb you again

### #5 alirezait

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 14-April 12

## Re: Prime Numbers question

Posted 18 April 2012 - 04:09 AM

Hi.
I still can't understand how can I right justify my numbers.
I have to use printf, is it right?. how can I use that?. I'm really confused with this justifying

### #6 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Prime Numbers question

Posted 18 April 2012 - 05:48 AM

Here's a simple explanation and exampleof justifying text using printf.