Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: solve cubic equations (Python)

by no_slogan (Deacon)
on May 04, 2017 at 13:37 UTC ( #1189497=note: print w/replies, xml ) Need Help??


in reply to Re^3: solve cubic equations (Python)
in thread solve cubic equations

Try and solve a few problems on hackerrank.com that way. Their cpu time restrictions are usually too stringent for Math::BigInt, even with a "fast" backend. Python ints are built-in, bignum is an afterthought.

Replies are listed 'Best First'.
Re^5: solve cubic equations (Java)
by choroba (Bishop) on May 04, 2017 at 15:59 UTC
    Show me a hackerrank problem that can't be solved in Perl because of time restrictions.

    The only one I haven't been able to solve in Perl was PRNG Sequnce Guessing, so I solved it in Java. But the problem wasn't the time constraint, but the different behaviour of bit operators on big numbers.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Now that I've had some time to kick it around, this is actually a great example. My Python program solves the sample problem in 1 second. In Perl, using Math::BigInt with the default backend, it takes 10 seconds. With the GMP backend, 5 seconds. With Math::GMP, 1 second, same as Python. By resorting to vile trickery, I was able to get the Perl to run in 0.5 seconds with no modules at all. Here's the kicker, though: the Python program runs in 0.1 second under PyPy (basically JIT compiled Python).
        Talking about JIT, maybe try benchmarking a JS version too. :)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

        That's intriguing. I admit I've never used Perl's bignum or bigint features for anything serious. Are you able to post short benchmark programs for this? It would be interesting to profile and figure out where the bottleneck is. Which version of Perl, by the way?

      Oh look, it's basically the same problem I solved in my old node, Predict Random Numbers. Fifteen years ago. Back then, there wasn't so much information available online, so I actually had to get on my bike and ride to the Stanford math department library to find that algorithm.

      That problem involves 48-bit numbers, which really aren't all that big. Maybe try something in the Number Theory category.

      Update: Haha, we didn't have youtube back then either.

        Hey! That was a great node. Lattice basis reduction? Why don't you take a crack at >this< Subset Sum problem? :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1189497]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2019-05-20 20:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (129 votes). Check out past polls.

    Notices?