473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to get prime number generator to also output excluded nonprimes?

The code I created prints prime numbers but i also need to print their multiples that I previously excluded from being generated. Sorry if that's confusing but...I have the code worked out so that all the prime numbers generate as followed:
2:
3:
5:
etc.
However, I have absolutely no idea how to get it to print the numbers (the multiples of the prime numbers printed) excluded after it so that it looks like:
2: 4 6 8 10
3: 9 15
etc


Expand|Select|Wrap|Line Numbers
  1. Scanner keyboard = new Scanner(System.in);
  2. //variables
  3. int size;
  4. int [] numberArray;
  5. int [] out;
  6.  
  7. //user input upperbound
  8. System.out.println("Enter the size of the array from 2 to 100:");
  9. size = keyboard.nextInt();
  10. //array
  11. numberArray = new int[size];
  12.  
  13. //Fill the array.
  14. for (int i=1; i<size; i++)
  15. {
  16. numberArray[i]=i;
  17. }
  18.  
  19.  
  20. numberArray[1] = -1;
  21. //Identify 2 as the first prime number and then remove all numbers that are evenly divisible by 2
  22. //start the for loop with 2
  23. for (int i=2; i<size; i++){
  24. //if the number has not already been discluded
  25. if(numberArray[i] != -1){
  26. for(int j = 0; j < size; j++ ){
  27. // if the number is a multiple of a prime
  28. if(numberArray[j]%numberArray[i] == 0 && numberArray[j] != numberArray[i] ){
  29. //exclude it
  30. numberArray[j] = -1;
  31.  
  32. }
  33. }
  34. }
  35. }
  36.  
  37. //print prime number
  38. for(int i = 1; i < size; i ++){
  39. if(numberArray[i] != -1)
  40. System.out.println(numberArray[i] + ":");
  41.  
  42. }
  43.  
  44.  
  45. System.out.println();
  46.  
  47.  
  48. }
Mar 1 '11 #1
1 2364
Dheeraj Joshi
1,123 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. 2:0 4 6 8 
  2. 3:9 
  3. 5:
  4. 7:
  5.  
Is this is what you expect for numbers between 0-9?

I added one print statement

Expand|Select|Wrap|Line Numbers
  1. for (int i=2; i<size; i++){
  2.          if(numberArray[i] != -1){
  3.              System.out.print(numberArray[i] + ":");
  4.          for(int j = 0; j < size; j++ ){
  5.              if(numberArray[j]%numberArray[i] == 0 && numberArray[j] != numberArray[i] ){
  6.                      System.out.print(numberArray[j] + " ");
  7.                          numberArray[j] = -1;
  8.                  }
  9.              }
  10.              System.out.println();
  11.          }
  12. }
  13.  
Try this.

Regards
Dheeraj Joshi
Mar 1 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
19
by: swisscheese | last post by:
I figured someone out there must have written a minimal code size prime number generator. I did not find one after a bit of searching around. For primes up to 100 the best I could do was 70...
10
by: Joel Mayes | last post by:
Hi All; I'm teaching myself C, and have written a prime number generator. It is a pretty inefficient implementation of the Sieve of Eratosthenes to calculate primes up to 1,000,000. If anyone...
5
by: maks | last post by:
Hi! I need some help in modifying this prime number generator code. How do I modify this code so that it assigns prime numbers to an array and returns it? I have tried to get it work but it...
24
by: shivaraman | last post by:
can any one give me the code for generating prime numbers using c program
7
by: Caffiend | last post by:
Well, I've been picking at learning python, got tired of reading, and figured I'd try to replicate my prime number generator I wrote (with much TSDN forum help) in C++. I've hit a stumbling block......
3
by: koolest1 | last post by:
I've been having problem with the following code. It's supposed to print the prime numbers between 10 and 100. But i'm not getting any output, i.e. i guess the outer 'for' loop is being traversed...
15
by: mjslaugh | last post by:
I am new to Java and programming, I have an assignment that I am a little stuck on and I was hoping to get a little help if anyone is willing. I have read a few posts on the topic and it seems most...
2
by: sudankanakavel | last post by:
i need a random prime number generator which will generate prime numbers for given range for eg 22222 to 99999 operating system : windows language : java
2
by: willistwigge | last post by:
Good Afternoon, I am trying to create a program that generates all the prime numbers from 2 to n, an intiger input by the user. I tried to accomplish this by using a function that i created by the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.