-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactors files, adds asserts with the solutions
- Loading branch information
mippolito
committed
Mar 7, 2017
1 parent
5c6d0d3
commit b20cfb9
Showing
12 changed files
with
51 additions
and
33 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
from utilities import lower_than | ||
from itertools import count, imap, takewhile | ||
from itertools import count, takewhile | ||
|
||
from functools import lru_cache | ||
from utilities import lower_than | ||
|
||
limit = 1e6 | ||
|
||
cache = {1: 1} | ||
|
||
|
||
@lru_cache | ||
def collatz_len(n): | ||
try: | ||
return cache[n] | ||
except KeyError: | ||
retval = 1 + collatz_len((3 * n + 1) if (n % 2) else (n / 2)) | ||
cache[n] = retval | ||
return retval | ||
|
||
if n == 1: | ||
return 1 | ||
else: | ||
return 1 + collatz_len((3*n + 1) if (n % 2) else (n / 2)) | ||
|
||
gen = ((n, collatz_len(n)) for n in takewhile(lower_than(limit), count(1))) | ||
answer = max(gen, key=lambda t: t[1])[0] | ||
assert answer == 837799 | ||
|
||
answer = max(imap(collatz_len, takewhile(lower_than(limit), count(start=1))), key=lambda t: t[1]) | ||
print(answer) | ||
print answer |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ | |
if not number % p: | ||
answer = p | ||
|
||
assert answer == 6857 | ||
print answer |
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
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
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
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
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
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