
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to generate prime twins using Python?
Twin primes are pairs of primes which differ by two. The first twin primes are {3,5}, {5,7}, {11,13} and {17,19}. You can generate prime twins in python by running a for loop and checking for primality of the numbers as you do so.
example
def is_prime(n): for i in range(2, n): if n % i == 0: return False return True def generate_twins(start, end): for i in range(start, end): j = i + 2 if(is_prime(i) and is_prime(j)): print("{:d} and {:d}".format(i, j)) generate_twins(2, 100)
Output
This will give the output −
3 and 5 5 and 7 11 and 13 17 and 19 29 and 31 41 and 43 59 and 61 71 and 73
- Related Articles
- How to generate prime numbers using Python?
- How to generate prime numbers using lambda expression in Java?
- How to generate XML using Python?
- How to generate Prime Numbers in JavaScript?
- How to generate JSON output using Python?
- How to generate statistical graphs using Python?
- How to generate a 24bit hash using Python?
- How to generate pyramid of numbers using Python?
- How to generate secure temporary file name using Python?
- How to generate a string of bits using Python?
- How to generate a random 128 bit strings using Python?
- How to generate random regression problems using Python Scikit-learn?
- How can Bokeh be used to generate scatter plot using Python?
- How to generate and plot classification dataset using Python Scikit-learn?
- How to generate IP addresses from a CIDR address using Python?

Advertisements