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.