Sum of Prime Numbers in Python
What is Prime Number and its History:
Natural numbers, known as prime numbers, can be divided only by themselves, by one, and by one. In other words, primes are positive numbers greater than one with only two factors: the number and the amount. Prime numbers include 2, 3, 5, 7, 11, and 13. Remember, 1 is neither a prime nor a composite number. Apart from 1, the remaining figures can all be categorized as prime and composite numbers. All primes are odd, except 2, which is regarded as the smallest positive integer and the only sometimes prime number. Composite numbers are those that include more than two factors.
Eratosthenes was the first to discover the prime number (275-194 B.C., Greece). He used the metaphor of a sieve to separate the composite numbers from the list of positive integers and separate the prime numbers. To practice this technique, students can write the positive integers from 1 to 100, circle the prime numbers, and cross out composite numbers. This type of activity alludes to the Eratosthenes Sieve.
Properties:
- Each number larger than 1 has at least each prime number that can be divided by.
- Any positive integer greater than can be represented by combining two prime numbers.
- Other than 2, all prime numbers are odd. In other words, we can say that there is only the same between prime numbers, which is 2.
- The two main numbers always have a coprime relationship.
- Each composite number's prime factors have been identified, and they are all unique in their own right.
The user must take the following actions to print every prime number between the specified range:
- The range of elements is looped through multiple times.
- See if each number has a factor that is around 1 and itself.
- If the answer is positive, the percentage is not prime, and the process moves on to the following.
- If the response is negative, the program will print the positive number and look for the next.
- When it reaches the upper value, the loop will end.
Let us now consider the following program code demonstrating the same.
CODE:
lower_value = int(input ("Enter the Lowest Range Value: "))
# Printing the user input for the lowest value.
upper_value = int(input ("Enter the Upper Range Value: "))
# Printing the user input for the Upper value.
print ("The Prime Numbers in the range are: ")
for num in range (lower_value, upper_value + 1):
if num > 1:
for i in range (2, num):
if (num % i) == 0:
break
else:
print (num)
OUTPUT:
Enter the Lowest Range Value: 2
Enter the Upper Range Value: 20
The Prime Numbers in the range are:
2
3
5
7
11
13
17
19
Program for checking whether a number is prime or not:
CODE:
def PrimeChecker(a):
# Checking that the given number is more than 1
if a > 1:
# Iterating over the given number with for loop
for j in range(2, int(a/2) + 1):
# If the given number is divisible or not
if (a % j) == 0:
print(a, "is not a prime number")
break
# Else it is a prime number
else:
print(a, "is a prime number")
else:
print(a, "is not a prime number")
a = int(input("Enter an input number:"))
PrimeChecker(a)
OUTPUT:
Enter an input number:109
109 is a prime number
Program for the sum of prime numbers with a given range:
CODE:
lower_value=2
upper_value = int(input ("Enter the Upper Range Value: "))
# Printing the user input for the Upper value.
print ("The prime numbers in range are: ")
# ans=sum(num)
for num in range (lower_value, upper_value + 1):
if num > 1:
for i in range (2, num):
if (num % i) == 0:
break
else:
print(num)
def sumOfPrimes(n):
# list to store prime numbers
prime = [True] * (n + 1)
p = 2
while p * p <= n:
# If prime[p] is not changed, then
# it is a prime
if prime[p] == True:
# Update all multiples of p
i = p * 2
while i <= n:
prime[i] = False
i += p
p += 1
sum = 0
for i in range (2, n + 1):
if(prime[i]):
sum += i
return sum
n=num
print("The sum is:")
print(sumOfPrimes(n))
OUTPUT:
Enter the Upper Range Value: 20
The prime numbers in range are:
2
3
5
7
11
13
17
19
The sum is:
77