# detects if a binary string 1101010010001... # has a Fibonacci spacing 0,1,1,2,3,5,... of the 1's $x = "1101010010001000001"; $s0 = 0; $s1 = 1; # initial conditions print "It is a Fibonacci sequence\n" if $x =~ /^1 # match an initial '1' ( (??{'0' x $s0}) # match $s0 of '0' 1 # and then a '1' (?{ $largest = $s0; # largest seq so far $s2 = $s1 + $s0; # compute next term $s0 = $s1; # in Fibonacci sequence $s1 = $s2; }) )+ # repeat as needed $ # that is all there is /x; print "Largest sequence matched was $largest\n";