-1

So, I've made this code:

y = 10
prime_num = [x for x in range(1,y) if y%x != 0]

It's returned me:

[3, 4, 6, 7, 8, 9]

The purpose of this code is to generate prime numbers within 1 to 10, it returned me an odd number which I do not quiet get. I was expecting this to work since my list comprehension was an attempted replica of:

def is_prime(x):
    if x < 2:
        return False
    else:
        for n in range(2, x-1):
            if x % n == 0:
                return False
        return True

Which I've created from codecademy, I've simply changed "x%n == 0" to "x%n != 0".

Blueboy90780
  • 145
  • 1
  • 12
  • All your comprehension is doing is creating a list of the numbers from 1 to y-1 that are not factors of y. When y is 10, that would be 3, 4, 6, 7, 8, 9. Not even close to primes, nor is it close to your `is_prime` function. – Tom Karzes Nov 17 '17 at 07:50

4 Answers4

1

I don't think your code follows the given code from codeacacdemy .This is somewhat closer to the code from codeacademy

y = 10
factor_array = [x for x in range(2,y) if y%x == 0 ]
if not factor_array :
  print("prime")
else : 
  print ("composite")
UchihaItachi
  • 2,602
  • 14
  • 21
  • This appears to check for primality of `y`, not "Generate a list of prime numbers using list comprehension" as written in the title of the question. – Adam Smith Nov 17 '17 at 07:53
  • @AdamSmith , The code which on which he basing upon the question is actually a primality test too. I have gone by the code instead of the header – UchihaItachi Nov 17 '17 at 07:55
  • The code that I mentioned wasn't an official code, it was the one that I've used to pass one of their exercise. – Blueboy90780 Nov 17 '17 at 09:21
0

From your question it is unclear what the problem is and what kind of tools are you expecting to be used.

If you want to get to a list of the first n with a list comprehension only, I am afraid this is not extremely efficient as there is no straightforward way to get to know how many objects are going to be generated beforehand.

If instead you want to obtain the primes in a given range, say below some number num, this can be achieved (more or less efficiently) by:

num = 10
primes = [
    x for x in range(2, num)
    if all([x % f != 0 for f in range(2, x - 1)])]

However, if you define a function for primality test (like is_prime()), you can achieve better performances for large numbers.

In that case, the code would read:

num = 10
primes = [x for x in range(2, num) if is_prime(x)]

Note though that the is_prime() implementation you provide is not particularly efficient.

Also, there are might be more efficient ways of achieving this with list comprehension by addressing some corner cases directly, e.g.:

num = 10
primes = ([] if num < 2 else [2]) + [x for x in range(3, num, 2) if is_prime(x)]
norok2
  • 25,683
  • 4
  • 73
  • 99
0

Your code only cares if the possible prime x evenly divides 10. That's not a test of primality.

cap = 10
prime_nums = [2] + [x for x in range(3, cap+1, 2) if
                    all(x%z!=0 for z in range(2, int(x**(0.5))+1))]

Your prime testing here is:

all(x % z != 0 for z in range(2, int(x**(0.5))+1))
#   ^ not divisible by anything in the range of...
#                       ^ [2..sqrt(x)]
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

What your code currently does

[x for x in range(1,y) if y%x != 0]

You iterate through the range [1, y) and check if y is not divisible by x. Your code currently generates [1, y) \ Divisors(y), which is clearly not what you want. Instead, you want to a perform primality test on x, and how you shall do so is described below.

What you should do

You should change your list comprehension a bit, because you actually never use is_prime:

prime_nums = [x for x in range(2, y + 1) if is_prime(x)]

By the way, you can improve the efficiency of the primality test significantly by visiting this thread.

If you want to avoid your is_prime() function, you can use the following code:

prime_nums = [x for x in range(2, y + 1) if all(x%k for k in range(2, x-1))]
Community
  • 1
  • 1
Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
  • yes but what if I wanted to do this without the use of functions? From what I can see in my function, it returns False if x%n == 0, so the inverse of that would be x%n != 0: My code: [x for x in range(2,y) for y in range(2, x) if x%y != 0] – Blueboy90780 Nov 17 '17 at 09:53
  • @Blueboy90780 Well I cannot read minds, you should state your requests better. Anyway, If you want to avoid `is_prime`, you can use `prime_nums = [x for x in range(2, y + 1) if all(x%k for k in range(2,x))]`. You cannot just use `x%n != 0` because in your function you also iterate through a range. – Mr. Xcoder Nov 17 '17 at 09:54