Invitation to Computer Science 8th Edition

Published by Cengage Learning
ISBN 10: 1337561916
ISBN 13: 978-1-33756-191-4

Chapter 2 - Exercises - Page 88: 20

Answer

See explanation

Work Step by Step

The above function checks for all integer between 1 to n and check if that is a factor of n. If none of the number is found a factor of n then n must be prime. Algorithm Steps Input the positive integer N If $N\leq 1$, then Output “not prime” and stop Set a variable flag = 0 (to track if a factor is found) For each integer i from 2 to N ÷ 2 (inclusive), do a. If $N$ mod $𝑖=0$, then i. Output “not prime” ii. Output the message “Factor: i” iii. Set flag = 1 iv. Stop the loop If after the loop flag = 0, then Output “prime”
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.