-2

So I'm new to python coming from a java background and it's hard for me to get use to python's syntax and scoping and loops etc. This was a question my instructor asked and I'm lost to what I'm doing wrong

Q. write a function or just use some if-else statement to figure out how many prime numbers are there between 100 and 200

def isPrime(num):
    if num > 1:
        for i in range(2, num):
            if(num % i) == 0:
                return True

def primeNumbers():
    count = 0

    for num in range(100, 200):
        prime = isPrime(num)
        if prime == True:
            count += 1
    
    print(count)

primeNumbers()
Selcuk
  • 57,004
  • 12
  • 102
  • 110
Qube
  • 13
  • 1
  • 5
  • What is the *actual* and *specific* question / issue? There is a "Q" and some presented code.. perhaps https://codereview.stackexchange.com/ might be more suitable if just looking for feedback. – user2864740 Oct 21 '21 at 00:40
  • Question: are you sure it's necessary to use `range(2, num)`? Do you think you could use a smaller range and still get the right answer? – Z4-tier Oct 21 '21 at 00:41
  • 1
    There are non-Pythonic idioms in the code but this is a logic issue. isPrime is wrong. What happens when i == 2? The function returns True. Is that really what you mean? – Passerby Oct 21 '21 at 00:43
  • _I'm lost to what I'm doing wrong_ Tell us what the code is doing that you believe is wrong. Do you get an error? Is a certain number identified as prime when it shouldn't be? – John Gordon Oct 21 '21 at 00:45
  • A simple implementation would be to use a sieve of eratosthenes up to 200 and up to 100 and subtract – neuops Oct 21 '21 at 00:45
  • Does this answer your question? [How to find prime numbers in python](https://stackoverflow.com/questions/62564607/how-to-find-prime-numbers-in-python) – Z4-tier Oct 21 '21 at 00:50
  • The function returns true when the number IS divisible by another number, which is the opposite of what you want. – John Gordon Oct 21 '21 at 00:50

3 Answers3

2

Basically your isPrime function is wrong. It returns True when the number is divisible (i.e. not prime), and never returns False. A fixed version would be:

def isPrime(num):
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                return False
        return True
    else:
        return False

That being said, a more Pythonic (and PEP-8 compliant) version of your program could be:

def is_prime(num):
    if num > 1:
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                return False
        return True
    else:
        return False


def prime_numbers(start, end):
    count = 0
    for num in range(start, end):
        if is_prime(num):
            count += 1
    return count


print("The number of prime numbers between 100 and 200 is", prime_numbers(100, 200))
Selcuk
  • 57,004
  • 12
  • 102
  • 110
1

Your program is actually returning the count of numbers that are not prime.
The isPrime function is return True if the number is divisible by by a number between 2 and num. The function should be:

def isPrime(num):
    if num > 1:
        for i in range(2, round(num**0.5)+1): # No need to search up to num
            if(num % i) == 0:
                return False
        return True
    return False

Or print 100 - count in your primeNumbers function.

You could also check out the sieve of Eratosthenes

0

In isPrime(num)

def isPrime(num):
    if num > 1:
        for i in range(2, num):
            if(num % i) == 0: # <-- this condition
                return True # <-- this return value

Notice that if(num % i) == 0: checks that if the remainder of num divided by i is 0, meaning that you want to check that if i is divisible by num. But if that condition is true, then num is not a prime, so you have to return False. And if that condition is False for every i in range(2, num), then you can be assured that num is a prime.

A little thing I want to point out is that if i is a divisor of num then num/i is also a divisor. So in reality you only want to check about from 2 to sqrt(num) or num ** 0.5

Suggested code:

def isPrime(num):
    if num > 1:
        for i in range(2, int(num ** 0.5)):
            if(num % i) == 0:
                return False
        reuturn True
    else:
        return False

p/s: As others have pointed out, you shouldn't really use the normal prime testing in this project, instead, use the Sieve of Eratosthenes