Skip to content

Instantly share code, notes, and snippets.

@strategicpause
strategicpause / aks.py
Created July 5, 2012 16:24
AKS Algorithm for detecting prime numbers
from math import ceil
from math import floor
from math import log
from math import pow
from math import sqrt
from numbertheory import is_power
from numbertheory import gcd
from numbertheory import modular_exponentation
# Determines if n is prime using the AKS algorithm
@strategicpause
strategicpause / numbertheory.py
Created July 5, 2012 16:24
Number theory methods
from math import ceil
from math import log
from math import pow
# Code Taken from http://en.wikipedia.org/wiki/Modular_multiplicative_inverse
def extended_gcd(a, b):
x, last_x = 0, 1
y, last_y = 1, 0
while b:
@strategicpause
strategicpause / gist:1132279
Created August 8, 2011 17:47
Exercise#4.js
function isCreditCard( CC ) {
var sum = 0, mul = 1;
if (CC.length > 19)
return false;
forEach(CC, function(digit) {
var tproduct = digit * mul;
sum += (tproduct >= 10) ? (tproduct % 10) + 1 : sum += tproduct;
(mul == 1) ? mul++ : mul--;
});
return ((sum % 10) == 0);
@strategicpause
strategicpause / life.html
Created February 15, 2011 06:43
Conway's Game of Life in HTML5 Canvas
<html>
<head>
<script type="text/javascript">
// Author: Nick Peters
// Date: 02/14/11
// Based on http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
// Demo at http://www.nickpeters.net/life.html
var WIDTH = 100;
var HEIGHT = 100;
var state = new Array(WIDTH);
@strategicpause
strategicpause / gist:750988
Created December 22, 2010 02:27
Converts decimal to binary using bit operations
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n = 32;
int exp = (int)ceil(log(n)/log(2));
int x = (int)pow(2, exp);
replacements = { 'a': ['@', '4'], 'e': ['3'], 'i': ['!', '1'], 'o': ['0'] }
word = list('I am an awesome person')
for index, letter in enumerate(word):
if letter.lower() in replacements:
n = len(replacements[letter.lower()])
word[index] = replacements[letter.lower()][index % n]
print newWord # Prints '! @m 4n @w3s0m3 p3rs0n'
from math import floor
from math import pow
def is_power(n):
b, l, u = 2, 1, n
while(True):
a = floor((l + u) / 2)
if a == l or a == u:
b = b + 1
l = 1
# Determines whether or not n is of the form a**b
from math import ceil
from math import log
from math import pow
def is_power(n):
a = 2
b = ceil(log(n) / log(2))
while b > 1:
def gcd(a, b):
# Make sure a is greater than b
if(a < b):
a, b = b, a
if a % b == 0:
print b
return
gcd(b, a % b)
gcd(3,5) # Prints 1
gcd(2,6) # Prints 2
# Thought of this simple technique for differentiating polynomial functions
# while not paying attention in my Differential Equations class
def differentiate(f):
f = f[0:-1] # Remove last value since it's a constant
for i in range(0, len(f)):
f[i] = f[i] * (len(f) - i)
return f
f = [1, 2, 1] # Represents x^2+2x+1
print differentiate(f) == [2,2] # 2x + x