|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Prime numbers using arrays
This is Question??
The Sieve of Eratosthenes is an ancient Greek algorithm for deriving the list of prime numbers up to a certain point. The algorithm works as follows: 1. Create an empty list of known prime numbers 2. Create a list of numbers from 2 up to the maximum number you wish to test. 3. Add 2 to the list of known prime numbers and then mark of every multiple of 2 as tested for primality. 4. The next unmarked number in the list is prime, however all its subsequent multiples are not. Add this number to the known primes list and mark all its multiples accordingly. 5. Repeat step 4 until no untested numbers remain in the list. Once you reach the square root of the maximum number being tested all remaining numbers in the list will be prime. Implement the above algorithm using the Java Programming language. Create a PrimeCandidate class which encapsulates an integer value as well as an indication as to whether or not it has been tested for primality. Use appropriate information hiding techniques. Represent the list of numbers to be tested as an array of PrimeCandidate and the list of known primes as an ArrayList. Create a suitable user interface to demonstrate your practical, it is only necessary to output the final list of prime numbers and not to show each step in the calculation. This is What I have done and is working. public class InitArray { public static void main(String args[]) { if(args.length != 3) System.out.println("Please re-enter the entire command, including\n" + "array size, initial value and increment."); else { int arrayLength = Integer.parseInt(args[0]); int array[] = new int[arrayLength]; int initialValue = Integer.parseInt(args[1]); int increment = Integer.parseInt(args[2]); for(int counter = 0;counter < array.length;counter++) array[counter] = initialValue + increment * (counter + 2); System.out.printf("%s%8s\n", "Index", "Value"); for(int counter = 0;counter < array.length;counter++) System.out.printf("%5d%8d\n", counter,array[counter]); } } } My problem is I don't know how to add the number beacuse I am entering the number I wish and is not suppose to be like that I have to declare a list of prime numbers ,I don't know how to dao that and also I don't undrstand the fith statement about the squaroot I don't know how to do that please help me. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Prime numbers using arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|