This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |