Skip to content

Instantly share code, notes, and snippets.

@junichiro
Created February 8, 2018 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junichiro/98fb8ea069d70f7e274e9f096bdc1fbf to your computer and use it in GitHub Desktop.
Save junichiro/98fb8ea069d70f7e274e9f096bdc1fbf to your computer and use it in GitHub Desktop.
会社で少し盛り上がった Project Euler をやってみる 014 ref: https://qiita.com/junichiro/items/42b5dea47b5030097ddf
import sys
from multiprocessing import Pool
class Problem14:
CASH = {}
def main(self, n):
result = 1
num = 1
for i in range(2, n):
count = p.collatzNumCount(i)
self.CASH[i] = count
if result < count:
num = i
result = count
print(num, result)
def collatzNumCount(self, n):
i = 0
while(n != 1):
if n in self.CASH:
i += self.CASH[n]
return i
n = self.collatzNum(n)
i += 1
return i
def collatzNum(self, n):
if n % 2 == 0:
return self._collatzEven(n)
else:
return self._collatzOdd(n)
def _collatzOdd(self, n):
return 3 * n + 1
def _collatzEven(self, n):
return n / 2
if __name__ == '__main__':
num = 1000000
if len(sys.argv) == 2:
num = sys.argv[1]
p = Problem14()
p.main(int(num))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment