Prime Number I'd like to learn how to figure the prime numbers out

pragram should accepts a positive int n (2<n<500) and uses the S

Page 1 of 1

10 Replies - 1295 Views - Last Post: 31 March 2009 - 11:55 AM Rate Topic: -----

#1 SovbodMark  Icon User is offline

  • New D.I.C Head

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

Prime Number I'd like to learn how to figure the prime numbers out

Post icon  Posted 31 March 2009 - 10:29 AM

This program should accepts a postive integer n (2<n<500) and uses the Sieve of Eratosthenes to determine all prime numbers less than or equal to n.
This program take me forever finally I found your website
I hope I can learn
public class PrimeNumbers
{
   
public final static int MAX = 500;
   
	  public void initialize(int[] primeArr)
	  {
		 primeArr[0] = 0;
		 primeArr[1] = 0;
		 for (int i = 2; i < primeArr.length; i++)
		 {
			primeArr[i] = i;
		 }
	  }
   
	   public int next(int[] primeArr, int k)
	  {
		 for (int i = k; i < primeArr.length; i++)
		 {
			if (primeArr[i] != 0)
			   return i;
		 }
	  
		 return primeArr.length - 1;
	  }
   
	   public void getPrimes(int[] primeArr)
	  {
	  
		 initialize(primeArr);
	  
		 for (int i = 2; i < primeArr.length; i++)
		 {
			int next = next(primeArr, i);
		 
			for (int j = next + 1; j < primeArr.length; j++)
			{
			
			   if (primeArr[j] % primeArr[next] == 0)
			   {
				  primeArr[j] = 0;
			   }
			
			}
		 }
	  
	  }
   
	 public static void main(String[] args)
	 {
		 int[] primeArr = new int[MAX];
	  
		 PrimeNumbers primes = new PrimeNumbers();
		 primes.getPrimes(primeArr);
	  
		 for (int k = 2; k < primeArr.length; k++)
		 {
			if (primeArr[k] != 0)
			   System.out.print(primeArr[k] + "  ");
		 }
	  
	  }
   
}


Thank you in advance

This post has been edited by SovbodMark: 31 March 2009 - 10:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Prime Number I'd like to learn how to figure the prime numbers out

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 10:44 AM

public static void primes(int n)
{
		boolean [] a = new boolean[n+1];
		for(int i = 2; i <= n; i++)
		{
			a[i] = true;
		}
		
		for(int i = 2; i*i <= n; i++)//crosses out the multiples of prime numbers
		{
			for(int j = i; j*i <= n; j++)
			{
				a[j*i] = false;
			}
		}
		for(int i = 1; i < a.length; i++)
		{
			if(a[i])
				System.out.println(i);
		}
}


this method should do it for you
Was This Post Helpful? 1
  • +
  • -

#3 SovbodMark  Icon User is offline

  • New D.I.C Head

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

Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 10:49 AM

I really appreciate your :^: , but where should I put this in my code and I should get a message ask the user to enter number between 2<n<500 and give all the prime number less than or == n.


View Postmostyfriedman, on 31 Mar, 2009 - 09:44 AM, said:

public static void primes(int n)
{
		boolean [] a = new boolean[n+1];
		for(int i = 2; i <= n; i++)
		{
			a[i] = true;
		}
		
		for(int i = 2; i*i <= n; i++)//crosses out the multiples of prime numbers
		{
			for(int j = i; j*i <= n; j++)
			{
				a[j*i] = false;
			}
		}
		for(int i = 1; i < a.length; i++)
		{
			if(a[i])
				System.out.println(i);
		}
}


this method should do it for you

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:00 AM

Quote

I really appreciate your icon_up.gif , but where should I put this in my code and I should get a message ask the user to enter number between 2<n<500 and give all the prime number less than or == n.


you can do that in your main method, print the message and then call the method with 500

just make sure that you understand the code and how it works, you better trace it on a piece of paper to see what's going on
Was This Post Helpful? 0
  • +
  • -

#5 SovbodMark  Icon User is offline

  • New D.I.C Head

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

Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:00 AM

See what I did, but it still gives me an error :wub:
I don't know where these error comes from.
Can anybody do all the changes needed in my code
here is my final code
public class PrimeNumbers
{
   
public final static int MAX = 500;
   
	  public void initialize(int[] primeArr)
	  {
		 primeArr[0] = 0;
		 primeArr[1] = 0;
		 for (int i = 2; i < primeArr.length; i++)
		 {
			primeArr[i] = i;
		 }
	  }
   
	   public int next(int[] primeArr, int k)
	  {
		 for (int i = k; i < primeArr.length; i++)
		 {
			if (primeArr[i] != 0)
			   return i;
		 }
	  
		 return primeArr.length - 1;
	  }
   
	   public void getPrimes(int[] primeArr)
	  {
	  
		 initialize(primeArr);
	  
		 for (int i = 2; i < primeArr.length; i++)
		 {
			int next = next(primeArr, i);
		 
			for (int j = next + 1; j < primeArr.length; j++)
			{
			
			   if (primeArr[j] % primeArr[next] == 0)
			   {
				  primeArr[j] = 0;
			   }
			
			}
		 }
	  
	  }
   public static void primes(int n)
{
		boolean [] a = new boolean[n+1];
		for(int i = 2; i <= n; i++)
		{
			a[i] = true;
		}
		
		for(int i = 2; i*i <= n; i++) //crosses out the multiples of prime numbers
		{
			for(int j = i; j*i <= n; j++)
			{
				a[j*i] = false;
			}
		}
		for(int i = 1; i < a.length; i++)
		{
			if(a[i])
				System.out.println(i);
		}
}
  public static void main(String [] args)
	{
		int number;

		number = getInteger();

		if(isPrime(number))
		  {
			System.out.println(number + " is a prime number.");
		} else {
			System.out.println(number + " isn't a prime number.");
			System.out.println("All the prime number before " + number + " is " +						   getNextPrime(number) + ".");
		}
	}

	// Reads an integer read fron the user's keyboard
	private static int getInteger()
	{
		Scanner keyboard = new Scanner(System.in);

		System.out.print("Enter number between 2 and 500? ");

		return keyboard.nextInt();
	}

	 public static void main(String[] args)
	 {
		 int[] primeArr = new int[MAX];
	  
		 PrimeNumbers primes = new PrimeNumbers();
		 primes.getPrimes(primeArr);
	  
		 for (int k = 2; k < primeArr.length; k++)
		 {
			if (primeArr[k] != 0)
			   System.out.print(primeArr[k] + "  ");
		 }
	  
	  }
   
}


Thanks Laudies ;) and Gentlmen :)
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:03 AM

you cant have 2 main methods in the same class, you have to remove one

btw you also dont need all those methods to generate prime numbers, the method i provided you is all you need
Was This Post Helpful? 0
  • +
  • -

#7 SovbodMark  Icon User is offline

  • New D.I.C Head

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

Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:14 AM

I know what you're talking about, but your gives me all the prime numbers btw 2 - 500, I need a message says hey you should enter, n number btw 2<n<500 and run all the prime numbers less or equal to that n.
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:17 AM

ah i see, then
Scanner sc = new Scanner(System.in); 
System.out.println("enter a number between 2 and 500);
int n = sc.nextInt();
System.out.println(primes(n));


all this in the main method
Was This Post Helpful? 0
  • +
  • -

#9 SovbodMark  Icon User is offline

  • New D.I.C Head

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

Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:29 AM

View Postmostyfriedman, on 31 Mar, 2009 - 10:17 AM, said:

ah i see, then
Scanner sc = new Scanner(System.in); 
System.out.println("enter a number between 2 and 500);
int n = sc.nextInt();
System.out.println(primes(n));


all this in the main method

I know I should just use one method in class, but Which main method should it be.
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:37 AM

just keep one of them and add to it the functionality you want
Was This Post Helpful? 0
  • +
  • -

#11 SovbodMark  Icon User is offline

  • New D.I.C Head

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

Re: Prime Number I'd like to learn how to figure the prime numbers out

Posted 31 March 2009 - 11:55 AM

It didnt like it this way, I might put it somewhere elso or change it.
what do u think?
public class PrimeNumbers
{
   
	 public static void main(String[] args)
	 {
		 int[] primeArr = new int[MAX];
	  
		 PrimeNumbers primes = new PrimeNumbers();
		 primes.getPrimes(primeArr);
	  
		 for (int k = 2; k < primeArr.length; k++)
		 {
			if (primeArr[k] != 0)
			   System.out.print(primeArr[k] + "  ");
		 }
	  
	  }
		Scanner sc = new Scanner(System.in); 
	  System.out.println("enter a number between 2 and 500");
	  int n = sc.nextInt();
	  System.out.println(primes(n));

		public static void primes(int n)
	  {
		boolean [] a = new boolean[n+1];
		for(int i = 2; i <= n; i++)
		{
			a[i] = true;
		}
		
		for(int i = 2; i*i <= n; i++)//crosses out the multiples of prime numbers
		{
			for(int j = i; j*i <= n; j++)
			{
				a[j*i] = false;
			}
		}
		for(int i = 1; i < a.length; i++)
		{
			if(a[i])
				System.out.println(i);
		}

   
}
public final static int MAX = 500;
   
	  public void initialize(int[] primeArr)
	  {
		 primeArr[0] = 0;
		 primeArr[1] = 0;
		 for (int i = 2; i < primeArr.length; i++)
		 {
			primeArr[i] = i;
		 }
	  }
   
	   public int next(int[] primeArr, int k)
	  {
		 for (int i = k; i < primeArr.length; i++)
		 {
			if (primeArr[i] != 0)
			   return i;
		 }
	  
		 return primeArr.length - 1;
	  }
   
	   public void getPrimes(int[] primeArr)
	  {
	  
		 initialize(primeArr);
	  
		 for (int i = 2; i < primeArr.length; i++)
		 {
			int next = next(primeArr, i);
		 
			for (int j = next + 1; j < primeArr.length; j++)
			{
			
			   if (primeArr[j] % primeArr[next] == 0)
			   {
				  primeArr[j] = 0;
			   }
			
			}
		 }
	  
	  }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1