Do you know where your variables are? 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'

Replies are listed 'Best First'.
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

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 (Hermit) on Dec 25, 2012 at 17:26 UTC
Shorter still:

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

Create A New User
Node Status?
node history
Node Type: note [id://1006294]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2018-02-21 00:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (274 votes). Check out past polls.

Notices?