Answered step by step
Verified Expert Solution
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 which takes a positive integer 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 are and because Note that is not a prime factor because it is not prime, and is not a prime factor because it is not a factor of
Prime factors can be repeated when the same factor divides the original number multiple times; for example, the prime factors of are and because and are both prime and The prime factors of are and because We'll display repeated factors on a single line as a power expression; for example, would display because is repeated four times.
Here's a highlevel 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 to inclusive
a If is evenly divisible by the possible factor
i Set a number count to be
ii Repeat the following procedure until is not divisible by the possible factor
Set count to be count plus
Set to divided by the factor
iii. If the number count is exactly
Print the factor by itself
iv If the number count is greater than
Print where is the factor and is the count
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