Skip to content

Instantly share code, notes, and snippets.

View gubatron's full-sized avatar

Angel Leon gubatron

View GitHub Profile
def lacsap(n):
if n==0:
return [2]
if n == 1:
return [2,2]
else:
coeficientsToGenerate = n-1
above = lacsap(n-1)
lenAbove = len(above)
result = [2]
#include "brilliant.h"
#define true 1
#define false 0
int gcd(int a, int b) {
int c;
while (a!=0) {
c=a;
a=b%a;
b=c;
#include "brilliant.h"
#define true 1
#define false 0
int gcd(int a, int b) {
int c;
while (a!=0) {
c=a;
a=b%a;
b=c;
/** Java's BigInteger doesn't have a squared root function, sigh */
public static BigInteger sqrt(BigInteger n) {
BigInteger a = BigInteger.ONE;
BigInteger b = new BigInteger(n.shiftRight(5).add(new BigInteger("8")).toString());
while(b.compareTo(a) >= 0) {
BigInteger mid = new BigInteger(a.add(b).shiftRight(1).toString());
if(mid.multiply(mid).compareTo(n) > 0) b = mid.subtract(BigInteger.ONE);
else a = mid.add(BigInteger.ONE);
}
return a.subtract(BigInteger.ONE);
@gubatron
gubatron / gist:7688283
Created November 28, 2013 07:07
find out if a number has a perfect square.
public static boolean isPerfectSquare(int n) {
double root = Math.sqrt(n);
return root-((double)((int)root)) == 0;
}
@gubatron
gubatron / gist:7904896
Created December 11, 2013 04:00
Miss Python's "in" keyword when coding in Java. Here's something that may help avoid long OR statements.
package com.frostwire.util;
public class Condition {
/**
* Useful to shorten long "or" boolean expressions.
* @param needle
* @param args
* @return true if needle is in any of the args.
*/
@gubatron
gubatron / gist:8027555
Created December 18, 2013 18:43
put this on your .bash_profile/.bashrc or a script included by those. emacs leaves backup files you might need (that's why i don't turn it off in emacs), once you know you can get rid of them, invoke rmTildes and it will delete recursively.
function rmTildes {
find . | grep -e "~$" | xargs rm
}
@gubatron
gubatron / gist:8890923
Last active August 29, 2015 13:56
replacing copyright headers on files changed in 2014 #quickNDirty
'''
Run this script inside of src/ and it will look for all the files
that were changed this year that still have the last year in the
copyright headers, and it will fix the headers on that file using
a perl regex one liner.
For example: if it finds something like this and we're in 2014
// Copyright (c) 2009-2013 The Bitcoin developers

A pseudonymous trust system for a decentralized anonymous marketplace

Dionysis Zindros, National Technical University of Athens dionyziz@gmail.com

Keywords

pseudonymous anonymous web-of-trust identity trust bitcoin namecoin proof-of-burn timelock decentralized anonymous marketplace openbazaar

Abstract

@gubatron
gubatron / gist:24c798a38360e6127f77
Created August 17, 2014 01:52
Testing the cost of invoking ioloop.instance()
#My theory is that if ioloop.instance() is to be called for every request the websocket might receive
#this is costly as the instance() method has a dictionary lookup in there.
#Plus there's also the cost of allocating memory for the stack of the instance() method and then returning back to
#the method which invokes it, vs just keeping the reference once and reusing it.
#So here's some test code
import cProfile
from zmq.eventloop import ioloop
ioloop.install()