News for hackers. Stuff that matters.
Re:A new house number ... by antirez on Friday October 02, 2009 @07:22AM (#856782)
I had to write the following to understand this is going to converge to 6:

    >> 1/((1..1000000).map{|n| (1/((n*3.14)**2))}.inject{|a,b| a+b})
    => 5.99392169730553
Re:A new house number ... by RiderOfGiraffes on Friday October 02, 2009 @08:08AM (#856831)

    >>> from math import pi
    >>> 1/sum( [1/(pi*n)**2 for n in xrange(1,1000)] )
    6.00365160802217
Re:A new house number ... by jrockway on Friday October 02, 2009 @11:42AM (#857270)

    Prelude> 1/sum ([1..1000] >>= \x -> [(pi*x)**(-2)])
    6.003647956020607
Re:A new house number ... by apgwoz on Friday October 02, 2009 @08:21AM (#856848)
let's not get all fizzbuzz-y in here...
Re:A new house number ... by antirez on Friday October 02, 2009 @08:23AM (#856852)
Shorter but, "ad hoc" like everything in Python (the reason I never liked it). The Ruby version looks like math, if you know the concepts of map and reduce it's trivial to understand what's going on.

Compare this with sum() + "for n" and "xrange". Also if I remember some Python correctly "xrange" is almost like "range" but it's something like a lazy version, another ad-hoc stuff.

Sorry, didn't resisted.

Re:A new house number ... by RiderOfGiraffes on Friday October 02, 2009 @08:36AM (#856877)
Each to his/her own. This isn't intended to be a language war. If you prefer map and reduce then you can have them:

  >>> from math import pi
  >>> add = float.__add__
  >>> def f(x): return (pi*x)**(-2)
  >>> 1/reduce(add, map(f,range(1,1000)) )
  6.0036796863530508
Or, as a one-liner:

  >>> 1/reduce(float.__add__, map(lambda x:1.0/(3.1416*x)**2,range(1,1000)) )
People who aren't mathmos often find the list comprehensions more comprehensible, hence the version I wrote not as a competitor, but as an alternative, to yours.

Horses for courses. Personally, I've found the Python version to look more like math when I choose to express it like math, but I'm programming, so why not make it look like what I'm doing? Golf is a stupid game in programming. Understandability is the goal, but different readers find different things natural.

That's never going to go away.

Re:A new house number ... by antirez on Friday October 02, 2009 @09:14AM (#856930)
I guess it's really a matter of tastes indeed. My point is that, no matter the fact that I can try to mimic the Python idiomatic way or the reverse, there is a real difference between this languages, and is a difference that I see in many other languages, splitting them into two families: the "tools" languages that try to provide already build abstractions able to solve common problems, and the "concepts" languages where there are just a few rules you can combine to solve problems. I love the latter approach.
Re:A new house number ... by andrewparker on Friday October 02, 2009 @08:43AM (#856887)
Sorry if this is obvious, dusting off the math mind here... How is this solved without brute force computation?
Re:A new house number ... by RiderOfGiraffes on Friday October 02, 2009 @08:51AM (#856897)
It's neither obvious nor easy. It was an outstanding problem in the mid 1700's, and it was Euler that solved it but some superbly reckless arguments.

Euler's original paper:

http://www.17centurymaths.com/contents/euler/e041tr.pdf

The bottom of this page has a more modern treatment, although it takes a lot for granted:

http://www.math.wpi.edu/IQP/BVCalcHist/calc3.html

Another treatment of Euler's proof is here:

http://library.thinkquest.org/28049/Summing%20reciprocals.ht...

Re:A new house number ... by stilist on Friday October 02, 2009 @09:06AM (#856918)
Last link needs to swap %20 for the space.
Re:A new house number ... by RiderOfGiraffes on Friday October 02, 2009 @09:10AM (#856923)
Done - thanks.