0

I was trying to find the prime numbers within a range (input will be decided by user) and total count of prime numbers.

I am able to find the prime numbers but not able to do the part--counting the prime numbers. Can any one help me? here is the program

#!/usr/bin/env python
import math

lower=input("lower value:")
upper=input("upper value:")
print("prime numbers between", lower,"and", upper, "are:")
for num in range (lower, upper+1):
 if num>1:
  for i in range (2,num):
   if num % i==0:
    break
  else: print(num)
def  count_prime(num,lower,upper):
  count_prime= 0
  for x in num:
    if lower <= x <= upper:
      count_prime += 1
print ( "count_prime:",count_prime )

result output is as follows ( range between 10 and 100)

('prime numbers between', 10, 'and', 100, 'are:') 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 ('count_prime:', )

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • please see this link https://stackoverflow.com/questions/11619942/print-series-of-prime-numbers-in-python?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – anandsp Jun 10 '18 at 06:57
  • 1
    Increment a counter every time you print prime number. Or instead of printing straight away, store prime numbers to a list and find `len(lst)` to count primes within range. – Austin Jun 10 '18 at 07:00

4 Answers4

4

Easiest way would be to have a variable that increments every time you find a prime number. So when you are printing the number, also increment that count by one. You could do it in various other ways, i.e. by saving each prime number in an array then counting number of elements etc.

To do with variable:

lower=input("lower value:")
upper=input("upper value:")
count = 0
print("prime numbers between", lower,"and", upper, "are:")
for num in range (lower, upper+1):
 if num>1:
  for i in range (2,num):
   if num % i==0:
    break
  else: 
    print(num)
    count += 1
print ( "count_prime:", count )

To do with array:

lower=input("lower value:")
upper=input("upper value:")
array = []
print("prime numbers between", lower,"and", upper, "are:")
for num in range (lower, upper+1):
 if num>1:
  for i in range (2,num):
   if num % i==0:
    break
  else: 
    print(num)
    array.append(num)
print ( "count_prime:", len(array))
Jesse
  • 1,814
  • 1
  • 21
  • 25
  • result is ('the number of prime numbers are:', 91) for both programs. – Debasish Chowdhury Jun 10 '18 at 07:15
  • 1
    Your logic needs fixing, but the question was not about the logic, the question was "how can I count the my program outputs is prime". The above two methods are correct :) Both will out put ('count_prime:', 21) for the example between 10 and 100 in your use case above. – Jesse Jun 10 '18 at 07:19
  • yes if I copy your code it works fine, i am just trying to understand what eror I am doing while correcting my previous program as per your suggestion. – Debasish Chowdhury Jun 10 '18 at 07:25
  • give me some time. Thanks for your suggestion, i am sure I will be able to fix my own program own self, that will give me more confidence. – Debasish Chowdhury Jun 10 '18 at 07:26
1

To count the number of primes, you can use a list to store the prime numbers first and then just use len to find the number of prime numbers. It's pretty easy to understand this:

import math
store = []
lower=int(input("lower value:"))
upper=int(input("upper value:"))
print("prime numbers between", lower,"and", upper, "are:")
for num in range (lower, upper+1):
 if num>1:
  for i in range (2,num):
   if num % i==0:
    break
  else: 
    print(num)
    store.append(num)
print("The number of prime numbers are:",len(store))
Mahir Islam
  • 1,941
  • 2
  • 12
  • 33
1

"sympy" library will be helpful in these situations

import sympy
lower=int(input("lower value:"))          #let it be 30
upper=int(input("upper value:"))          #let it be 60
l=list(sympy.primerange(lower,upper+1))   #[31,37,41,43,47,53,59]
z=len(l)
print(z)
0

I found some other method to count the prime number found at https://stackoverflow.com/a/53451147/4683899.In this example you can customize your list.

def is_prime(n):
    if n <= 1:          # negative numbers, 0 or 1
        return False
    if n <= 3:          # 2 and 3
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False

    for i in range(5, int(math.sqrt(n)) + 1, 2):
        if n % i == 0:
            return False

    return True



ans = [i for i in input().split() if is_prime(int(i))]
print(len(ans))

or if you want to accomplish the same task in range then just edit the input().split() to range(lower,upper+1)

ans = [i for i in range(lower,upper+1) if is_prime(i)]
print(len(ans))
raunak rathi
  • 95
  • 1
  • 9