You are on page 1of 2

WEEK-4

(4.1)

Aim: Find the sum of all the prime numbers upto two million.

Program:

import math as m

sum=0

n=2000000

for n in range(2,n+1):

prime=True

for i in range(2,int(m.sqrt(n)+1)):

if n%i==0:

prime=False

break

if prime:

sum=sum+n

print("sum={}".format(sum))

Output:

========================= RESTART: E:/rs541/prime.py =========================

sum=207851932781152
(4.2)

Aim: Each new term in the Fibonacci sequence is generated by adding the previous two
terms.

By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,...

By considering the terms in the Fibonacci sequence whose values do not exceed four
million,

find the sum of the even-valued terms.

Program:

a=0;b=1;sum=0

while a<=4000000:

if a%2==0:

sum=sum+a

a,b=b,a+b

print("sum of even fibonacci numbers is",sum)

Output:

========================== RESTART: E:/rs541/fib.py ==========================

sum of even fibonacci numbers is 4613732

You might also like