The claim "n² + n + 41 is prime for every n" is true for the first forty values of n. Your program searches for the first n in a given range that proves the claim false.
What to build
A program that searches n = 0, 1, ..., L for a counterexample to the claim.
Requirements
- Read L.
- For n from 0 to L, compute n*n + n + 41. At the first n for which the value is not prime, print
counterexample: n = N, value = Vand stop. - If every value is prime, print
no counterexample up to L, with the number L in place of the letter.
Input
One whole number L.
Output
One line.
Constraints
0 ≤ L ≤ 2000.
Example
Input:
10
Output:
no counterexample up to 10
Why it works
A "for all" claim is refuted by one failing case, so the search stops at the first one. Trial division up to the square root decides primality, and a long run of successes (here 40) is still not a proof.
Examples
10
no counterexample up to 10
50
counterexample: n = 40, value = 1681
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
A number is composite if some d from 2 up to its square root divides it.
Hint 2
Loop n from 0 up to L and stop at the first n for which n*n + n + 41 is not prime.
Hint 3
Print the no-counterexample message only if the loop ends without a break.
Solution
Show one correct solution (try it yourself first)
def is_prime(n):
if n < 2:
return False
d = 2
while d * d <= n:
if n % d == 0:
return False
d += 1
return True
L = int(input())
for n in range(L + 1):
v = n * n + n + 41
if not is_prime(v):
print(f"counterexample: n = {n}, value = {v}")
break
else:
print(f"no counterexample up to {L}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step