Skip to content

Instantly share code, notes, and snippets.

View hamsham's full-sized avatar

Miles Lacey hamsham

View GitHub Profile
@hamsham
hamsham / num_zeros.cpp
Created January 4, 2016 00:39
Count the number of leading and trailing zeroes in an integer.
/**
* Count the number of leading and trailing zeroes in an integer
*
* g++ -std=c++11 -Wall -Wextra -Werror -pedantic-errors num_zeros.cpp -o num_zeros
*/
#include <iostream>
#include <cstdint>
@hamsham
hamsham / bn_strassen.cpp
Last active February 16, 2023 09:30
Bignum multiplication of numbers with arbitrary bases, using the Schönhage-Strassen algorithm.
/**
* Bignum multiplication of numbers with arbitrary bases, using the
* Schonhage-Strassen algorithm.
*
* g++ -std=c++11 -Wall -Wextra -Werror -pedantic-errors bn_strassen.cpp -o bn_strassen
*
* usage: bn_strassen.exe 123456789 987654321
*
* Sources:
@hamsham
hamsham / bn_karatsuba.cpp
Last active January 2, 2016 09:40
Bignum multiplication of numbers with arbitrary bases, using Karatsuba's algorithm.
/**
* Bignum multiplication of numbers with arbitrary bases, using Karatsuba's algorithm.
*
* g++ -std=c++11 -Wall -Wextra -Werror -pedantic-errors bn_karatsuba.cpp -o bn_karatsuba
*
* usage: bn_karatsuba.exe 123456789 987654321
*/
#include <algorithm>
@hamsham
hamsham / lockfile.py
Created December 21, 2015 04:33
A Python lock-file implementation for *nix systems,
#!/bin/env python
import sys
import os
import fcntl
def lockfile_ext():
return '.lock'
@hamsham
hamsham / karatsuba.cpp
Created November 16, 2015 08:18
Fast large integer multiplication using Karatsuba's divide-and-conquer algorithm.
/*
* Fast large integer multiplication using Karatsuba's divide-and-conquer algorithm.
*
* g++ -std=c++11 -Wall -Werror -Wextra -pedantic -pedantic-errors karatsuba.cpp -o karatsuba
*/
#include <cstdint>
#include <iostream>
#include <stack>
@hamsham
hamsham / sorts.c
Last active November 22, 2018 02:14
Implementations of various sorting methods in ANSI C.
/**
* Testing implementations of different sorting methods.
*
* gcc -ansi -Wall -Werror -Wextra -static-libgcc -pedantic-errors sorts.c -o sorts
*/
#include <assert.h>
#include <math.h>
#include <stdio.h>
@hamsham
hamsham / unique_ptr.cpp
Created November 15, 2015 09:03
Unique-pointer implementation with array-size tracking.
/**
* Testing a safe pointer interface
*
* g++ -std=c++11 -Wall -Werror -Wextra -pedantic -pedantic-errors unique_ptr.cpp -o unique_ptr
*/
#include <iostream>
#include <new>
#include <memory>
@hamsham
hamsham / thread_load.cpp
Created November 15, 2015 08:57
Example of loading files on a separate thread in C++11
/*
* Testing the loading of file data using a thread
* g++ -std=c++11 -Wall -Werror -Wextra -pedantic-errors -O3 -lpthread thread_load.cpp -o thread_load
*/
#include <atomic>
#include <chrono>
#include <fstream>
#include <iostream>
@hamsham
hamsham / cppthread.cpp
Created November 15, 2015 08:55
Test of C++11's threading API
// testing C++11 threads
// g++ -Wall -Wextra -Werror -pedantic-errors -std=c++11 cppthread.cpp -o cppthread
#include <iostream>
#include <chrono>
#include <map>
#include <thread>
#include <functional>
@hamsham
hamsham / root.cpp
Created November 15, 2015 08:46
Newton's method for approximating square roots.
/*
* Testing a simple square-root method.
*
* g++ -std=c++11 -Wall -Werror -Wextra -pedantic -pedantic-errors root.cpp -o root
*/
#include <iostream>
static constexpr double EPSILON = 1.0e-9;