Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the algorithm shown below, write the function printPrimeFactors ( x ) which takes a positive integer x and prints all of its prime factors

Using the algorithm shown below, write the function printPrimeFactors (x) which takes a positive integer x and prints all of its prime factors in a nice format.
A prime factor is a number that is both prime and evenly divides the original number (with no remainder). So the prime factors of 70 are 2,5, and 7, because 2**5**7=70. Note that 10 is not a prime factor because it is not prime, and 3 is not a prime factor because it is not a factor of 70.
Prime factors can be repeated when the same factor divides the original number multiple times; for example, the prime factors of 12 are 2,2, and 3, because 2 and 3 are both prime and 2*2*3=12. The prime factors of 16 are 2,2,2, and 2, because 2*2*2**2=16. We'll display repeated factors on a single line as a power expression; for example, 16 would display 2*****4, because 2 is repeated four times.
Here's a high-level algorithm to solve this problem. Start by iterating through all possible factors. When you find a viable factor, repeatedly divide the number by that factor until it no longer evenly divides the number. Our algorithm looks something like this:
Repeat the following procedure over all possible factors (2 to x, inclusive)
a. If x is evenly divisible by the possible factor
i. Set a number count to be 0
ii. Repeat the following procedure until x is not divisible by the possible factor
Set count to be count plus 1
Set x to x divided by the factor
iii. If the number count is exactly 1
Print the factor by itself
iv. If the number count is greater than 1
Print "f*****c", where f is the factor and c is the count
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions