Python Programing Lab EXERCISE - 4(a)

Aim: Write a program to find the sum of all primes below two million.

Description: 


                  A Prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 and that is not a prime number is called a composite number. Here, We are using a for loop and a break statement.
break: break statement terminates the loop and resumes execution at the next statement, just like the traditional break statement in C. The break statement can be used in both while and for loops. If we are using break statement inside a nested loop, it stops the execution of the innermost loop and start executing the next line of code after the block.

Algorithm:


Step1: Start 

Step2: Initialize sum = 0 and i = 2
Step3: Repeat Steps 4 to 13 while i < 2000000 
Step4: Initialize c = 0 
Step5: Check whether i is greater than 2 and i is divisible by 2 or not. If yes, goto Step6 else, goto                                                                                                                                                               Step7 
Step6: Set c = 1 and goto Step12 
Step7: Initialize j = 3 
Step8: Repeat Steps 9 to 11 while j <= int(i**0.5) 
Step9: Check whether i is divisible by j or not. If yes, goto Step10. Otherwise, goto Step11 
Step10: Set c = 1 and goto Step12 
Step11: Increment j by 2 
Step12: Check whether c is 0 or not. If yes, goto Step13 
Step13: Add i to the sum and store the result in sum 
Step14: Display sum 
Step15: Stop

Program:

print("To find sum of prime no's")
sum=0
for i in range(2,2000000):
    flag=0
    for j in range(2,i):
       if i%j==0:
          flag=1
       if flag==0:
           print(i)
           sum=sum+i
print sum





No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...