Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Vampire Numbers Revisited

by Roy Johnson (Monsignor)
on Mar 23, 2005 at 23:06 UTC ( [id://441913]=note: print w/replies, xml ) Need Help??


in reply to Vampire Numbers Revisited

Using a trick from the web site you linked to, this runs very quickly to get the 7 4-digit vampire numbers. $digits is the length of the factors; set it to 3 and it got the 6-digit numbers in a reasonable time, too (939658 = 986 * 953, for the largest example).
my $digits = 2; my @mods; for ([0,0],[2,2],[3,6],[5,8],[6,3],[8,5]) { $mods[$_->[0]] = $_->[1]; } my $min_factor = 1 . (0 x ($digits-1)); for my $a ($min_factor .. (9 x $digits)) { if (defined $mods[$a % 9]) { for (my $b = $min_factor - 1 + ($mods[$a % 9]||9); $b <= $a; $ +b += 9) { my $prod = $a * $b; my $fdigits = join '', sort map {split //} $a, $b; my $pdigits = join '', sort split //, $prod; if ($pdigits eq $fdigits) { print "$a * $b = $prod\n"; } } } }
Update: better way of excluding short factors.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Vampire Numbers Revisited
by tall_man (Parson) on Mar 24, 2005 at 20:17 UTC
    I made an optimized version based on some of the suggestions on the web site, such as storing the digit counts in bit fields, unrolling the inner loop, and making use of caching.

    It can find the 112,025 ten-digit vampire numbers (duplicated values included) in about an hour and a half on my machine. Since the problem takes about 100 times longer with each digit, going any higher would probably take recoding the inner loops in C.

    Note: because of the possibility of overflow in the 3-bit digit bins, there may be some false positives -- they should be screened out with another script. I found none when I tried this.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://441913]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found