• Lengthy numbers

    From Gilmeh Serda@21:1/5 to All on Sat Dec 21 11:49:30 2024
    Was just playing with numbers and stumbled on something I've never seen
    before.

    9**9**2 196627050475552913618075908526912116283103450944214766927315415537966391196809

    9**9**3 439328503696464329829774782657072712058010308177126710621676697750466344744764 029773301412612482563729435064854354299095570379503452515853238520182740967398 746503532324400000659505126023955913142968176998364877699089666171297275956245 407453033190168644894850576346492691458695174281789557994923607783461486426448 617667076393901104477324982631297641034277093818692823488603426279473674368943 609268871793467206677285688478458498235002859256706389043030847945506577080623 430066283504397583789044245429585982964571774605868466160379567432725704121260 940939343217905975847365096315872153240969882363435363449775254393010368267343 970426230801390250903399147001650831878665172798468587509747439118815689

    9**9**4
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: Exceeds the limit (4300 digits) for integer string conversion;
    use sys.set_int_max_str_digits() to increase the limit

    Explanation: https://discuss.python.org/t/int-str-conversions-broken-in-latest-python-bugfix-releases/18889


    Though, it would have been funnier had it been 4400...

    --
    Gilmeh

    The best definition of a gentleman is a man who can play the accordion --
    but doesn't. -- Tom Crichton

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Gilmeh Serda on Sat Dec 21 21:03:52 2024
    On Sat, 21 Dec 2024 11:49:30 GMT, Gilmeh Serda wrote:

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: Exceeds the limit (4300 digits) for integer string
    conversion;
    use sys.set_int_max_str_digits() to increase the limit

    Explanation: https://discuss.python.org/t/int-str-conversions-broken-in-latest-python-bugfix-releases/18889

    This is a feature, not a bug. It’s to guard against an attacker
    exhausting system resources (CPU) by forcing the execution of an O(N²) operation with large N.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gilmeh Serda@21:1/5 to Lawrence D'Oliveiro on Sun Dec 22 09:18:26 2024
    On Sat, 21 Dec 2024 21:03:52 -0000 (UTC), Lawrence D'Oliveiro wrote:

    not a bug.

    Who said anything about a bug?

    --
    Gilmeh

    Barbie says, Take quaaludes in gin and go to a disco right away! But Ken
    says, WOO-WOO!! No credit at "Mr. Liquor"!!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Oscar Benjamin on Mon Dec 23 08:02:08 2024
    Oscar Benjamin <oscar.j.benjamin@gmail.com> wrote or quoted:
    x = [0]; x.extend(iter(x))

    Yo, that's a gnarly piece of Python code!

    This code whips up an infinite list, but in a pretty slick way.

    Here's the lowdown:

    - First off, we're cooking up a list x with just one ingredient: 0

    - Then we're using extend() to pile more stuff onto x

    - The mind-bender is what we're extending with: iter(x)

    iter(x) spins up an iterator from the list x. When we extend x
    with this iterator, it starts tossing the elements of x back into
    itself. But here's the kicker: as x grows, the iterator keeps
    chugging along, creating an endless loop.

    So in theory, this would crank out an infinite list that looks like
    [0, 0, 0, 0, ...]. In the real world, though, if you try to print or
    use this list, your program will probably freeze up faster than the
    405 at rush hour.

    It's a pretty clever (and kind of diabolical) way to create an
    infinite data structure. Just watch out using something like
    this in actual code - it could cause some serious brain freeze!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Oscar Benjamin on Mon Dec 23 15:03:01 2024
    Oscar Benjamin <oscar.j.benjamin@gmail.com> writes:
    To solve that problem conversions from string->int and
    int->string were disallowed.

    The obvious conversion algorithms use quadratic time but it seems to me
    that O(n log n) is doable with some careful programming. For example,
    people have computed pi to billions or trillions of decimal places.

    I believe GMP uses the Toom-Cook algorithm for multiplication once the
    numbers are big enough. I don't know whether that is also used for
    decimal conversions.

    Page 14 of http://maths-people.anu.edu.au/~brent/pd/rpb032.pdf discusses
    base conversion. That link is from a stackexchange thread that I found
    with web search.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Gilmeh Serda on Mon Dec 23 23:06:16 2024
    On Sun, 22 Dec 2024 09:18:26 GMT, Gilmeh Serda wrote:

    On Sat, 21 Dec 2024 21:03:52 -0000 (UTC), Lawrence D'Oliveiro wrote:

    This is a feature, not a bug.

    Who said anything about a bug?

    Not sure what this is relevant to, but ... you tell me.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Stefan Ram on Mon Dec 23 15:38:13 2024
    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    So in theory, this would crank out an infinite list that looks like
    [0, 0, 0, 0, ...]. In the real world, though, if you try to print or
    use this list, your program will probably freeze up faster than the
    405 at rush hour.

    The freeze-up happens as soon as you create the list. Unfortunately
    iterators don't have anything like .extend so I don't see a way to
    create recursive ones like that, short of writing more verbose code
    using yield. In Haskell it is straightforward:

    fibonacci = 0 : 1 : zipWith (+) (tail fibonacci)

    main = print (take 10 fibonacci)

    should print [0,1,1,2,3,5,8,13,21,34].

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Paul Rubin on Tue Dec 24 12:26:26 2024
    Paul Rubin <no.email@nospam.invalid> wrote or quoted:
    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    So in theory, this would crank out an infinite list that looks like
    [0, 0, 0, 0, ...]. In the real world, though, if you try to print or
    use this list, your program will probably freeze up faster than the
    405 at rush hour.
    The freeze-up happens as soon as you create the list.

    I'm stoked you caught that! You hit the nail on the head -
    that resource-hogging calculation goes down when the expression's
    evaluated. No need to "print or use this list" to see it happen.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)