#!/usr/local/bin/perl5 -w ################################################################ ## ## Paul Garrett, garrett@math.umn.edu, 15 Sept 1997 ## ## Script to compute Friedman's "Index of Coincidence" of a string ## with shifts-forward of itself, in order to guess the period ## of a periodic substitution cipher ## ## Usage: ## ## $0 ## ## or ## ## $0 < ## ################################################################ ## ## First, let's count the actual coincidences, shifted by ? ## ################################################################# if ($in = shift) { chomp($in); } else { @in = ; $in = join(' ',@in); } $in = lc($in); $in =~ s/[^a-z]+//g; ## now it's all lowercase sans spaces @in = split(//,$in); $length = $#in; foreach $shift (1..80) { $number_coincidences = 0; foreach $i (0..$length-$shift) { if ($in[$i] eq $in[$i + $shift]) { $number_coincidences++; } } $out = int(10000 * $number_coincidences/($length-$shift+1))/100; print "Coincidences with shift ".$shift." = ".$out."\n" if ($out > 4.7); } print "\n"; #####################################