Results 1 to 8 of 8
  1. #1
    yingyang69 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default generating prime numbers...using an array

    I am trying to complete a class assignment and am currently unsuccessful...the assignment requires:
    This program requires that you find all of the primes between 1 and some number that you allow the user to input.

    Fill an array with the consecutive integers between 1 and the top number.
    Identify 2 as the first prime and then remove all numbers that are evenly divisible by 2 (use modulus function).
    Go to the next number remaining on the list (the next prime); now remove all of the numbers divisible by it.
    Continue until you have only prime numbers in your list. When can you stop?
    Now, write a Java program that generates all of the primes between 2 and some upper bound (greater than 50) that you allow the user to enter. As intermediate data, print one line for each divisor you eliminate along with the numbers eliminated. For example:
    2: 4 6 8 10 12 14 16 18...
    3: 9 15 21 27

    so far I have the following code, but it isn't working...

    Scanner keyboard = new Scanner(System.in);
    int size;
    int sentinel = 0;
    int [] numberArray;

    System.out.println("Enter the size of the array from 1 to 100:");
    size = keyboard.nextInt();
    numberArray = new int[size];

    //Fill the array.
    for (int i=0; i<size; i++)
    {
    numberArray[i]=i+1;
    }

    //Identify 2 as the first prime number and then remove all numbers that are evenly divisible by 2
    int baseNumber = 2;
    for (int i=1; i<size; i++)
    if (numberArray[i] != sentinel)
    {
    int base = numberArray[i];
    System.out.print(base + ": ");
    if (numArray[i]%baseNumber == 0)
    {
    System.out.print(numberArray[i] + " ,");
    numberArray[i] = sentinel;
    }
    }
    }

    }

  2. #2
    mr_guy is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    I dont know if its just me but i found your code quite confusing.
    here is how i would do it:
    Java Code:
    Scanner keyboard = new Scanner(System.in);
    	int size;	
    	int [] numberArray;
    	int [] out;
    
    	System.out.println("Enter the size of the array from 1 to 100:");
    	size = keyboard.nextInt();
    	numberArray = new int[size];
    
    	//Fill the array.
    	for (int i=1; i<size; i++)
    	{
    	numberArray[i]=i;
    	}
    
    	
    	numberArray[1] = -1;
    	//Identify 2 as the first prime number and then remove all numbers that are evenly divisible by 2
           //start the for loop with 2
    	for (int i=2; i<size; i++){
    		//if the number has not already been discluded
    		if(numberArray[i] != -1){
    			for(int j = 0; j < size; j++ ){
    				// if the number is a multiple of another number
    				if(numberArray[j]%numberArray[i] == 0 && numberArray[j] != numberArray[i] ){
    					//disclude it
    					numberArray[j] = -1;
    				}
    			}
    		}	
    	}
    	//disclude 2
    	numberArray[2] = -1;
    	
    	//print
    	for(int i = 1; i < size; i ++){
    		if(numberArray[i] != -1)
    			System.out.print(numberArray[i] + ", ");
    	}
    ps. please use code tags around your code

  3. #3
    yingyang69 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    thank you very much! how do i get the code to output the excluded numbers as well...we are supposed to output the number: its excluded multiples
    ex:
    2: 4 6 8 10 etc

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    mr_guy is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    you can change the code to make it more similar to yours and make it print as you go or you can edit the printing part on the pecie of code i made so it prints the multiples.
    its just a simple matter of inserting another 'for' loop and multiplying the number by the increasing variable.
    Last edited by mr_guy; 03-01-2011 at 05:23 AM. Reason: Edit becuse of sunde887's post below

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please don't spoon feed. What does he learn from you providing the source code? Give him pseudocode or push him in the right direction, this way he will learn to solve problems more efficiently, with less help from others.

    Also, the fill the array code will leave the first spot in the array null
    it should be
    Java Code:
    for(int i = 1; i <= size; i++
    
    //or
    for(int i = 0; i < size; i++){
      numArray[i - 1] = i;
    }
    Your method would produce something like
    Java Code:
    num[0] == 0
    num[1] == 1
    num[2] == 2
    ...
    num[10] == 9
    Last edited by sunde887; 03-01-2011 at 05:21 AM.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    This is performing the Sieve of Eratosthenes and I find it much easier to use an array of booleans.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    By the way Mr_guy, I am not trying to be rude, but think about it, if you come here with a problem and someone just posts you the code you are a lot less likely to understand how something works.

Similar Threads

  1. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 02:50 AM
  2. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 08:22 AM
  3. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 07:05 AM
  4. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:09 AM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 04:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •