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

Re: Determining if a rational number terminates

by tobyink (Abbot)
on Nov 29, 2012 at 16:27 UTC ( #1006294=note: print w/ replies, xml ) Need Help??


in reply to Determining if a rational number terminates

Nicely obfuscated, but this is shorter:

$_=pop;$_/=2until$_%2;print/(5|^1)$/?Y:N,$/
$_=pop;for$.(2,5){$_/=$.until$_%$.}print/^1$/?Y:N,$/

Of course, neither of these works for the fraction 3/6 which of course terminates as 0.5. For that to work, you'd need to add an initial step to reduce the fraction to its lowest terms.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Determining if a rational number terminates
Select or Download Code
Re^2: Determining if a rational number terminates
by tobyink (Abbot) on Nov 29, 2012 at 16:52 UTC

    There are probably shorter ways of doing it, but...

    # It is important to clearly comment your code to make it easier # for future maintainers. # # This is the numerator, denominator pair for which we wish to # determine if the decimal expansion terminates. # @ARGV = (33, 660); # Check if it terminates and print "Y" or "N". # sub _{@i=sort{$b-$a}@_;return$i[1]if$i[0]==$i[1];@i=($i[1],$i[0]%$i [1])while$i[1];$i[0]};sub __{$j=_@_;$_/=$j for@_;$_[1]<0and do{$_*= -1for@_}}__(($k,$l)=@ARGV);$l/=2until$l%2;print$l=~/(5|^1)$/?Y:N,$/
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Thanks for that, I didn't even consider that it wouldn't be in lowest form.

      It's really clever to take advantage of the fact that powers of five always end with five, I suppose then it's better to divide out the powers of two first than the powers of five. Tomorrow I will ++ both of these :D

        Meh... I was actually only checking multiples of five. D'oh! 1/15 was reporting "Y".

        Fixed...

        @ARGV = (15, 40); sub _{@i=sort{$b-$a}@_;return$i[1]if$i[0]==$i[1];@i=($i[1],$i[0]%$i[1 ])while$i[1];$i[0]}sub __{$j=_@_;$_/=$j for@_;$_[1]<0and do{$_*=-1for @_}}__(($k,$l)=@ARGV);for(2,5){$l/=$_ until$l%$_}print$l eq 1?Y:N,$/#
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      +1 for ironic comment on comments.

      As we all know, the uncommented part is the part that needs the comment here. (And if you say "the code is obvious and needs no comments", it's my sincere opinion you need to get out more.)

        Actually, if you fix up the whitespace and rename the functions:

        • _ => highest_common_factor
        • __ => canonical_fraction

        ... it's not too bad.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^2: Determining if a rational number terminates
by trizen (Friar) on Dec 25, 2012 at 17:26 UTC
    Shorter still:

    perl -E'$_=10;$_/=5until$_%5;say$_&$_-1?N:Y'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-09-21 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls