Skip to content

Commit

Permalink
Problem 014.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Oriani committed Jul 3, 2015
1 parent 1397fd8 commit 56e9c4f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ docs/_build/
target/

# Visual Studio
*.suo
*.suo
*.user
34 changes: 34 additions & 0 deletions 014.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# ----------------------------------
# Project Euler - projecteuler.net
# ----------------------------------
# Developed by Felipe B Oriani
# 014 - Longest Collatz sequence
# Which starting number, under one million, produces the longest chain?
# ----------------------------------

def collatz(n):
i = 0
r = n
while r > 1:
if r % 2 == 0:
r = int(r / 2)
else:
r = int(r*3 + 1)
i += 1
return [i, n]

def f(n):
m = 0
maxStarting = 0
for starting in range(0, n):
c = collatz(starting)
if c > m:
maxStarting = starting
m = c
return maxStarting

anwser = f(10**6)

#assert anwser == 233168

print('Answer: ', anwser)
4 changes: 2 additions & 2 deletions Euler.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ProjectGuid>da4d3d95-087f-4893-802b-0f173e243244</ProjectGuid>
<ProjectHome>
</ProjectHome>
<StartupFile>008.py</StartupFile>
<StartupFile>014.py</StartupFile>
<SearchPath>
</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
Expand All @@ -23,7 +23,7 @@
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Compile Include="011.py" />
<Compile Include="014.py" />
<Compile Include="008.py" />
<Compile Include="012.py" />
<Compile Include="007.py" />
Expand Down

0 comments on commit 56e9c4f

Please sign in to comment.