Skip to content

Instantly share code, notes, and snippets.

View cslarsen's full-sized avatar

Christian Stigen Larsen cslarsen

View GitHub Profile
@cslarsen
cslarsen / ipv4.py
Created January 11, 2012 15:16
Two small Python functions to convert IPv4 address to integer and vice-versa
#!/usr/bin/env python
"""Functions to convert IPv4 address to integer and vice-versa.
Written by Christian Stigen Larsen, http://csl.sublevel3.org
Placed in the public domain by the author, 2012-01-11
Example usage:
$ ./ipv4 192.168.0.1 3232235521
192.168.0.1 ==> 3232235521
@cslarsen
cslarsen / binary_gcd.cpp
Created January 18, 2012 20:03
Binary gcd algorithm in C++ using iteration and bit shifts
/*
* The binary gcd algorithm using iteration.
* Should be fairly fast.
*
* Put in the public domain by the author:
*
* Christian Stigen Larsen
* http://csl.sublevel3.org
*/
int binary_gcd(int u, int v)
@cslarsen
cslarsen / euler_phi.cpp
Created January 18, 2012 20:16
Euler's totient function phi --- a fast implementation in C++
/*
* Euler's totient function phi(n).
* http://en.wikipedia.org/wiki/Euler%27s_totient_function
*
* This is an *EXTREMELY* fast function and uses
* several tricks to recurse.
*
* It assumes you have a list of primes and a fast
* isprime() function. Typically, you use a bitset
* to implement the sieve of Eratosthenes and use
@cslarsen
cslarsen / reverse_string.cpp
Created February 16, 2012 19:04
Reverse string in "canonical" C++
/*
* Reverse string in "canonical" C++
*
* Why did I make this a gist? Because I've seen so
* many cumbersome ways of doing this in C++, including
* using <algorithm> and std::reverse, using loops and
* so on. Yeah, those work too, and loops are fine for
* C, but in C++, you should simply just use the iterators
* and standard constructors that std::string gives you.
*
@cslarsen
cslarsen / memoize.py
Created February 20, 2012 14:26
Memoization decorator in Python
"""
Implementation of memoization decorator in Python.
Written by Christian Stigen Larsen
http://csl.sublevel3.org
Put in the public domain by the author, 2012
This is an example of how to use decorators to implement different kind of
behaviour in Python.
@cslarsen
cslarsen / human-numbers.cpp
Created February 20, 2012 18:44
Convert big number to human readable format
/*
* A simple way to format numbers as human readable strings.
* E.g., 123456789 ==> 123 million
*
* Written by Christian Stigen Larsen
* http://csl.sublevel3.org
*
* Placed in the public domain by the author, 2012
*/
@cslarsen
cslarsen / objc-gcc.m
Created March 20, 2012 19:23
How to compile Objective-C on the command line on Mac OS X
/*
* To compile objective-c on the command line:
*
* gcc -framework Foundation objc-gcc.m
*
* You may have to link with -lobjc or other libs,
* as required.
*/
#import <Foundation/Foundation.h>
@cslarsen
cslarsen / mul.cpp
Created June 8, 2012 15:23
Shows how gcc cancels bad "optimizations"
/*
* Simple program I wrote after reading Felix Von Leitner's notes
* on optimization at http://www.fefe.de/source-code-optimization.pdf
*
* In the 90s, it used to be faster to do (x<<8)+(x<<6) than x*320,
* but today with the Intel Core i7, it's actually faster to do
* x*320.
*
* And the compiler knows it!
*
@cslarsen
cslarsen / celsius-fahrenheit.scm
Created September 8, 2012 14:35
R7RS version of Celsius-Fahrenheit table
#|
| Celsius-Fahrenheit temperature table
|
| Taken from
| http://programmingpraxis.com/2012/09/07/the-first-two-programs/2/
|
| Ported to current R7RS scheme by Christian Stigen Larsen
| Public Domain, 2012
|
| Works in Mickey Scheme
@cslarsen
cslarsen / lab0.cpp
Created November 19, 2012 20:26
Example of a race condition with and wihout locking (pthread or x86 TSL)
/*
* =================================================
* DAT320 Operating Systems, University of Stavanger
*
* LAB 0: PROTECTION AND THREADS
* Written by Christian Stigen Larsen, 2012-08-24
* =================================================
*
* ABOUT
* -----