#!/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