# Project Euler 14: (Iron)Python

Filed under:
|
|
|
##### python

In my attempt to learn (Iron)Python out in the open, here’s my solution for Project Euler Problem 14

As always, any feedback is welcome.

```# Euler 14
# http://projecteuler.net/index.php?section=problems&id=14
# The following iterative sequence is defined for the set
# of positive integers:
# n ->  n/2 (n is even)
# n ->  3n + 1 (n is odd)
# Using the rule above and starting with 13, we generate
# the following sequence:
# 13  40  20  10  5  16  8  4  2  1
# It can be seen that this sequence (starting at 13 and
# finishing at 1) contains 10 terms. Although it has not
# been proved yet (Collatz Problem), it is thought that all
# starting numbers finish at 1. Which starting number,
# under one million, produces the longest chain?
# NOTE: Once the chain starts the terms are allowed to go
# above one million.
import time
start = time.time()

def collatz_length(n):
# 0 and 1 return self as length
if n <= 1: return n

length = 1
while (n != 1):
if (n % 2 == 0):
n /= 2
else:
n = 3*n + 1

length += 1

return length

starting_number, longest_chain = 1, 0

for x in xrange(1, 1000001):
l = collatz_length(x)
if l > longest_chain: starting_number, longest_chain = x, l

print starting_number
print longest_chain

# Slow 31 seconds
print "Elapsed Time:", (time.time() - start) * 1000, "millisecs"
a=raw_input('Press return to continue')
```

© Johnny Coder or respective owner

### Related posts about ironpython

• #### Calling C# object method from IronPython

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm trying to embed a scripting engine in my game. Since I'm writing it in C#, I figured IronPython would be a great fit, but the examples I've been able to find all focus on calling IronPython methods in C# instead of C# methods in IronPython scripts. To complicate things, I'm using Visual Studio… >>> More

• #### Why is IronPython faster than the Official Python Interpreter

as seen on Stack Overflow - Search for 'Stack Overflow'
According to this: http://www.codeplex.com/IronPython/Wiki/View.aspx?title=IP20VsCPy25Perf&referringTitle=IronPython%20Performance IronPython (Python for .Net) is faster than regular Python (cPython) on the same machine. Why is this? I would think compiled C code would always be faster than… >>> More

• #### Name some non-trivial sites written using IronPython & Silverlight

as seen on Stack Overflow - Search for 'Stack Overflow'
Just what the title says. It'd be nice to know a few non-trivial sites out there using Silverlight in Python. >>> More

• #### Embedding IronPython in a WinForms app and interrupting execution

as seen on Stack Overflow - Search for 'Stack Overflow'
BACKGROUND I've successfully embedded IronPython in my WinForm apps using techniques like the one described here: http://blog.peterlesliemorris.com/archive/2010/05/19/embedding-ironpython-into-a-c-application.aspx In the context of the embedding, my user may any write loops, etc. I'm using the IronPython… >>> More

• #### Distributing IronPython applications - how to detect the location of ipyw.exe

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm thinking of developing a small application using Iron python, however I want to distribute my app to non-techies and so ideally I want to be able to give them a standard shortcut to my application along with the instructions that they need to install IronPython first. If possible I even want… >>> More

### Related posts about languages

• #### Programming languages, positional languages and natural languages

as seen on Programmers - Search for 'Programmers'
Some programming languages are modeled on machine code, like assembly languages. Other languages are modeled on a natural language, the English language. Others are not modeled on either machine code or natural language. Languages such as PROLOG, for example, don't follow either model. I came across… >>> More

• #### Dynamic type languages versus static type languages

as seen on Stack Overflow - Search for 'Stack Overflow'
What are the advantages and limitations of dynamic type languages compared to static type languages? See also: whats with the love of dynamic languages (a far more argumentative thread...) >>> More

• #### Are Mark Up languages considered programming languages?

as seen on Stack Overflow - Search for 'Stack Overflow'
Hi Are all those markup languages programming languages? For example XML or HTML are programming languages? >>> More

• #### Managed Languages vs Compiled Language difference?

as seen on Programmers - Search for 'Programmers'
I get confused when people try to make a distinction between compiled languages and managed languages. From experience, I understand that most consider compiled languages to be C,C++ while managed languages are Java,C# (There are obviously more, but these are just few examples). But what exactly is… >>> More

• #### Advantages of compilers for functional languages over compilers for imperative languages

as seen on Stack Overflow - Search for 'Stack Overflow'
As a follow up to this question What are the advantages of built-in immutability of F# over C#?--am I correct in assuming that the F# compiler can make certain optimizations knowing that it's dealing with largely immutable code? I mean even if a developer writes "Functional C#" the compiler wouldn't… >>> More