Skip to content

Instantly share code, notes, and snippets.

@andrejbauer
andrejbauer / Bijection.md
Last active November 29, 2023 22:05
A bijection between numbers and pairs of numbers.

A bijection between numbers and pairs of numbers

Every once in a while I am faced with someone who denies that the rational numbers (or fractions, or pairs of integers) can be put into a bijective correspondence with natural numbers. To deal with the situation, I coded up the bijection. So now I can just say: "Really? Interesting. Please provide a pair of numbers (i,j) which is not enumerated by f, as defined in my gist." I am still waiting for a valid counter-example.

Anyhow, here is a demo of f and g at work. I am using the Python version, but a Haskell variant is included as well.

The 100-th pair is:

>>> f(100)

(10, 4)

@0xabad1dea
0xabad1dea / tricksy.c
Last active December 17, 2015 14:59
A deceitful C program
// hello clever programmers, would you like to play a game?
// where's the bug?
// by 0xabad1dea :)
#include <stdio.h>
#include <string.h>
int main() {
char input[16] = "stringstring!!!";
char output[8];
@andkerosine
andkerosine / raskell.rb
Created August 15, 2012 05:56
Haskell-like list comprehensions in Ruby
$stack, $draws = [], {}
def method_missing *args
return if args[0][/^to_/]
$stack << args.map { |a| a or $stack.pop }
$draws[$stack.pop(2)[0][0]] = args[1] if args[0] == :<
end
class Array
def +@
@hellerbarde
hellerbarde / latency.markdown
Created May 31, 2012 13:16 — forked from jboner/latency.txt
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@axiomsofchoice
axiomsofchoice / envelope_choice.py
Created November 27, 2011 14:06
Experimental mathematics for November 2011 London MathsJam
#!/usr/bin/env python
"""This piece of experimental mathematics came out of a discussion at the November 2011
London MathsJam sparked by a Google Plus post from Randall Munroe https://plus.google.com/111588569124648292310/posts/dv9Fi45h91T
Code by @axiomsofchoice with help from other MathsJam attendees, including @Samuel_Hansen
"""
import random
n_trials = 1000000
@axiomsofchoice
axiomsofchoice / bcl9.pde
Created November 3, 2011 19:06
Code pulled together during a joint session at BarCamp London 9 on Arduino and Kinect
// Control a line of LED (connected to pins 8-11) via the serial port
// Sending a character '0', '1', '2' or '3' lights up one of the LEDs
// Note that for debugging the Arduino responds by sending 'A', 'B', 'C' or 'D' respectively
// Code pulled together during a joint session at BarCamp London 9 on Arduino and Kinect
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
@axiomsofchoice
axiomsofchoice / MyRandomNumbers.pde
Created September 3, 2011 14:32
Processing Tutorial #solo11
/*
#myrandomnumber Tutorial
blprnt@blprnt.com
April, 2010
*/
//This is the Google spreadsheet manager and the id of the spreadsheet that we want to populate, along with our Google username & password
SimpleSpreadsheetManager sm;
@axiomsofchoice
axiomsofchoice / query-all.sparql
Created August 31, 2011 15:33
How to select *everything* in a triplestore containing several different named graphs
# How to select *everything* in a triplestore containing several different named graphs
SELECT * { { ?s ?p ?o . } UNION { GRAPH ?g { ?s ?p ?o } } }
@visnup
visnup / StackScript.sh
Created August 23, 2011 17:45
node.js knockout 2011 StackScript
#!/bin/bash
# <UDF name="ssh_key" Label="Paste in your public SSH key" default="" example="" optional="false" />
# root ssh keys
mkdir /root/.ssh
echo $SSH_KEY >> /root/.ssh/authorized_keys
chmod 0700 /root/.ssh
# update to latest
require 'rubygems'
require 'nokogiri'
m = {}
f = {}
open("dist.male.first").readlines.each { |l|
d = l.split(/ +/)
m[d[0].downcase] = d[1].to_f
}
open("dist.female.first").readlines.each { |l|