What can be the problem?


#1

Hy All,
I've used maybe a bit stone-age way to solve this, but here is my code.

def is_prime(x):

if x<2:
    return False
if x==2:
    return True
if x%2==1:
        return True
return False

the problem is that for 9 it says:

Oops, try again. Your function fails on is_prime(9). It returns True when it should return False.

9 is prime, so what can be the problem?

Thanks in advance for any help


Is_prime
#2

9 is not a prime number. A prime number only has 1 and itself as divisors. But 9 can be divided by 3 hence it's a composite number.

Your code there seems to be looking for odd numbers.


#3

Yes, I've realized meanwhile. It's 3:26 am here, and I'm getting weak. :smiley:


#4

This assumes that all odd numbers are Primes, which we know they are not. At most only about a sixth of all numbers are Primes (but don't hold me to this). Every odd number has odd multiples. There is an infinite number of multiples of 3, 5, 7, etc., to name just three odd Primes.


#5

There are a few steps you must do.
Make sure it is a positive number [DONE]
eliminate the number 1 as False [DONE]
eliminate the number 2 as True [DONE]
eliminate all of the other even numbers [DONE]
eliminate all possible odd numbers by using range starting at position 2 for the length of x. you must test if any numbers in the range of x will multiply evenly into x.[NOT DONE] Note: range could start at position 1 but you have already eliminated the even numbers.

I hope this didn't give too much away.


#6

Only the first of these is necessary. The loop will catch all the rest.


#7

It is an interesting issue.

For the interval 1 to 1000, the fraction of numbers that are prime is indeed about 1/6. However, the primes within the neighborhood of an integer, x, become less common with increasing x. The Prime number theorem deals with this issue. See Wikipedia: Prime number theorem.

Since the fraction of numbers that are prime keeps decreasing with increasing x, but never reaches 0, there is no answer to the question regarding what fraction of all numbers are prime. You can, however, use a formula to approximate the fraction for a given x.


#8

Which makes perfect sense, as the more numbers we have, the more multiples that exist within that set.

This I imagine is a bit tricky, unless we actually generate the Primes for the given range and take a ratio. Is that what we would do?

Edit. Just read the page on Wikipedia, and see what you mean. the asymptotic law of distribution of prime numbers.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.