Archive for May 14th, 2009


May 14, 2009

So I noticed that two solutions used code like this to generate the Fibbonaci numbers:



a, b = b, a+b

Being used to older languages like C, these multiple assignments would not have been allowed.  But beyond that, it raises the question of the order in which the assignments are made.  For example, I would have thought they would be processed just as they are written, in which case the second assignment would be adding “b” to itself instead of the old “a” (which is why I used an extra temporary variable in my solution.) Since this code gets the right answer, this is apparently not the case.

All Python data types are objects, so what if the multiple assignment had more complicated side effects?  Do all the objects involved get copied in memory as an assignment like this takes place to save their state? This is probably all wrapped up in the reference system.  Maybe someone could clarify the assignment process for me.  (And save my lazy ass the trouble of looking it up.)

More solutions…

May 14, 2009

So mine are almost identical to Tim’s but I’ll post them anyway along with the time they take…

Problem 1:

sum = 0
for i in range(1000):
  if (i%3 == 0 or i%5 == 0):
    sum = sum + i
print sum

Time: 0.000477 sec

Problem 2:

fibsum = 0
a, b = 0, 1
while b < 4000000:    if b%2 == 0:      fibsum = fibsum + b    a, b = b, (a+b) print fibsum [/sourcecode] Time: 0.000148 sec


May 14, 2009

I tried the problem first in Mathematica, and then in Sage. Here is my Mathematica code:

j = 0;
For[i = 1, i < 1000, i++, j += If[Divisible[i, 3], i, If[Divisible[i, 5], i, 0]]]; j [/sourcecode] Mathematica claims a running time of 0.005759 seconds. Now here is the code I ran in Sage: [sourcecode language='python'] j=0; def is_divisible_by(number,divisor): return number%divisor == 0 for i in range(1,1000): if is_divisible_by(i,3): j+=i if is_divisible_by(i,3)==false: if is_divisible_by(i,5): j+=i print(j) [/sourcecode] Sage tells me that it took 0.02s to run that code. Then I saw some of your previouly posted solutions, and amended my code so that the for loop looked like [sourcecode language='python'] for i in range(1,1000): if is_divisible_by(i,3) or is_divisible_by(i,5): j+=i [/sourcecode] (i.e., I made my code almost exactly the same as some of the previously posted solutions). Now Sage says it takes 0.01s to compute the answer.

Rustic Solutions

May 14, 2009

Here are my solutions.  I don’t have any comments yet, but I thought I’d at least post once to see how this all works.  I have very little experience with programming, so my solutions aren’t particularly sophisticated.  Please let me know if I do something horribly wrong…

These gave me the right answers when I ran them with Python, but I might have distorted things when I posted them.  The spacing and indentation have changed.


while a< 1000:      if a%3==0 or a%5==0:      b=a+b a=a+1 print b [/sourcecode] [sourcecode language='python'] a,b,s=0,1,0 while a< 4000000:      if a%2==0:      s=s+a a,b=b,a+b print s [/sourcecode]