Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Recurring Cycle of Fractions

by shmem (Chancellor)
on Sep 09, 2007 at 17:32 UTC ( #637922=note: print w/replies, xml ) Need Help??

in reply to Recurring Cycle of Fractions

Am I missing something, or is a simple regex all that is needed?
my @values = map { 1 / $_ } (2,3,6,7,9,11,13,14); for (@values) { /\.\d*?(\d+?)\1/; print "$_: $1\n" } __END__ 0.5: 0.333333333333333: 3 0.166666666666667: 6 0.142857142857143: 142857 0.111111111111111: 1 0.0909090909090909: 09 0.0769230769230769: 076923 0.0714285714285714: 714285


_($_=" "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}

Replies are listed 'Best First'.
Re^2: Recurring Cycle of Fractions
by Skeeve (Vicar) on Sep 09, 2007 at 17:45 UTC
    You're missing oh so much... Try your attempt with 17 and 19...

      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..." :-)


      _($_=" "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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://637922]
[james28909]: i know but scroll through your calendar on your computer.
[james28909]: i thiught you were going to say make both hands into a fist and puch yourself in the face
[atcroft]: .oO(Sorry, I probably should have said take two normal hands....)
[atcroft]: james28909: No, unless you are a politician, I wouldn't say that (and even if you are, I still probably wouldn't say that).
[james28909]: i mean how hard can it be? its just subtracting days lol
[atcroft]: james28909: What about October 5, 1582?
[stevieb]: atcroft: "Make both hands into fists..."... is something my Ma taught me in our native lang, but I was to ignorant and young to pay attention. Thanks for that :)
[atcroft]: stevieb: Sad to say that I only recently learned that particular trick, but I have since found it very useful.... :)
[james28909]: ill be back with a solution eventually
[stevieb]: it's a reminder to re-inforce it :P

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-04-29 04:33 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.