Archive for May 13th, 2009

Problem 1 – nah

May 13, 2009

Eric wins for the first solution. I think I can beat his execution time by approximately a factor of 2 though (on my computer, after running each a few times). The built-in set data structure also has a built-in union function, which I found in chapter 5 of the tutorial. So, my go at the problem is:

``` from time import time

t = time()

threes = set(3*n for n in range(1,1000/3 + 1)) # multiples of 3
fives = set(5*n for n in range(1,1000/5)) # multiples of 5

either = threes | fives # set union

print sum(either), "in", time()-t, "sec"

```

I also included the code to calculate execution time that I learned about from the blog “Numerical Recipes“.

Anybody wanna do up a more general solution? Maybe see how the different approaches scale to numbers higher than 1000, or with other choices of divisors (allowing more of them, perhaps)?

Have another approach? How does it compare, speed-wise?

Just a side-note: I also took my solution and put it all into one line, and avoided saving the sets as variables. This technique seemed to take, on average, just slightly longer to run.

A Word of Warning

May 13, 2009

Well, I learned one thing from my first post: Python is really strict about indentation, since that’s how it determines loops and conditionals and such (no braces!).  I think this is a great thing, and most editors which are made for programming will help you out with this.  But I did notice that copying and pasting my code lost a couple of the tabs (though it should be fixed now), and this might make the interpreter give errors to anyone who tried it.  So I guess we gotta be a bit careful about the formatting as we post these things . . .

Out of the gates in a hurry!

May 13, 2009

Well, I had already tried a bunch of these guys, and I want to try out this posting deal, so here’s my solutions to the first two.

```
#
#  problem1.py
#

nums = set(i for i in range(1,1000) if ((i%3 == 0) or (i%5 == 0)))

print sum(nums)

```

#
#  problem2.py
#

fib_last = 1
fib_cur = 1

ev_sum = 0

def isEven(num):
return (num % 2 == 0)

while fib_cur <= 4000000: if isEven(fib_cur): ev_sum += fib_cur temp = fib_cur fib_cur += fib_last fib_last = temp print ev_sum [/sourcecode]

Syntax Highlighting

May 13, 2009

A quick commenter (our first!) posted a link to a great tip about syntax highlighting, instead of using just preformatted text, for code. At that page, scroll to the bottom, to the section on “Posting Source Code”.

Here’s an example (I hope):

```
print "Hello, World!"

```

Looks awesome! And even gives us line numbers to refer to. Now, let’s hope Project Euler doesn’t get upset with us, as our commenter suggests they might.

Hello, World

May 13, 2009

This blog is intended to contain the work of a collection of math graduate students at the University of Virginia as we learn Python (and/or Sage) by using projecteuler.net as a problem library. For those of us that need tutorials, the Python one is here, and Sage here. Surely lots of other documentation will be found and used (and linked to) in this process.

We will (at least, at the start) set two problems each week as our goal, the link in the sidebar giving which problems, and the “due date.” All of us are encouraged to post whatever we come up with as far as solutions, even if it is similar to an already posted solution. We should also feel free to post programming questions here, and any math questions or facts that we encounter while working on these problems. We should link to any pages we find helpful (programming resources, or math resources), and leave comments on each others posts whenever we are inspired to do so.

I encourage each author to tag each of their posts with their initials, so we can track authors. Presumably wordpress will also handle that for us, so we might drop this habit when we see how it all works. Also tag posts with the problem number, for example: use the tag “prob1” and “prob2”. Then all of the content for each problem can be quickly accessed. You might also consider giving your posts titles that indicate the author and the problem. Maybe also use the tag “math” if you talk about the math of a solution, “question” for questions you post, “misc” for other things? These are just ideas – do whatever makes sense to you at the time, and we’ll work it out.

If you are beaten to a solution, you may try gathering up some of the other contributor’s solutions to compare them, e.g. based on speed or generality or… It will also be fun to see what happens when different solutions claim different answers. If you modify a solution that you have already posted, e.g. to fix an error or something, start a new post (with links back), or add to your old post – do not remove content from your old posts. It’s instructive to see errors.

Let’s put all of our code in

```# Preformatted Text
print "Hello, World!"```

to distinguish it from other text. For authors new to wordpress, the button at the far right, at the top of the editor when you make a new post, gives more formatting options. This is where you will be able to set code as preformatted (and indented, if you chose to use it).

If any of the authors would like to mess around with more settings for this blog, they are welcome to email me, and I will change their status to administrator. Additionally, if authors have ideas about how to change how things are set up or run, they are welcome to just make a post so that everybody can see it (tag it “misc” or “admin” perhaps?). While I may have been the one to start this thing, I really want it to be a group project.

Well, that’s all I can think of for now. Happy hacking!