5 Replies - 16865 Views - Last Post: 24 March 2009 - 04:20 PM Rate Topic: -----

#1 almayhm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 09

100 Prime Numbers Program

Post icon  Posted 24 March 2009 - 11:29 AM

Hey guys, I made this program to generate the first 100 prime numbers starting from 1. Only thing I need help with is having the output print out horizontally, instead of a straight down list. Maybe like a break between every 30 numbers or so. Any form of help is greatl appreciated, thanks!


//Allan Rodriguez


public class whileprime
{
	public static void main(String args[])
{
	int num = 4;
	int count = 0;
	System.out.println("First 100 Prime Numbers: ");
	System.out.println("1");
	System.out.println("2");
	System.out.println("3");
	while(count < 100)
		{
			num++;
			if(num % 2 != 0)
				{
					int dv = 3;
					while((num % dv != 0)&&(dv <= ((num - 1) / 2)))
						{
							dv++;
						}
							if(num % dv != 0)
								{
									count++;
									System.out.println(num + "		 ");
									
								}
				}
		}


}


} 

--------------------Configuration: <Default>--------------------
First 100 Prime Numbers: 
1
2
3
5		 
7		 
11		 
13		 
17		 
19		 
23		 
29		 
31		 
37		 
41		 
43		 
47		 
53		 
59		 
61		 
67		 
71		 
73		 
79		 
83		 
89		 
97		 
101		 
103		 
107		 
109		 
113		 
127		 
131		 
137		 
139		 
149		 
151		 
157		 
163		 
167		 
173		 
179		 
181		 
191		 
193		 
197		 
199		 
211		 
223		 
227		 
229		 
233		 
239		 
241		 
251		 
257		 
263		 
269		 
271		 
277		 
281		 
283		 
293		 
307		 
311		 
313		 
317		 
331		 
337		 
347		 
349		 
353		 
359		 
367		 
373		 
379		 
383		 
389		 
397		 
401		 
409		 
419		 
421		 
431		 
433		 
439		 
443		 
449		 
457		 
461		 
463		 
467		 
479		 
487		 
491		 
499		 
503		 
509		 
521		 
523		 
541		 
547		 
557		 

Process completed.




Is This A Good Question/Topic? 0
  • +

#3 Nat3TheGreat13  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 162
  • Joined: 09-February 09

Re: 100 Prime Numbers Program

Posted 24 March 2009 - 11:31 AM

Use
System.out.print(num + "		 ");


Was This Post Helpful? 1

#4 almayhm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 09

Re: 100 Prime Numbers Program

Posted 24 March 2009 - 11:35 AM

View PostNat3TheGreat13, on 24 Mar, 2009 - 10:31 AM, said:

Use
System.out.print(num + "		 ");




thanks!, is there a way to have it do the same but in groups of 10?

ex. output as follows:

1 29
2 31
3 37
5 41
7 43
11 47
13 53
17 59
19 61
23 67 ETC.

This post has been edited by almayhm: 24 March 2009 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: 100 Prime Numbers Program

Posted 24 March 2009 - 11:46 AM

btw 1 is not a prime number
Was This Post Helpful? 1
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: 100 Prime Numbers Program

Posted 24 March 2009 - 11:51 AM

here's a better algorithm for this, this is called the Sieve of eratosthenes algorithm
import java.util.Scanner;

public class Primes
{
	public static void generate(int x, int y)
	{
		boolean [] a = new boolean[y+1];
		for(int i = 2; i < a.length; i++)
			a[i] = true;

		for(int i = 2; i*i <= y; i++)
		{
			for(int j = i; j*i <= y; j++)
				{
				a[i*j] = false;
			}
		}
		
		for(int i = x; i <= y; i++)
			if(a[i])
				System.out.print(i + " ");
		System.out.println();
	}

	public static void main(String[]args)
	{
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int y = sc.nextInt();
		generate(x, y);
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 thebpf  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 42
  • Joined: 19-March 09

Re: 100 Prime Numbers Program

Posted 24 March 2009 - 04:20 PM

Create an array of type String to place the results in:

String primes[] = new String[10];
for (i=0 to 9)
for (j=0 to 9) // this is needed so you can get your 100 prime numbers
primes[i] = prime+"<tab>"

Then:
for(i=0 to 9)
System.out.println(primes[i]);

View Postalmayhm, on 24 Mar, 2009 - 10:35 AM, said:

thanks!, is there a way to have it do the same but in groups of 10?

ex. output as follows:

1 29
2 31
3 37
5 41
7 43
11 47
13 53
17 59
19 61
23 67 ETC.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1