Skip to content

Instantly share code, notes, and snippets.

View kimwalisch's full-sized avatar

Kim Walisch kimwalisch

View GitHub Profile
env:
OSX v10.4.11
Python 2.5
mercurial v1.2.1
git v1.6.3.1
repos:
using python repository as of 2009-06-18
@kimwalisch
kimwalisch / contents_scale_test.py
Created June 11, 2021 09:10 — forked from akiross/contents_scale_test.py
Zooming QtQuick Painted item using contents size
#!/usr/bin/env python3
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import *
from PyQt5.QtQuick import *
from PyQt5.QtWidgets import *
class Item(QQuickPaintedItem):
def __init__(self, useBoundRect, parent=None):
super().__init__(parent)
@kimwalisch
kimwalisch / gist:e1a821b3a7f8b66b4be02326f45941da
Created December 13, 2018 13:53 — forked from TooTallNate/gist:906665
Output of "sysctl -a hw" on my iPhone 4.
hw.ncpu: 1
hw.byteorder: 1234
hw.memsize: 527433728
hw.activecpu: 1
hw.physicalcpu: 1
hw.physicalcpu_max: 1
hw.logicalcpu: 1
hw.logicalcpu_max: 1
hw.cputype: 12
hw.cpusubtype: 9
@kimwalisch
kimwalisch / segmented_bit_sieve2.cpp
Last active March 6, 2018 10:06
Counting bits in the segmented sieve of Eratosthenes
// unset bits > limit
if (high == limit)
{
int64_t bits = 0xff << (limit % 16 + 1) / 2;
sieve[sieve_size - 1] &= ~bits;
}
// count primes
for (int64_t n = 0; n < sieve_size; n++)
count += popcnt[sieve[n]];
@kimwalisch
kimwalisch / segmented_bit_sieve.cpp
Last active September 24, 2021 12:42
Segmented sieve of Eratosthenes using a bit array
/// @file segmented_bit_sieve.cpp
/// @author Kim Walisch, <kim.walisch@gmail.com>
/// @brief This is an implementation of the segmented sieve of
/// Eratosthenes which uses a bit array with 16 numbers per
/// byte. It generates the primes below 10^10 in 7.25 seconds
/// on an Intel Core i7-6700 3.4 GHz CPU.
/// @license Public domain.
#include <iostream>
#include <algorithm>
@kimwalisch
kimwalisch / segmented_bit_sieve1.cpp
Last active March 6, 2018 13:36
Segmented bit sieving
// sieve segment using bit array
for (std::size_t i = 0; i < primes.size(); i++)
{
int64_t j = multiples[i];
for (int64_t k = primes[i] * 2; j < segment_size; j += k)
sieve[j >> 4] &= unset_bit[j & 15];
multiples[i] = j - segment_size;
}
@kimwalisch
kimwalisch / segmented_sieve5.cpp
Last active March 4, 2018 17:32
Identify primes
for (; n <= high; n += 2)
if (sieve[n - low]) // n is a prime
count++;
@kimwalisch
kimwalisch / segmented_sieve4.cpp
Last active March 6, 2018 13:35
Segmented sieve
// sieve the current segment
for (std::size_t i = 0; i < primes.size(); i++)
{
int64_t j = multiples[i];
for (int64_t k = primes[i] * 2; j < segment_size; j += k)
sieve[j] = false;
multiples[i] = j - segment_size;
}
@kimwalisch
kimwalisch / segmented_sieve3.cpp
Last active March 6, 2018 09:01
Generate sieving primes
// generate sieving primes using simple sieve of Eratosthenes
for (; i * i <= high; i += 2)
if (is_prime[i])
for (int64_t j = i * i; j <= sqrt; j += i)
is_prime[j] = false;
// initialize sieving primes for segmented sieve
for (; s * s <= high; s += 2)
{
if (is_prime[s])
@kimwalisch
kimwalisch / segmented_sieve2.cpp
Last active March 6, 2018 10:46
Outer sieving loop
for (int64_t low = 0; low <= limit; low += segment_size)
{
std::fill(sieve.begin(), sieve.end(), true);
// current segment = [low, high]
int64_t high = low + segment_size - 1;
high = std::min(high, limit);