No of Pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion

2024/10/6 8:40:15

How to find number of pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion (using any programming language and without using any external libraries) with considering time complexity?

  1. Tried sieve of eratosthenes but getting consecutive primes is challenge

  2. Used generators but time complexity is very high

The code is:

def gen_numbers(n):for ele in range(1,n+1):for i in range(2,ele//2):if ele%i==0:breakelse:yield eleprev=0count=0for i in gen_numbers(2000000000):if i-prev==6:count+=1prev = i
Answer

Interesting question! I have recently been working on Sieve of Eratosthenes prime generators. @Hans Olsson says

You should use segmented sieve to avoid memory issue:en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Segmented_sieve

I agree, and happen to have one which I hacked to solve this question. Apologies in advance for the length and non Pythonic-ness. Sample output:

$ ./primes_diff6.py 100
7 prime pairs found with a difference of 6.
( 23 , 29 ) ( 31 , 37 ) ( 47 , 53 ) ( 53 , 59 ) ( 61 , 67 ) ( 73 , 79 ) ( 83 , 89 )
25 primes found.
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 
83, 89, 97]
$ ./primes_diff6.py 1e5
1940 prime pairs found with a difference of 6.
9592 primes found.

The code:

#!/usr/bin/python -Wall
# program to find all primes smaller than n, using segmented sieve
# see https://github.com/kimwalisch/primesieve/wiki/Segmented-sieve-of-Eratosthenesimport sysdef segmentedSieve(limit):sqrt = int(limit ** 0.5)segment_size = sqrtprev = 0count = 0# we sieve primes >= 3i = 3n = 3sieve       = []is_prime    = [True] * (sqrt + 1)primes      = []multiples   = []out_primes  = []diff6       = []for low in xrange(0, limit+1, segment_size):sieve = [True] * segment_size# current segment = [low, high]high = min(low + segment_size -1, limit)# add sieving primes needed for the current segment#   using a simple sieve of Eratosthenese, starting where we left offwhile i * i <= high:if is_prime[i]:primes.append(i)multiples.append(i * i - low)two_i = i + ifor j in xrange(i * i, sqrt, two_i):is_prime[j] = Falsei += 2# sieve the current segmentfor x in xrange(len(primes)):k = primes[x] * 2j = multiples[x]while j < segment_size:     # NB: "for j in range()" doesn't work here.sieve[j] = Falsej += kmultiples[x] = j - segment_size# collect results from this segmentwhile n <= high:if sieve[n - low]:out_primes.append(n)if n - 6 == prev:count += 1diff6.append(n)prev = nn += 2print count, "prime pairs found with a difference of 6."if limit < 1000:for x in diff6:print "(", x-6, ",", x, ")",printreturn out_primes# Driver Code
if len(sys.argv) < 2:n = 500
else:n = int(float(sys.argv[1]))primes = [2] + segmentedSieve(n)print len(primes), "primes found."
if n < 1000:print primes

This might work as-is if you run it for size 2e9 (2 billion) and subtract the result of size 1e9 (1 billion).

EDIT

Performance info, requested by @ValentinB.

$ time ./primes_diff6.py 2e9 
11407651 prime pairs found with a difference of 6. 
98222287 primes found. real 3m1.089s 
user 2m56.328s 
sys  0m4.656s

... on my newish laptop, 1.6 GHz i5-8265U, 8G RAM, Ubuntu on WSL, Win10

I found a mod 30 prime wheel here in a comment by Willy Good that is about 3x faster than this code at 1e9, about 2.2x faster at 2e9. Not segmented, the guts is a Python generator. I'm wondering if it could be segmented or changed to use a bit array to help its memory footprint without otherwise destroying its performance.

END EDIT

https://en.xdnf.cn/q/70387.html

Related Q&A

Building a docker image for a flask app fails in pip

from alpine:latest RUN apk add --no-cache python3-dev \&& pip3 install --upgrade pipWORKDIR /backend COPY . /backendRUN pip --no-cache-dir install -r requirements.txt EXPOSE 5000 ENTRYPOINT [py…

Why is numba so fast?

I want to write a function which will take an index lefts of shape (N_ROWS,) I want to write a function which will create a matrix out = (N_ROWS, N_COLS) matrix such that out[i, j] = 1 if and only if j…

How to create a field with a list of foreign keys in SQLAlchemy?

I am trying to store a list of models within the field of another model. Here is a trivial example below, where I have an existing model, Actor, and I want to create a new model, Movie, with the field …

Implementing a recursive algorithm in pyspark to find pairings within a dataframe

I have a spark dataframe (prof_student_df) that lists student/professor pair for a timestamp. There are 4 professors and 4 students for each timestamp and each professor-student pair has a “score” (s…

Python Delegate Pattern - How to avoid circular reference?

I would to ask if using the Delegate Pattern in Python would lead to circular references and if so, what would be the best way to implement it to ensure the object and its delegate will be garbage coll…

Render Jinja after jQuery AJAX request to Flask

I have a web application that gets dynamic data from Flask when a select element from HTML is changed. of course that is done via jquery ajax. No probs here I got that.The problem is, the dynamic data …

shape-preserving piecewise cubic interpolation for 3D curve in python

I have a curve in 3D space. I want to use a shape-preserving piecewise cubic interpolation on it similar to pchip in matlab. I researched functions provided in scipy.interpolate, e.g. interp2d, but …

ForeignKey vs OneToOne field django [duplicate]

This question already has answers here:OneToOneField() vs ForeignKey() in Django(12 answers)Closed 9 years ago.I need to extend django user with some additional fields . I found 2 different ways there…

How to sort glob.glob numerically?

I have a bunch of files sorted numerically on a folder, when I try to sort glob.glob I never get the files in the right order.file examples and expected output sorting folder ------ C:\Users\user\Deskt…

How to determine a numpy-array reshape strategy

For a python project I often find myself reshaping and re-arranging n-dimensional numpy arrays. However, I have a hard time to determine how to approach the problem, visualize the outcome of the result…