Question: In JAVA Please. These are the directions First we ll create a class called Prime. If you are a java student import java.util.LinkedList, then create
In JAVA Please. These are the directionsFirst well create a class called Prime. If you are a java student import java.util.LinkedList, then create a linked list of Integers. If you are a C# student, using System, System.Collections.Generic and Systems.Linq, then make a LinkedList of int. Write a constructor which takes in an integer called max and calculates all primes up to max. Start off with an array of booleans of the max size. Initialize all cells to true You are going to use this array of booleans to find the prime numbers. The index of the cell in the array represents the number you are deciding if its prime or not. A true value in the cell indicates that index is prime, a false value indicates its not. Follow the algorithm described above. Youll ignore cells and of the array, because those arent prime numbers. Using a loop, start at and continue until you reach the square root of max Change the value of each cell to false when you determine its not a prime. Once youve completed the algorithm all cells still true are prime. Add all prime numbers to the linked list: Java students, can use add C# students can use AddFirst Write a method isPrime which takes in a number and returns true if its found anywhere in the LinkedList. Using a foreach loop, iterate over the LinkedList. If the number passed in is found in the list, return true, otherwise false. Write a method getPrime which takes a position in the linked list and returns that value. For example, if passed it should return the first prime if passed it would return the next prime if passed it would return the rd prime Note: Java students can use get on a linked list. C# students will need ElementAt ElementAt requires that you are using System.Linq Next in your Main class: Create a constant and set it to Write a method findFactor which takes in a target int and a Prime object. Use a loop starting at index of the linked list and going up to the target. Find the first prime number using getPrime in the Prime object that evenly divides into the target. Return that number. For example, if passed it would return since is the first prime number that divides into evenly. If passed it would return since doesnt divide evenly, but does. If passed itll return If you cant find a value, return this would only happen if the method were passed a number Print out What primes make up followed by the number you picked. Using a loop: Call findFactor on the target number, which will return the lowest prime factor. Print that factor followed by a plus, unless its the last factor Divide the target by the returned factor Repeat calling findFactor on the smaller values until you are left with a number less than Sample Output:NOTE: Each time it is run, youll get a different random outputFirst RunWhat primes make up xxxSecond RunWhat primes make up xThird RunWhat primes make up xxxx
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
