Just another Perl shrine PerlMonks

### Re^2: Recurring Cycle of Fractions

by Skeeve (Vicar)
 on Sep 09, 2007 at 17:45 UTC ( #637923=note: print w/replies, xml ) Need Help??

in reply to Re: Recurring Cycle of Fractions
in thread Recurring Cycle of Fractions

You're missing oh so much... Try your attempt with 17 and 19...

s\$\$([},&%#}/&/]+}%&{})*;#\$&&s&&\$^X.(\$'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`\$''`"e

Replies are listed 'Best First'.
Re^3: Recurring Cycle of Fractions
by shmem (Chancellor) on Sep 09, 2007 at 19:15 UTC
Try your attempt with 17 and 19...

I do. Since there aren't enough digits, Math::BigFloat to the rescue:

```#!/usr/bin/perl
use Math::BigFloat;
Math::BigFloat->div_scale(50);
my @values = map {
my \$i = Math::BigFloat->new( 1 );
scalar \$i->bdiv( \$_ );
} (2,3,6,7,9,11,13,14,17,19,23);

for (@values) {
/\.\d*?(\d+?)\1/;
print "\$_: \$1\n"
}
__END__
0.5:
0.33333333333333333333333333333333333333333333333333: 3
0.16666666666666666666666666666666666666666666666667: 6
0.14285714285714285714285714285714285714285714285714: 142857
0.11111111111111111111111111111111111111111111111111: 1
0.090909090909090909090909090909090909090909090909091: 09
0.076923076923076923076923076923076923076923076923077: 076923
0.071428571428571428571428571428571428571428571428571: 714285
0.058823529411764705882352941176470588235294117647059: 058823529411764
+7
0.052631578947368421052631578947368421052631578947368: 052631578947368
+421
0.043478260869565217391304347826086956521739130434783: 043478260869565
+2173913

The regex is the same, though. Still missing something?

update: Oh, I see. "try with 170..." :-)

--shmem

```_(\$_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
/\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.(\$e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;\$e.e && print}```

That's pretty much the same as my solution, so just a couple of things:

(1): The default precision with bignum (as I used) is good enough for up to 99, but if I want to go above, I need to use higher precision too. Your code fails even earlier. A constant div_scale of 50 only goes so far ... though of course when the list of numbers checked is also constant, I guess I can't fault it.

(2): Your code will break for numbers above 100 (even if you up the div_scale), since every float will begin with "0.00", and your code will see the repeated "0", and think it is the period ... no matter what follows it! Even if you hard code it so that you skip those initial zeros, it will find false positives for other numbers as well: 1/2048 == 0.00048828125, exact, but your code (if fixed to skip the zeros) will find a repeated digit "8" and assume it is the cycle sequence.

(I solve this problem by insisting that there are fewer than COUNT characters remaining after the match and making sure the string is big enough that we get at least two COUNTs worth of cyclying, which also requires keeping trailing zeros.)

So yeah, I guess you were still missing something. :)

print "Just another Perl \${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!

No. Don't try it with 170 but with another, carefully crafted fraction: 102/900
```#!/usr/bin/perl
use Math::BigFloat;
Math::BigFloat->div_scale(50);
my @values = map {
my \$i = Math::BigFloat->new( 102 );
scalar \$i->bdiv( \$_ );
} (900);

for (@values) {
/\.\d*?(\d+?)\1/;
print "\$_: \$1\n"
}
Output: 0.113333333333333333333...333: 1

s\$\$([},&%#}/&/]+}%&{})*;#\$&&s&&\$^X.(\$'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`\$''`"e

Create A New User
Node Status?
node history
Node Type: note [id://637923]
help
Chatterbox?
 [Discipulus]: a solution would be disabiltate sig as default and let users to show them [karlgoethebier]: now it's getting weird... [GotToBTru]: 'getting'? [Discipulus]: why karlgoethebier ?

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2017-06-22 12:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (519 votes). Check out past polls.