Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Determining if a rational number terminates

by ColonelPanic (Friar)
on Nov 30, 2012 at 09:36 UTC ( #1006439=note: print w/ replies, xml ) Need Help??


in reply to Determining if a rational number terminates

Here's a short one:

use bigrat;print((shift()*10**1000/shift())=~?/??'N':'Y')
Is this blatant cheating? Perhaps :)


When's the last time you used duct tape on a duct? --Larry Wall


Comment on Re: Determining if a rational number terminates
Download Code
Re^2: Determining if a rational number terminates
by tobyink (Abbot) on Nov 30, 2012 at 10:07 UTC

    This produces a deprecation warning in recent versions of Perl thanks to ?/?. You can use '/' or "/" instead to avoid the deprecation warning. Perl is smart enough to know you meant a regexp rather than a string if it follows =~.

    bigrat is in core, so I don't consider it cheating. Now, if you wrote chkrat.pm, and uploaded it to CPAN specifically to help your golf score, that would be cheating.

    However, I'm disappointed by the lack of a line break.

    Here's my reinterpretation of your implementation. I've added a line break at the cost of three keystrokes, but managed to save six keystrokes elsewhere...

    use bigrat;print((shift()*10**1000/shift)=~"/"?N:Y,$/)

    These are also quite cute, though very slightly longer...

    use bigrat;print[Y,N]->[(shift()*10**1000/shift)=~"/"],$/
    use bigrat;$_=shift()*10**1000/shift;print[Y,N]->[m"/"],$/
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Thanks for your suggestions. Here is a yet shorter version:
      use bigrat;print+(1e1000/pop()*pop)=~"/"?N:Y,$/


      When's the last time you used duct tape on a duct? --Larry Wall

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2015-07-03 03:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls