Skip to content

Instantly share code, notes, and snippets.

@drkpkg
Last active September 12, 2016 17:05
Show Gist options
  • Save drkpkg/caa035b910a4133d2f249c899949037d to your computer and use it in GitHub Desktop.
Save drkpkg/caa035b910a4133d2f249c899949037d to your computer and use it in GitHub Desktop.
Conjetura de collatz
class Collatz(object):
def __init__(self):
self.numbers = []
self.ways = 1
def solve(self, number):
self.numbers.clear()
self.ways = 0
if number > 1:
while True:
#bad ways, but it works
self.push(number)
number = self.calculate_way(number)
if number == 1:
self.push(number)
break
else:
print("Number below to 1")
def push(self, number):
self.numbers.append(number)
self.ways += 1
def get_ways(self):
return self.ways
def get_numbers(self):
return self.numbers
@staticmethod
def calculate_way(actual_number):
if actual_number % 2 == 0:
return actual_number / 2
else:
return (actual_number * 3) + 1
if __name__ == '__main__':
collatz = Collatz()
collatz.solve(27)
print(collatz.get_ways())
print(collatz.get_numbers())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment