Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Actually, every rational number has a recurring cycle in a positional representation ... it's just that for some numbers it is a trivial "0" cycle. But that's just a quibble.

Assuming that the provided string is long enough for two cycles to appear gets boring pretty quickly ... on my system that assumption fails already for 1/17, unless I use bignum. So I'll do that. And, true to habit, I'll use a regex to solve the problem. :)

(Pass an argument to the program to get the cycle for the inverse of more (or less) than the default 99 first natural numbers:)

#!/usr/bin/perl use strict; use warnings; use constant COUNT => $ARGV[0]||99; use constant LENGTH => length COUNT; use bignum a => 3*COUNT; # accuracy ... for my $n (1..COUNT) { my $f = substr(1/$n, 0, 3*COUNT); # precision too ... if ( $f =~ /(\d+?)\1+(?!\d{${\COUNT}})/ ) { # a little leeway ... printf " 1/%-*d = %-30.30s -> %s\n", LENGTH, $n, $f, $1; } else { # should never happen ... die "Insufficient precision/accuracy? Cannot handle the following +:\n$f\n"; } }

Thank you — that was an interesting distraction. :)

Update: Made the leeway big enough that we get the cycle sequence as it first appears, and not some arbitrary cycling of it, no matter how long the sequence.

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


In reply to Re: Recurring Cycle of Fractions by Sidhekin
in thread Recurring Cycle of Fractions by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (9)
    As of 2014-09-17 08:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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











      Results (70 votes), past polls