Skip to content

Instantly share code, notes, and snippets.

@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 August 12, 2024 17:01
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.
# 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.cr
Last active August 12, 2024 17:02
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.
# 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.rs
Last active March 20, 2025 21:07
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.
// 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 / 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
@jzakiya
jzakiya / gcd.nim
Created March 17, 2018 12:59
Iterative Stein's (binary) GCD (greatest common divisor) algorithm in Nim
proc gcd(a, b: int): int =
var (a, b) = (a, b)
if a == 0: return b
if b == 0: return a
var k = 0
while ((a or b) and 1) == 0:
a = a shr 1
b = b shr 1
k += 1