Skip to content

Instantly share code, notes, and snippets.

View AnthonyBloomer's full-sized avatar

Anthony Bloomer AnthonyBloomer

View GitHub Profile
@AnthonyBloomer
AnthonyBloomer / collatz.py
Last active August 20, 2016 17:33
Collatz conjecture
import sys
# Take any positive integer n.
# If n is even, divide it by 2 to get n / 2.
# If n is odd, multiply it by 3 and add 1 to obtain 3n + 1.
# The conjecture is that no matter what number you start with, you will always eventually reach 1.
def collatz(n):
if n % 2 == 0:
@AnthonyBloomer
AnthonyBloomer / convert.py
Created January 6, 2016 03:14
Convert a decimal to a given base and then back to it's decimal representation.
# convert a decimal to a given base
def d2b(dec, base):
stack = []
result = ''
while dec > 0:
rem = dec % base
dec = dec / base
stack.append(rem)
while stack:
result += str(stack.pop())