Skip to content

Instantly share code, notes, and snippets.

@ajakubek
ajakubek / div_const.py
Created July 24, 2020 21:40
Fast division, modulus and divisibility test with constant divider
#!/usr/bin/env python
# The algorithms implemented in this module are based on the following article:
# D. Lemire, O. Kaser, and N. Kurz, Faster Remainder by Direct Computation, 2018.
import math
def div(value, reciprocal, fractional_bits):
return (value * reciprocal) >> fractional_bits
@ajakubek
ajakubek / feigenbaum.html
Last active July 24, 2019 19:11
Tiny HTML5 obituary for M. J. Feigenbaum, 278 bytes
M. J. Feigenbaum 19.12.1944 - 30.06.2019<br><canvas id=c width=640 height=400><script>c=document.getElementById('c').getContext('2d');c.fillStyle='rgba(0,0,0,.1)';g=Math.random
for(i=1e6;--i;){r=g()*1.6+2.4;x=g();for(s=99;--s;)x*=r-r*x;c.fillRect(r*400-960,x*400,1,1)}</script>
#include <iostream>
#include <signal.h>
#include "signal_guard.hpp"
int main()
{
signal_guard sg(SIGINT);

Keybase proof

I hereby claim:

  • I am ajakubek on github.
  • I am ajakubek (https://keybase.io/ajakubek) on keybase.
  • I have a public key whose fingerprint is 141A 9FFB B8B0 7F5B C564 1B59 53C6 3FF3 5CDA FFD9

To claim this, I am signing this object:

#ifndef PRODUCT_ITERATOR_H
#define PRODUCT_ITERATOR_H
#include <cassert>
#include <iterator>
#include <tuple>
#include <utility>
template <typename Iterator1, typename Iterator2>
class product_iterator

Keybase proof

I hereby claim:

  • I am ajakubek on github.
  • I am ajakubek (https://keybase.io/ajakubek) on keybase.
  • I have a public key whose fingerprint is 6A9A 6D0C 67E8 54DB 6CAF E417 BB3F 3953 15FB 5700

To claim this, I am signing this object:

@ajakubek
ajakubek / rand.php
Created February 21, 2013 01:59
PHP fails at uniform distribution
<?php
const RAND_MULTIPLE = 4;
$histogram = array_fill(0, getrandmax() * RAND_MULTIPLE, 0);
for ($i = 0; $i < 10000000; ++$i)
{
$rnd = rand(0, getrandmax() * RAND_MULTIPLE);
++$histogram[$rnd];
@ajakubek
ajakubek / chrome_focus_stealing_prevention.diff
Created August 12, 2012 23:42
Chrome focus stealing prevention.
commit 6e59bfbc45983b8680f023ac07a34cabe80985e5
Author: Adam Jakubek <ajakubek@gmail.com>
Date: Mon Aug 13 01:24:39 2012 +0200
Prevent focus stealing when opening diverted URLs
This patch allows to override current Chrome's behavior, where opening a
link from an external application (e.g. mail client) causes Chrome's
window to steal focus.
It works by introducing a new command line switch
@ajakubek
ajakubek / cropentro.py
Created June 12, 2012 00:29
Entropy based cropping of images
import math
from optparse import OptionParser
from PIL import Image
def entropy(img):
hist = img.histogram()
total_prob = sum(hist)
probs = [float(x) / total_prob for x in hist]
return -sum([p * math.log(p) for p in probs if p != 0])
@ajakubek
ajakubek / mathquiz.py
Created January 29, 2011 13:38
Given a set of N numbers (operands) and a M binary operators, finds all N-ary expressions evaluating to specified target value.
"""Given a set of N numbers (operands) and a M binary operators,
finds all N-ary expressions evaluating to specified target value.
See this fragment of the Countdown TV show for a sample problem:
http://www.youtube.com/watch?v=EO472qM6M9g"""
import itertools
import sys
class Leaf:
def __init__(self):