5 Replies - 951 Views - Last Post: 18 April 2012 - 05:48 AM Rate Topic: -----

#1 alirezait  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :code:

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  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,274
  • Joined: 21-June 11

Re: Prime Numbers question

Posted 15 April 2012 - 04:45 AM

View Postpbl, 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
Was This Post Helpful? 1
  • +
  • -

#4 alirezait  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :D ;)
Was This Post Helpful? 0
  • +
  • -

#5 alirezait  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :(
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • 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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1