A famous theorem says that if a prime p divides a product a × b, then p divides a or p divides b. The statement is false for composite numbers, and your program finds a counterexample for each n up to N.
What to build
A program that tests the claim "if n divides a × b then n divides a or n divides b" for every n from 2 to N.
Requirements
- Read N.
- For each n from 2 to N, look for the first pair (a, b) with 1 ≤ a ≤ b < n, searching a upward and then b upward, such that n divides a × b but n divides neither a nor b.
- If such a pair exists print
n: fails with a=A b=B. Otherwise printn: holds.
Input
One whole number N.
Output
One line for each n from 2 to N.
Constraints
2 ≤ N ≤ 40.
Example
Input:
10
Output:
2: holds
3: holds
4: fails with a=2 b=2
5: holds
6: fails with a=2 b=3
7: holds
8: fails with a=2 b=4
9: fails with a=3 b=3
10: fails with a=2 b=5
Why it works
For a prime n the claim is a theorem, so no pair exists. For a composite n = s × t with s, t > 1 the pair a = s, b = t is a counterexample, because s × t is divisible by n but neither s nor t is. A search finds the smallest such pair, and one pair disproves the claim for that n.
Examples
10
2: holds 3: holds 4: fails with a=2 b=2 5: holds 6: fails with a=2 b=3 7: holds 8: fails with a=2 b=4 9: fails with a=3 b=3 10: fails with a=2 b=5
3
2: holds 3: holds
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
For each n, try a from 2 up to n - 1 and b from a up to n - 1, in that order, and stop at the first pair that works.
Hint 2
The pair works when (a * b) % n == 0 while a % n != 0 and b % n != 0.
Hint 3
If no pair works, the claim holds for that n: print holds.
Solution
Show one correct solution (try it yourself first)
N = int(input())
for n in range(2, N + 1):
found = None
for a in range(1, n):
for b in range(a, n):
if (a * b) % n == 0 and a % n != 0 and b % n != 0:
found = (a, b)
break
if found:
break
if found:
print(f"{n}: fails with a={found[0]} b={found[1]}")
else:
print(f"{n}: holds")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step