I have to count the sum of all prime numbers that are less than 1000 and do not contain the digit 3.
My code:
def primes_sum(lower, upper):
total = 0
for num in range(lower, upper 1):
if not num % 3 and num % 10:
continue
elif num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
total = num
return total
total_value = primes_sum(0, 1000)
print(total_value)
But still I don't have right result
CodePudding user response:
def primes_sum(lower, upper):
"""Assume upper>=lower>2"""
primes = [2]
answer = 2
for num in range(lower, upper 1):
if any(num%p==0 for p in primes): continue # not a prime
primes.append(num)
if '3' in str(num): continue
answer = num
return answer
The issue in your code was that you were checking for num%3
, which checks whether num
is divisible by 3, not whether it contains a 3
.