Sum of Primes

Back to homepage

Project Overview

Write a program that prints the sum of all prime numbers under 1000. This is one of those challenges that has a "correct" answer (76127), so as long as your program gets the right answer you're good. But nice looking code is obviously better.

This challenge started 2020 Oct 27 and ended 2020 Nov 03.

Submissions

Kat Crawford

I had to modify this submission a bit because the original didn't print the total at the end.

start = 1
end = 1000
total = 0

for a in range(start,end):
  if (a > 1):
    for b in range(2,a):
        if (a % b==0):
            break
    else:
        print(str(a))
        total += a
print(str(total))

Benjamin Lowry

def is_prime(n):
    if n == 1:
        return False # special case needed because 1 isn't a prime
    i = 1
    while i < n:
        i += 1
        if n % i == 0 and n != i:
            return False
    print(str(n))
    return True

if __name__ == "__main__":
    total = 0
    for x in range(1, 1000):
        if is_prime(x):
            total += x
    print(str(total))

Will Stark