# Project Euler 12: (Iron)Python

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

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

As always, any feedback is welcome.

```# Euler 12
# http://projecteuler.net/index.php?section=problems&id=12
# The sequence of triangle numbers is generated by adding
# the natural numbers. So the 7th triangle number would be
# 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms
# would be:
# 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
# Let us list the factors of the first seven triangle
# numbers:
#  1: 1
#  3: 1,3
#  6: 1,2,3,6
# 10: 1,2,5,10
# 15: 1,3,5,15
# 21: 1,3,7,21
# 28: 1,2,4,7,14,28
# We can see that 28 is the first triangle number to have
# over five divisors. What is the value of the first
# triangle number to have over five hundred divisors?
import time
start = time.time()

from math import sqrt

def divisor_count(x):
count = 2 # itself and 1
for i in xrange(2, int(sqrt(x)) + 1):
if ((x % i) == 0):
if (i != sqrt(x)): count += 2
else: count += 1
return count

def triangle_generator():
i = 1
while True:
yield int(0.5 * i * (i + 1))
i += 1

triangles = triangle_generator()

while True:
num = triangles.next()
if (divisor_count(num) >= 501):
break;

print "Elapsed Time:", (time.time() - start) * 1000, "millisecs"
```

© 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