in reply to Re: Vampire Numbers Revisited
in thread Vampire Numbers Revisited
On my system the main execution time seems to be the time to load Perl. It can find both answers of length 10 in under a second.#! /usr/bin/perl -w use strict; my $length = shift || 6; unless ($length =~ /^\d*[02468]\z/) { die "Length must be an even number"; } $length /= 2; my $low = join '', 1, map 0, 2..$length; my $high = join '', map 9, 1..$length; for my $first ($low..$high) { my $last = int(sqrt($first.$low)) - $first; $last = $low if $last < $low; my $square = ($last + $first)*($last + $first); while ($square < $first . $last and $last <= $high) { $last++; $square = ($last + $first)*($last + $first); } if ($last <= $high and $square == $first . $last) { print "$first$last\n"; } }
|
---|