Question
pdf file with more details: https://drive.google.com/open?id=18cdtjoTHtn3C98BWEUWDDNDJn-fwuHEA For centuries mathematicians has been fascinated by the natural (counting) numbers. Early on they discovered properties like whether a
pdf file with more details: https://drive.google.com/open?id=18cdtjoTHtn3C98BWEUWDDNDJn-fwuHEA
For centuries mathematicians has been fascinated by the natural (counting) numbers. Early on they discovered properties like whether a number is "prime" or "perfect". We all were taught that prime numbers are those that are only divisible by 1 and the number itself1. But what is a perfect number? A number is perfect if it is equal to the sum of its proper factors. A proper factor is a smaller number that evenly divides the number. Any natural number is divisible by itself (e.g. 6 is a factor of 6) making it a factor, but the number itself is considered an improper factor. Example: 6 has improper factors: 1, 2, and 3 (remember 6 is excluded). 6 is the first perfect number since 6 = 1 + 2 + 3 The goal of this program is to consider the numbers 2 through 30 (inclusive) and determine which are prime and which are perfect. Along the way you will learn about loops, arrays, and integer arithmetic. Let's start with a simple loop to generate the numbers: for (int number=2; number <= 30; number++) { System.out.printf("Number: %d",number); } So far so good. Now to determine if a number is perfect you must keep track of it factors. One way of keeping track of the factors is to store them in an array. Let's declare an array and initialize it to zeros. // declare the factors array and allocate space for values int [] factors = new int[30]; // at MOST 30 factors. // now initialize the array elements for (int i=0; i < 30; i++) { factors[i] = 0; } Now here's an outline for the entire program. for (int number=2; number <= 30; number++) { // print the number // initialize factors array to zeros (see above) // determine proper factors of number (this requires another loop) // print proper factors // determine the sum of the proper factors (another loop) // if number is prime (sum == 1) print "prime" // else if number is perfect (sum == number) print "perfect" } Let's analyze the step that determines the proper factors of the number being analyzed. This will require: 1) a counter and 2) a loop. The counter is required since we don't know how many factors an arbitrary number between 2 and 30 will have. All the primes will have only one proper factor, whereas non-primes will have a variable number of factors. Therefore, a counter variable is needed. Now for the loop to test the candidate factors. A for loop will do if we set up the proper initialization and test condition. 1 is a divisor or all numbers so, we have: for (int candidate=1; candidate < number; candidate++) { We test candidate < number because we want only proper factors. But do we have to test all the way up to number - 1 as candidates? No, we can stop at of the number inclusive: for (int candidate=1; candidate <= number / 2; candidate++) { Inside this loop is the test for divisibility. Is number evenly divisible by candidate? Please consider the modulus operator which returns the remainder following an integer division. If the answer is "yes,' then you must store the candidate in the factors array and increment that counter variable we discussed earlier. You are on your own for the rest of the program. Your output should look like this: Number: 2 Factors: 1 2 is prime! Number: 3 Factors: 1 3 is prime! Number: 4 Factors: 1 2 Number: 5 Factors: 1 5 is prime! Number: 6 Factors: 1 2 3 6 is perfect! . . . Number: 30 Factors: 1 2 3 5 6 10 15 Hint: There is another perfect number in this range. Assignment Create a new Java code which contains the main() method. Now turn the pseudo code above into statements within main that produce the desired output.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started