Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: trying to generate prime numbers

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default trying to generate prime numbers

    so for my code i am trying to generate all the prime numbers between 2 and a given user input...i have succeeded in that. however, I also need to include all the excluded multiplies of that prime number.
    EX:
    2: 4 6 8 ....
    3: 9 15 21....

    this is what i have so far...

    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;
     
    				}
    			}
    		}
    	}
     
    	//print
    	for(int i = 1; i < size; i ++){
    		if(numberArray[i] != -1)
    			System.out.println(numberArray[i] + ":");
            }
     
     
            System.out.println();

    any help would be greatly appreciated!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: trying to generate prime numbers

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/39676-generating-prime-numbers-using-array.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: trying to generate prime numbers

    Welcome to the forums yingyang69.

    If you scroll down to the bottom of this thread, there is a 'Similar Threads' box which will contain links that may help you.

    http://www.javaprogrammingforums.com...-200-java.html

    http://www.javaprogrammingforums.com...hile-loop.html

    http://www.javaprogrammingforums.com...ci-series.html

    I have compiled the code and there seems to be a few issues.
    If you enter 1 as the size of the array, the program falls over with an ArrayIndexOutOfBoundsException exception.

    I can not get it to display any prime numbers...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: trying to generate prime numbers

    I have checked several of the similar threads and have so far found none that help me with my specific question...

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: trying to generate prime numbers

    First off, I think you need to get it to correctly take the size of the array, then the number from user input and print out the prime numbers.
    When that is complete, we can work on showing the excluded multiplies.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: trying to generate prime numbers

    I only need to start at 2 so I adjusted the instructions in the first println to state you must enter a number between 2 and 100. Thank you for catching that. When I enter a number greater than 2 the primes do show up.

Similar Threads

  1. Display prime numbers from 100 to 200 in Java
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 25th, 2011, 03:14 PM
  2. prime numbers
    By tdz013 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2011, 11:24 AM
  3. composite and prime
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 3
    Last Post: October 13th, 2010, 09:01 PM
  4. Need help.. Counting Prime #'s up to 50 w/while loop
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 6th, 2010, 05:40 PM
  5. Generate prime numbers within fiboncacci series?
    By Manish87 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 7th, 2010, 12:24 PM