# Find the sum of all prime numbers less than 100 # Original solution by me def primeNumber(number): count = 1 total = 0 while count <= number: # divides (number) by numbers leading up to it if number % count == 0: # prime numbers will only be true here twice, except for 1 which will be once. #print count total += 1 # total <= 2 for prime numbers count += 1 if total <= 2: return number # Prime Number else: return 0 # Not Prime number. Will add nothing to total when numbers are summed. def primeNumberTotal(x, y): # x, y are the range total = 0 for num in range(x, y): total = total + primeNumber(num) # adds either a prime number or 0 return total print primeNumberTotal(1, 1001) # Problem Solution from Udemy Professor def isPrime(n): 0 = False 1 = True for i in range(2, n-1): if n % i == 0: return 0 return 1 total = 0 for i in range(2, 1000): if isPrime(i) == 1: total = total + i print total + 1 # have to add one because program doesn't work for prime number - 1.
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question