Computer Science: An Overview: Global Edition (12th Edition)

Published by Pearson Higher Education
ISBN 10: 1292061162
ISBN 13: 978-1-29206-116-0

Chapter 5 - Algorithms - Chapter Review Problems - Page 267: 51

Answer

See explanation

Work Step by Step

# Target product TARGET = 2424 # Range of numbers numbers = list(range(1, 1001)) # Use a set for O(1) lookups num_set = set(numbers) pairs = set() # use a set to avoid duplicates for x in numbers: if TARGET % x == 0: # only if x divides the target y = TARGET // x if y in num_set and x <= y: # ensure y is in range and avoid reversed duplicates pairs.add((x, y)) # Print results print("Pairs whose product is 2424:") for p in sorted(pairs): print(p) print(f"\nTotal pairs found: {len(pairs)}") - The program loops through all numbers from 1 to 1000. - For each number x, it checks if x divides the target (TARGET % x == 0). - If so, it computes the partner y = TARGET // x. - If y is also in the list (here automatically within 1–1000), the pair (x, y) is stored. - The condition x <= y prevents duplicates like (4, 606) and (606, 4). Time complexity: $O(n) $ - one pass through the list, constant-time lookups in a set.
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.