Created
February 8, 2018 03:46
-
-
Save junichiro/98fb8ea069d70f7e274e9f096bdc1fbf to your computer and use it in GitHub Desktop.
会社で少し盛り上がった Project Euler をやってみる 014 ref: https://qiita.com/junichiro/items/42b5dea47b5030097ddf
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
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