Question: Data Structures and Algorithms, there are 3 files associated with the assignment, the text of the second is copied below the instruction. I will post
Data Structures and Algorithms, there are 3 files associated with the assignment, the text of the second is copied below the instruction. I will post the other two as separate questions.
The objectives of this assignment are to:
- Gain an appreciation for the importance of algorithm analysis
- Gain a feel of the different performance characteristics
- Gain experience with improving the performance of algorithms
The zip file Asmt2.zip contains an eclipse project with solutions to three problems, one to generate an array of prime numbers, another to calculate the perfect power, and the last to perform a pair-wise election over a rank ordered set of ballots. All solutions have been set up with test cases in the main method (except that the Primes class currently has the last test case commented out). The test cases will time the execution of the case. All solutions have terrible performance problems as you will find out.
Step 1:
Run each solution and capture the output into text files. Name each output appropriately to identify the problem and indicate before time. Store each file in the project directory.
Step 2:
Improve the algorithms such that:
- The Primes will generate a list of 1,000,000 (yes one million) prime numbers in under 2.5s of elapsed execution time. You will need to uncomment the last test case.
- The Perfect Power completes all test cases in under .01s of elapsed execution time.
- The pair-wise election completes all test cases in under .01s of elapsed execution time.
Step 3:
Run each solution and capture the output into text files. Name each output appropriately to identify the problem and indicate after time. Store each file in the project directory.
File #2 PerfectPower
/** * @author dtsmith * * A utlity class to calculate the perfect power of an integer */ public class PerfectPower { public static void main(String[] args) { new TimeExec(new Runnable() { public void run() { System.out.println("Pth of 17 is " + PerfectPower(17)); } }, "Get Pth of 17", System.out).start(); new TimeExec(new Runnable() { public void run() { System.out.println("Pth of 625 is " + PerfectPower(625)); } }, "Get Pth of 625", System.out).start(); new TimeExec(new Runnable() { public void run() { System.out.println("Pth of 1024 is " + PerfectPower(1024)); } }, "Get Pth of 1024", System.out).start(); new TimeExec(new Runnable() { public void run() { System.out.println("Pth of 10000 is " + PerfectPower(10000)); } }, "Get Pth of 10000", System.out).start(); new TimeExec(new Runnable() { public void run() { System.out.println("Pth of 1073741824 is " + PerfectPower(1073741824)); } }, "Get Pth of 1073741824", System.out).start(); }
/** * Get the perfect power for a number. * @param x number for which to calculate the perfect power. */ public static int PerfectPower(int x) { int largestP = 1; for (int p = 1; p < x; p++) { for (int b = 1; b < x; b++) { if (Math.pow(b,p) == x) { largestP = p; } } } return largestP; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
