Skip to content

Instantly share code, notes, and snippets.

@jzakiya
jzakiya / twinprimes_ssoz.go
Last active February 13, 2025 21:20
Twinprimes generator, multi-threaded, using SSoZ (Segmented Sieve of Zakiya), written in Go.
// This Go source file is a multiple threaded implementation to perform an
// extremely fast Segmented Sieve of Zakiya (SSoZ) to find Twin Primes <= N.
// Inputs are single values N, or ranges N1 and N2, of 64-bits, 0 -- 2^64 - 1.
// Output is the number of twin primes <= N, or in range N1 to N2; the last
// twin prime value for the range; and the total time of execution.
// Code originally developed on a System76 laptop with an Intel I7 6700HQ cpu,
// 2.6-3.5 GHz clock, with 8 threads, and 16GB of memory. Parameter tuning
// probably needed to optimize for other hardware systems (ARM, PowerPC, etc).
@jzakiya
jzakiya / twinprimes_ssoz_ser.go
Last active February 13, 2025 21:27
Twinprimes generator, single-threaded, using SSoZ (Segmented Sieve of Zakiya), written in Go.
// This Go source file is a single threaded implementation to perform an
// extremely fast Segmented Sieve of Zakiya (SSoZ) to find Twin Primes <= N.
// Inputs are single values N, or ranges N1 and N2, of 64-bits, 0 -- 2^64 - 1.
// Output is the number of twin primes <= N, or in range N1 to N2; the last
// twin prime value for the range; and the total time of execution.
// Code originally developed on a System76 laptop with an Intel I7 6700HQ cpu,
// 2.6-3.5 GHz clock, with 8 threads, and 16GB of memory. Parameter tuning
// probably needed to optimize for other hardware systems (ARM, PowerPC, etc).
@jzakiya
jzakiya / twinprimes_ssoz_ser.cr
Last active January 29, 2026 20:25
Twinprimes generator, single-threaded, using SSoZ (Segmented Sieve of Zakiya), written in Crystal
# This Crystal source file is a single threaded implementation to perform an
# extremely fast Segmented Sieve of Zakiya (SSoZ) to find Twin Primes <= N
# for the 64-bit range 0..18,446,744,073,709,551,615 (2**64 - 1)
# Compile as: $ crystal build --release --mcpu native twinprimes_ssoz_ser.cr
# To reduce binary size do: $ strip twinprimes_ssoz_ser
# Single val: $ ./twinprimes_ssoz_ser val1
# Range vals: $ ./twinprimes_ssoz_ser val1 val2
# val1 and val2 can be entered as either: 123456789 or 123_456_789
@jzakiya
jzakiya / twinprimes_ssoz.cr
Last active January 29, 2026 20:30
Twinprimes generator, multi-threaded, using SSoZ (Segmented Sieve of Zakiya), written in Crystal
# This Crystal source file is a multiple threaded implementation to perform an
# extremely fast Segmented Sieve of Zakiya (SSoZ) to find Twin Primes <= N
# for the 64-bit range 0..18,446,744,073,709,551,615 (2**64 - 1)
# Compile as: $ crystal build --release -Dpreview_mt --mcpu native twinprimes_ssoz.cr
# To reduce binary size do: $ strip twinprimes_ssoz
# Thread workers default to 4, set to system max for optimum performance.
# Single val: $ CRYSTAL_WORKERS=8 ./twinprimes_ssoz val1
# Range vals: $ CRYSTAL_WORKERS=8 ./twinprimes_ssoz val1 val2
# val1 and val2 can be entered as either: 123456789 or 123_456_789
@jzakiya
jzakiya / twinprimes_ssoz.rs
Last active January 20, 2026 23:12
Twinprimes generator, multi-threaded using rayon, using SSoZ (Segmented Sieve of Zakiya), written in Rust
// This Rust source file is a multiple threaded implementation to perform an
// extremely fast Segmented Sieve of Zakiya (SSoZ) to find Twin Primes <= N.
// Inputs are single values N, or ranges N1 and N2, of 64-bits, 0 -- 2^64 - 1.
// Output is the number of twin primes <= N, or in range N1 to N2; the last
// twin prime value for the range; and the total time of execution.
// Can compile as: $ cargo build --release
// or: $ RUSTFLAGS="-C opt-level=3 -C debuginfo=0 -C target-cpu=native" cargo build --release
// The later compilation creates faster runtime on my system.
@jzakiya
jzakiya / sozgensa.f
Created December 19, 2018 22:31
Forth versions of Sieve of Zakiya, with benchmarks for various Prime Generators (32 bit code)
\ This code performs the Sieve of Zakiya (SoZ) in Forth.
\ The SoZ uses Number Theory Sieves (NTS) which are
\ classes of number theory prime generator functions.
\ This code implements a revised (from original) algorithm
\ that is optimized in terms of speed and lower memory use.
\ This code implements the first five Stricly Prime (SP)
\ SoZ generators -- P3, P5, P7, P11, P13.
\ Memory use (prms) decreases as the generator sizes increase.
\ Performance scales linearly (for gforth), with higher
\ generators being faster than lower ones for the same N.
@jzakiya
jzakiya / twinprimes_ssoz.d
Last active February 11, 2025 20:14
Twinprimes generator, multi-threaded, using SSoZ (Segmented Sieve of Zakiya), written in D
/* This D source file is a multiple threaded implementation to perform an
* extremely fast Segmented Sieve of Zakiya (SSoZ) to find Twin Primes <= N.
*
* Inputs are single values N, or ranges N1 and N2, of 64-bits, 0 -- 2^64 - 1.
* Output is the number of twin primes <= N, or in range N1 to N2; the last
* twin prime value for the range; and the total time of execution.
*
* Code originally developed on a System76 laptop with an Intel I7 6700HQ cpu,
* 2.6-3.5 GHz clock, with 8 threads, and 16GB of memory. Parameter tuning
* would be needed to optimize for other hadware systems (ARM, PowerPC, etc).
@jzakiya
jzakiya / gcdbenchmarks.cpp
Last active February 18, 2024 19:21
gcd implementation comparison benchmarks in C++
// Daniel Lemire in this article
// https://lemire.me/blog/2013/12/26/fastest-way-to-compute-the-greatest-common-divisor/
// presented benchmark comparisons of different implementations of Stein's (binary) gcd algorithm.
// The wikipedia (iterative) implementation of the algorithm was noted to be very inefficient.
// Lemire presented benchmarked comparisons of various implementations, original code below.
// https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2013/12/26/gcd.cpp
// I have modified the output to make it explicit and clear, modified the code in some functions
// to make them easier to follow and standardized variable names, and added the Ruby implementation.
// I also ordered the ouput to show fastest to slowest.
// The results: gcdwikipedia7fast32 is fastest by far; the implementation shown in widipedia is slowest.
@jzakiya
jzakiya / banned.md
Last active July 19, 2018 21:12
Banned in the Nim Forum on the 4th of July

Banned in the Nim Forum on the 4th of July

As a longtime Ruby user I've become accustomed to how nice and friendly the Ruby community is. There's a saying among Rubyist - MINSWAN - Matz is nice so we are nice. This is not just a reference to Ruby's creator Yukihiro Matsumoto, but also to what Rubyist feel they should be because of Matz. And through the years I've been able to submit a few ideas for improving Ruby that he's accepted, a recent example here, Using Ruby 2.5’s new Integer#sqrt method.

Looking to keep my programming skills current, I keep an eye out for other languages to learn that I may find interesting, useful, or just fun. With that in mind, around 2015/6 I heard about Nim, then I saw this video, and after reading [this article](http://www.bootstrap.me.uk/boot

@jzakiya
jzakiya / twinprimes_ssoz.nim
Last active August 12, 2024 17:06
Twinprimes generator, multi-threaded, using SSoZ (Segmented Sieve of Zakiya), written in Nim
#[
This Nim source file is a multiple threaded implementation to perform an
extremely fast Segmented Sieve of Zakiya (SSoZ) to find Twin Primes <= N.
Inputs are single values N, or ranges N1 and N2, of 64-bits, 0 -- 2^64 - 1.
Output is the number of twin primes <= N, or in range N1 to N2; the last
twin prime value for the range; and the total time of execution.
Code originally developed on a System76 laptop with an Intel I7 6700HQ cpu,
2.6-3.5 GHz clock, with 8 threads, and 16GB of memory. Parameter tuning