Skip to content

Instantly share code, notes, and snippets.

@1995eaton
1995eaton / fibonacci.java
Created January 11, 2015 03:31
Fibonacci Iterator function in Java 8
import java.util.Iterator;
import java.math.BigInteger;
class Main {
public static Iterable<BigInteger> fibonacci(final long N) {
return () -> new Iterator<BigInteger>() {
private long cursor = 0;
private BigInteger a = BigInteger.ZERO,
b = BigInteger.ONE;
public boolean hasNext() {
@1995eaton
1995eaton / miller-rabin.py
Created December 30, 2014 20:16
Miller-Rabin Primality Test
from random import randrange
def miller_rabin(n, k):
if n == 2 or n == 3:
return True
if n < 2 or ~n & 1:
return False
s, d = 0, n - 1
while ~d & 1:
@1995eaton
1995eaton / rewrite.c
Created December 10, 2014 13:13
rw: like sponge, but better
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <poll.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <getopt.h>
@1995eaton
1995eaton / sponge.c
Created December 10, 2014 07:00
My own implementation of the Unix sponge command: http://linux.die.net/man/1/sponge
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *text;
size_t len;
} filedata_t;
filedata_t *
#include <iostream>
#include <string>
class Node
{
friend class LinkedList;
private:
Node* _next;
std::string _data;
public:
@1995eaton
1995eaton / read.cc
Created December 2, 2014 01:40
Fast one-liner to read a file into a string.
#include <iostream>
#include <sstream>
#include <fstream>
int main() {
std::string data = static_cast<std::ostringstream&>
( std::ostringstream() << std::ifstream("data.txt").rdbuf() ).str();
std::cout << data << std::endl;
}
@1995eaton
1995eaton / log.cc
Last active August 29, 2015 14:10
C++ logging functions
#ifndef LOG_H
#define LOG_H
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cctype>
template <typename T>
struct TYPE;
#include <string>
#include <iostream>
#include <time.h>
using namespace std;
/***************************** Person Class **********************************/
class Person
{
@1995eaton
1995eaton / fib.S
Last active April 30, 2016 15:51
Fibonacci sequence in GCC assembly
.intel_syntax noprefix
.globl fib
fib:
sub rsp, 24
xor eax, eax
mov ecx, 1
0:
xadd rax, rcx
@1995eaton
1995eaton / brainfuck.c
Created November 14, 2014 17:32
Brainfuck interpreter in C
#include <stdio.h>
#include <stdlib.h>
static struct {
size_t buffer_size;
char *buffer, *ptr;
} bf;
void bf_init(size_t initial_cap) {
bf.buffer_size = initial_cap;