#!/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 () { # 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 #### 1/2 : 0.50 1/3 : 0.333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 => 3 1/6 : 0.166666666666666666666666666666666666666666666666666666666666666666666666666666666666666 => 6 1/7 : 0.142857142857142857142857142857142857142857142857142857142857142857142857142857142857142 => 142857 1/14 : 0.071428571428571428571428571428571428571428571428571428571428571428571428571428571428571 => 714285 1/17 : 0.058823529411764705882352941176470588235294117647058823529411764705882352941176470588235 => 0588235294117647 1/23 : 0.043478260869565217391304347826086956521739130434782608695652173913043478260869565217391 => 0434782608695652173913 1/29 : 0.034482758620689655172413793103448275862068965517241379310344827586206896551724137931034 => 0344827586206896551724137931 1/47 : 0.021276595744680851063829787234042553191489361702127659574468085106382978723404255319148 1/48 : 0.020833333333333333333333333333333333333333333333333333333333333333333333333333333333333 => 3 1/49 : 0.020408163265306122448979591836734693877551020408163265306122448979591836734693877551020 => 020408163265306122448979591836734693877551 1/196 : 0.005102040816326530612244897959183673469387755102040816326530612244897959183673469387755 => 510204081632653061224489795918367346938775 1/197 : 0.005076142131979695431472081218274111675126903553299492385786802030456852791878172588832 1/224 : 0.004464285714285714285714285714285714285714285714285714285714285714285714285714285714285 => 428571 1/467 : 0.002141327623126338329764453961456102783725910064239828693790149892933618843683083511777 => 7 : also works with arbitrary strings: : thisisjustblahblahblahb => blah : yadda, yadda, yadd => yadda,