This file contains hidden or 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
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: |
This file contains hidden or 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
# 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()) |
NewerOlder