(Challenge) Write an algorithm to compute the list of divisors of that are prime numbers.
(Hint: Compute and then filter out the primes in this list.)
Confirming your payment
The World of Algorithms · Question 14 of 15
(Challenge) Write an algorithm to compute the list of divisors of that are prime numbers.
(Hint: Compute and then filter out the primes in this list.)
Algorithm primedivisors(n)
Let divisors-of-n be the list obtained by computing divisors(n).
Start with an empty list prime-divisors.
For each number in divisors-of-n: if prime() reports that is prime, add to prime-divisors.
Report prime-divisors.
Here prime() is the algorithm of the previous question (divisors() has exactly 2 elements). Since divisors-of-n is in increasing order, prime-divisors is also in increasing order.
Executing primedivisors(180):
divisors(180)
Checking each element: 1 is not prime (one divisor); 2, 3 and 5 are prime; 4, 6, 9, 10, 12, 15, 18, 20, 30, 36, 45, 60, 90 and 180 each have more than two divisors, so they are not prime.
So primedivisors(180) .
Another check: primedivisors(84) , since .