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.
Related Posts:
- Append values to a set in Python
- Does Python have an ordered set?
- Does Python have an ordered set?
- Python set to list
- How to sort a set in python?
- Empty set literal?
- Add list to set?
- Creating 2D dictionary in Python
- Python Sets vs Lists
- In Python, when to use a Dictionary, List or Set?
- How to get all subsets of a set? (powerset)
- Type error Unhashable type:set
- Why is there no tuple comprehension in Python?
- Why is there no tuple comprehension in Python?
- How to make a set of lists
- TypeError: ‘set’ object does not support indexing
- How do I add two sets?
- set object is not JSON serializable
- Get difference between two lists
- Converting a list to a set changes element order
- ValueError: invalid literal for int() with base 10: ”
- Print string to text file
- How can I parse a YAML file in Python
- Cannot find module cv2 when using OpenCV
- Maximum and Minimum values for ints
- How is Anaconda related to Python?
- What is the python keyword “with” used for? [duplicate]
- What is the purpose of meshgrid in Python / NumPy?
- How to add to the PYTHONPATH in Windows, so it finds my modules/packages?
- FileNotFoundError: [WinError 2] The system cannot find the file specified:
- Remove all whitespace in a string
- What is a “method” in Python?
- How to generate all permutations of a list?
- Is there a Python equivalent to Ruby’s string interpolation?
- Pandas group-by and sum
- Python List vs. Array – when to use?
- How to terminate a script?
- numpy array concatenate: “ValueError: all the input arrays must have same number of dimensions”
- csv.Error: iterator should return strings, not bytes
- SyntaxError: non-default argument follows default argument
- socket.error: [Errno 48] Address already in use
- Creating a basic auto clicker in python
- What’s the difference between torch.stack() and torch.cat() functions?
- proper name for python * operator?
- How to do superscripts and subscripts in Jupyter Notebook?
- How do I remove all packages installed by PIP?
- ValueError: need more than 1 value to unpack python
- What is the best way to exit a function (which has no return value) in python before the function ends (e.g. a check fails)?
- How to disable Python warnings?
- LinAlgError: Last 2 dimensions of the array must be square
- How to get data from SNMP with python?
- What does axis in pandas mean?
- Dynamically growing a python array when assigning to it
- “OverflowError: Python int too large to convert to C long” on windows but not mac
- Can’t compare naive and aware datetime.now() <= challenge.datetime_end
- List of zeros in python
- Displaying better error message than “No JSON object could be decoded”
- List attributes of an object [duplicate]
- How can I set the aspect ratio in matplotlib?
- In Python how should I test if a variable is None, True or False
- Pythonic way to iterate through a range starting at 1
- Convert from CSV to array in Python
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
- Relative paths in Python
- Convert bytes to int?
- SQLAlchemy ORDER BY DESCENDING?
- ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
- Relative paths in Python
- TypeError: ‘bool’ object is not callable
- How do I maximize the display screen in PyGame?
- Why does PyQt crashes without information? (exit code 0xC0000409)
- Python: count repeated elements in the list
- Is there a “do … until” in Python?
- How to put the legend out of the plot
- Python string prints as [u’String’]
- How can I check the extension of a file?
- Matplotlib: TypeError: can’t multiply sequence by non-int of type ‘numpy.float64’
- NameError: global name ‘unicode’ is not defined – in Python 3
- Create nice column output in python
- TypeError: unsupported operand type(s) for +=: ‘int’ and ‘list’
- TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’
- Histogram plotting “AttributeError: max must be larger than min in range parameter.”
- Python TypeError: non-empty format string passed to object.__format__
- AttributeError: ‘Series’ object has no attribute ‘reshape’
- plot a circle with pyplot
- Printing subscript in python
- How to clear Tkinter Canvas?
- Python official installer missing python27.dll
- TypeError: ‘range’ object does not support item assignment
- how to build .exe for python 3.5+, 3.6 if possible?
- How to query as GROUP BY in django?
- Python regex match space only
- OpenCV houghLinesP parameters
- Generate a random letter in Python
- NameError from Python input() function
- How do I tell Matplotlib to create a second (new) plot, then later plot on the old one?
- Syntax highlighting in vim for python
- Flask Value error view function did not return a response [duplicate]
- AttributeError: ‘Series’ object has no attribute ‘split’ error in sending emails