How to Find Sum of all Primes below a Number

sum of primes

In this post I have written a program in Python which tries to find out the sum of all prime numbers below a number. I have implemented the solution in Python. This problem was originally posted on Project Euler and it asked to find the sum of all prime numbers below two Million. Seems exciting, right?

Recently I have been experimenting with various problems related to prime numbers and trying to implement in Python. I also came across Trinket.Io. This is a fabulous way to embed python interpreter in a browser for academic and learning purposes and it helps two causes:

  • An algorithms enthusiast like me who just wants to see how the algorithm works and how can we make it faster and better. Most likely I am interested to see the code but before that I am more likely to learn more about the efficiency of algorithm. So I can directly tweak the algorithm in Trinket in browser and see what I see in output.
  • A novice who is not aware of installing Python and just wants to see first how the specific programming problem can be solved using Python.

If you have Python terminal installed on your Unix or Python IDLE editor on Windows Machine, then make a new .py file and run in the terminal following command:
execfile("SumOfPrimeNumbers.py")

"""
In order to use this python code, just change the value of i.
"""
def main():
    summation=0
    x=[]
    i=2000000
    x=primes_upto(i)
    summation=sum(x)
    print summation

def primes_upto(limit):
    is_prime = [False] * 2 + [True] * (limit - 1)
    for n in range(int(limit**0.5 + 1.5)): # stop at ``sqrt(limit)``
        if is_prime[n]:
            for i in range(n*n, limit+1, n):
                is_prime[i] = False
    return [i for i, prime in enumerate(is_prime) if prime]


if __name__ == '__main__':
    main()

What if Python is Not Installed

Go to Repl.it and write the below code which I have customized so that you directly run the code and see the output.

"""
In order to use this python code, just change the value of i.
"""
def primes_upto(limit):
    is_prime = [False] * 2 + [True] * (limit - 1)
    for n in range(int(limit**0.5 + 1.5)): # stop at ``sqrt(limit)``
        if is_prime[n]:
            for i in range(n*n, limit+1, n):
                is_prime[i] = False
    return [i for i, prime in enumerate(is_prime) if prime]
summation=0
x=[]
i=7
x=primes_upto(i)
summation=sum(x)
print summation

Please let me know if the code works for you and it would be great if you could write your feedback in comments.

 

Read More Post

STAY CONNECTED

Most Popular

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know
17 Dec

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know

If you’re heading to Singapore for a few weeks or many months, maybe for work or study or just to

Manifesting Your Dream Career: Finding Your Passion And Purpose
01 Aug

Manifesting Your Dream Career: Finding Your Passion And Purpose

Are you passionate about finding your dream career? Manifesting can help you align your career with your passions and purpose.

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service
27 Feb

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service

Finding a reliable auto service provider is paramount in the bustling world of vehicle maintenance and repair. That's where Carolina

Floratam: The Robust Grass Solution From Council Growers SOD
25 Jan

Floratam: The Robust Grass Solution From Council Growers SOD

Floratam grass, a popular St. Augustine variety, has become a go-to choice for homeowners and landscapers seeking a lush, durable

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair
25 Jan

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair

For vehicle owners, the braking system is among the most critical components for safety and performance. Carolina Mobile Autoservice, a

Safeguarding Your Property: Expert Water Damage Services By MBC Capital
24 Jan

Safeguarding Your Property: Expert Water Damage Services By MBC Capital

Water damage can significantly impact homes and businesses, often leaving a trail of destruction and disruption in its wake. Understanding