Skip to content

Instantly share code, notes, and snippets.

@user140242
user140242 / Double_Segmented_Wheel_Sieve.cpp
Last active July 5, 2022 13:32
Segmented wheel sieve
/// This is a implementation of the wheel segmented sieve that
/// uses two vectors in order to have the wheel with the basis {2,3}.
/// This sieve 1/3 of the memory is used compared to the traditional sieve.
/// For large numbers a double segmentation is used.
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdlib>
@user140242
user140242 / wheel_segmented_sieve.cpp
Created July 10, 2022 08:43
wheel segmented sieve
/// This is a implementation of the wheel segmented sieve for counting primes.
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <stdint.h>
void Sieve_Wheel_Base_6(int64_t dim_step, std::vector<char> &Primes5mod6, std::vector<char> &Primes1mod6)
@user140242
user140242 / goldbach_function.py
Last active July 10, 2022 15:33
Goldbach function
import math
import numpy as np
def goldbach(n):
# for n>17 goldbach function returns vector G with G[i]=g(2i) for 2<=i<=n/2
Primes5mod6 =np.ones((n//6+1), dtype=bool)
Primes1mod6 =np.ones((n//6+1), dtype=bool)
imax=math.isqrt(n)//6+1
for i in range(1,imax+1):
if Primes5mod6[i]:
Primes5mod6[6*i*i::6*i-1]=[False]
@user140242
user140242 / Segmented_Sieve_Wheel_6.cpp
Last active July 15, 2022 12:54
Segmented Sieve Wheel base 6
/// This is a implementation of the wheel segmented sieve with the basis {2,3}.
/// This algorithm uses O(sqrt(n)/3) space.
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <stdint.h>
@user140242
user140242 / Algorithm_Sieve_Wheel.txt
Last active July 25, 2022 10:42
Algorithm Sieve Wheel
With the sieve of Eratosthenes algorithm in the Boolean vector SIEVE of size (n+1) initially all set to true
multiples of primes p can be set to false so:
for (p=2; p<=sqrt(n); p++)
if (SIEVE[p])
for (m=p*p; m<n+1; m+=p)
SIEVE[m]=false;
In wheel sieve with base bW ( bW<sqrt(n) and bW divisible by prime p with p<=p_s ).
@user140242
user140242 / segmented_sieve_wheel_30.cpp
Created August 7, 2022 13:51
segmented sieve wheel base 30
/// This is a implementation of the wheel segmented sieve with the basis {2,3,5}.
/// This algorithm uses (sqrt(n) *8/30) space.
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <stdint.h>
@user140242
user140242 / bit_segmented_sieve_wheel_30.cpp
Last active August 8, 2022 18:22
bit segmented sieve wheel base 30
/// This is a implementation of the bit wheel segmented sieve with the basis {2,3,5}.
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <stdint.h>
const int64_t L1_CACHE_SIZE = 32768;
@user140242
user140242 / ll_test.cpp
Last active September 2, 2022 14:20
Lucas-Lehmer Primality Test
// Lucas-Lehmer Primality Test
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <stdint.h>
const int modulus=30; // example modulus=6,12,18,30
@user140242
user140242 / bit_wheel_segmented_sieve.cpp
Last active January 4, 2023 23:28
bit wheel segmented sieve basic choice 30 210 2310
/// This is a implementation of the bit wheel segmented sieve
/// with max base wheel choice 30 , 210 , 2310
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <stdint.h>
#include <time.h>
@user140242
user140242 / bit_wheel_segmented_sieve_multithread.cpp
Last active September 2, 2023 16:29
sieve wheel segmented multithreaded
/// This is a implementation of the bit wheel segmented sieve
/// multithreaded with OpenMP (compile with -fopenmp option)
/// with max base wheel size choice 30 , 210 , 2310 - user140242
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <stdint.h>