Python Set Comprehension
Asked Answered
S

3

102

So I have these two problems for a homework assignment and I'm stuck on the second one.

  1. Use a Python Set Comprehension (Python's equivalent of Set Builder notation) to generate a set of all of the prime numbers that are less than 100. Recall that a prime number is an integer that is greater than 1 and not divisible by any integer other than itself and 1. Store your set of primes in a variable (you will need it for additional parts). Output your set of primes (e.g., with the print function).

  2. Use a Python Set Comprehension to generate a set of ordered pairs (tuples of length 2) consisting of all of the prime pairs consisting of primes less than 100. A Prime Pair is a pair of consecutive odd numbers that are both prime. Store your set of Prime Pairs in a variable. Your set of number 1 will be very helpful. Output your Set of Prime Pairs.

For the first one, this works perfectly:

r= {x for x in range(2, 101) 
if not any(x % y == 0 for y in range(2, x))} 

However, I'm pretty stumped on the second one. I think I may have to take the Cartesian product of the set r with something but I'm just not sure.

This gets me somewhat close but I just want the consecutive pairs.

cart = { (x, y) for x in r for y in r
     if x < y }
Subcontinent answered 14/2, 2014 at 4:29 Comment(0)
C
91
primes = {x for x in range(2, 101) if all(x%y for y in range(2, min(x, 11)))}

I simplified the test a bit - if all(x%y instead of if not any(not x%y

I also limited y's range; there is no point in testing for divisors > sqrt(x). So max(x) == 100 implies max(y) == 10. For x <= 10, y must also be < x.

pairs = {(x, x+2) for x in primes if x+2 in primes}

Instead of generating pairs of primes and testing them, get one and see if the corresponding higher prime exists.

Correll answered 14/2, 2014 at 4:52 Comment(0)
W
17

You can get clean and clear solutions by building the appropriate predicates as helper functions. In other words, use the Python set-builder notation the same way you would write the answer with regular mathematics set-notation.

The whole idea behind set comprehensions is to let us write and reason in code the same way we do mathematics by hand.

With an appropriate predicate in hand, problem 1 simplifies to:

 low_primes = {x for x in range(1, 100) if is_prime(x)}

And problem 2 simplifies to:

 low_prime_pairs = {(x, x+2) for x in range(1,100,2) if is_prime(x) and is_prime(x+2)}

Note how this code is a direct translation of the problem specification, "A Prime Pair is a pair of consecutive odd numbers that are both prime."

P.S. I'm trying to give you the correct problem solving technique without actually giving away the answer to the homework problem.

Wolfson answered 14/2, 2014 at 5:5 Comment(1)
Although problem 2 can be simplified to just looping over the result frim problem 1, as hinted in the instructions.Oceania
B
7

You can generate pairs like this:

{(x, x + 2) for x in r if x + 2 in r}

Then all that is left to do is to get a condition to make them prime, which you have already done in the first example.

A different way of doing it: (Although slower for large sets of primes)

{(x, y) for x in r for y in r if x + 2 == y}
Barbarbarbara answered 14/2, 2014 at 4:37 Comment(4)
For some reason, I don't see which ones are missing. I get set([(29, 31), (59, 61), (5, 7), (71, 73), (41, 43), (3, 5), (17, 19), (11, 13)]). Which pairs are missing? You already applied the condition (of being prime) to r, so the code should be okay.Barbarbarbara
No you're absolutely right. I was under the impression (7,11) and (13,17) etc... were included in the prime pairs since they were technically "consecutive" in the list of primes. But now I understand.Subcontinent
Get rid of the "different way" it's got quadratic performance, and is harder to readEdirne
I think doing it that way is good because it's similar to how the poster attempted their solution, and would help the poster understand what they needed to do. But you are right that it is slower, and I have added that in as a note.Barbarbarbara

© 2022 - 2024 — McMap. All rights reserved.