http://www.perlmonks.org?node_id=637949


in reply to Recurring Cycle of Fractions

Just for fun, here's another approach, based on the idea of XOR-ing the string with shifted versions of itself, to then extract the recurring substring from the case with the longest trailing "nulled-out" part. Hope you'll appreciate the creative aspect :) — even though it's not as compact as the regex approach suggested by ikegami.  Due to its generic approach, the algorithm should work not only with numbers, but with any kind of string.

#!/usr/bin/perl my @vals; { # set up some high-precision fractions use constant PREC => 89; use bignum p => -(PREC); for my $num (2, 3, 6, 7, 14, 17, 23, 29, 47, 48, 49, 196, 197, 224, 467) { my $s = 1/$num; $s =~ s/0+$/0/; $s = substr($s, 0, PREC); push @vals, ["1/$num", $s]; } } while (<DATA>) { # add some non-numeric stuff, just as demo chomp; push @vals, ["", $_]; } for my $e (@vals) { my ($frac, $s) = @$e; my $ls = length $s; my $ss = $s; my $lmin = $ls; my $i = 0; my $n = 0; my $c = 0; while ($c++ < $ls) { last if $c > $lmin-$n; $ss = substr "\x01$ss",0,$ls; my $xor = $s ^ $ss; $xor =~ s/\0+$//; my $l = length $xor; if ($l < $lmin) { $lmin = $l; next if $l+$c > $ls; $i = $l-$c; $i = 0 if $i<0; $n = $c; } } printf "%-5s : %s", $frac, $s; printf "\n => %s%s", " "x$i, substr $s, $i, $n if $n; print "\n"; } __DATA__ also works with arbitrary strings: thisisjustblahblahblahb yadda, yadda, yadd

prints:

1/2 : 0.50 + 1/3 : 0.333333333333333333333333333333333333333333333333333333333333 +333333333333333333333333333 => 3 1/6 : 0.166666666666666666666666666666666666666666666666666666666666 +666666666666666666666666666 => 6 1/7 : 0.142857142857142857142857142857142857142857142857142857142857 +142857142857142857142857142 => 142857 1/14 : 0.071428571428571428571428571428571428571428571428571428571428 +571428571428571428571428571 => 714285 1/17 : 0.058823529411764705882352941176470588235294117647058823529411 +764705882352941176470588235 => 0588235294117647 1/23 : 0.043478260869565217391304347826086956521739130434782608695652 +173913043478260869565217391 => 0434782608695652173913 1/29 : 0.034482758620689655172413793103448275862068965517241379310344 +827586206896551724137931034 => 0344827586206896551724137931 1/47 : 0.021276595744680851063829787234042553191489361702127659574468 +085106382978723404255319148 1/48 : 0.020833333333333333333333333333333333333333333333333333333333 +333333333333333333333333333 => 3 1/49 : 0.020408163265306122448979591836734693877551020408163265306122 +448979591836734693877551020 => 020408163265306122448979591836734693877551 1/196 : 0.005102040816326530612244897959183673469387755102040816326530 +612244897959183673469387755 => 510204081632653061224489795918367346938775 1/197 : 0.005076142131979695431472081218274111675126903553299492385786 +802030456852791878172588832 1/224 : 0.004464285714285714285714285714285714285714285714285714285714 +285714285714285714285714285 => 428571 1/467 : 0.002141327623126338329764453961456102783725910064239828693790 +149892933618843683083511777 => + 7 : also works with arbitrary strings: + : thisisjustblahblahblahb + => blah : yadda, yadda, yadd + => yadda,

BTW, I'll admit it up-front: in its current version, this algorithm has a subtle problem. As you can see when looking at the last number (1/467), it falsely reports '7' as the recurring part, as a result of the number being cut off prematurely due to insufficient precision... (a problem it shares with the regex solution, btw). Presumably some heuristic workaround can be found for that, but I'll leave this as an 'exercise for the reader'... ;)