in reply to Re: Perl vs. Python for prime numbers
in thread Perl vs. Python for prime numbers

I observe that your inner loop only runs to $n - 1 while in the Python script it runs to n. The former makes sense to me but the latter not. I know little about Python, but running the inner loop to n should produce no primes found at all. Why does it work anyway (I checked it does)? I guess this is off topic as it is a Python question...

Replies are listed 'Best First'.
Re^3: Perl vs. Python for prime numbers
by choroba (Cardinal) on Jun 14, 2013 at 07:31 UTC
    I observerd that too, so I ran python and tried
    print(range(1,10))

    Lo and behold:

    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^3: Perl vs. Python for prime numbers
by LanX (Saint) on Jun 14, 2013 at 07:44 UTC
    Actually I copied choroba's code w/o caring about the difference, but yes, as he already showed, the range built-in in Python excludes the upper bound.

    Makes sense from a mathematical point of view (combining different ranges is easier) but I prefer the more intuitive Perl way to do it.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      ...the range built-in in Python excludes the upper bound. Makes sense from a mathematical point of view (combining different ranges is easier) but I prefer the more intuitive Perl way to do it.

      Theoretically, I prefer semi-open ranges [begin, end), aka half-open intervals, because:

      • The range size is simply "end - begin"
      • Empty ranges are expressed as "begin equals end" and so do not require special handling
      • Two subsequences are adjacent means that the upper bound of the one equals the lower bound of the other
      This theoretical superiority was eloquently expressed in hand-written notes by Edsger W Dijkstra in 1982, who further argued that zero (not one) is the natural first array subscript, as in [0, N). With typical attention to detail, I see that the three page numbers of Dijkstra's note are: 0, 1, and 2! :)

      In practice, I prefer Python semi-open ranges to the inclusive (closed) ranges emitted by the Perl and Ruby range operator. I remember finding Python's semi-open ranges nicer when golfing with string slices. After enjoying Python string slices, I miss them when coding in Perl; the closest Perl equivalent, the substr function, seems unwieldy by comparison.

      Semi-open ranges also feel comfortable to me because they form a crucial part of C++ STL, in particular iterators, which in turn were influenced by C pointers and arrays. Stepanov extended some common (semi-open) C idioms, such as:

      for (i = 0; i < N; ++i) { // a[i] ... } for (ptr = a; ptr < a+N; ++ptr)
      inventing a more general iterator abstraction:
      for (iter = begin; iter != end; ++iter)
      thus enabling STL algorithms to work on any container that implements the iterator interface.

      References

        "Edsger"! Really?

        Wow I didn't know that! 8-o

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        PS: Just kidding ;-)... thanks for the pointers. =)