A Vampire Number is equal to a product of it's digits like 1435 = 41 * 35. I think the name comes from a reference to such a number in an Anne Rice novel.
So I went hunting for more vamps. This program only looks for products of two factors, it would be interesting to look for products of more than two factors.
Algorithm is basically to generate a list of orderings of the digits (1234, 1243, 1342, 1324, ...), not taking the trouble to eliminate duplicates.
Next each ordering is split at each digit and tested (1*234, 12*34, 123*4).
Of note is 153. In addition to being a Triangle Number (1+2+3+...+17), it is also a self-cube-referential number (1**3 + 5**3 + 3**3), a sum of factorials (1! + 2! + 3! + 4! + 5!), and a Vampire Number (3 * 51).
VampFoo
Update: The book is 'The Loom of God'. A 40-digit V-num is listed there: 98765432198765432198 * 98765432198830604534 = 9754610597415368368844499268390128385732, whew!
#!/usr/bin/perl use strict; if (@ARGV < 2) { print STDERR "\nUsage: $0 firstnumber lastnumber\n\n"; } my ($beg, $end) = @ARGV; my ($test, $this, $that); for $test ($beg..$end) { ($this, $that) = factors($test); if ($this ne '') { print "$test = $this * $that\n"; } } #----------------------------------------------------------- sub factors { my ($target) = @_; my ($order, $olist); my ($split, $slist); $olist = orderings($target); for $order (@{$olist}) { #print "$order\n"; $slist = splittings($order); for $split (@{$slist}) { #print "$split->[0] $split->[1]\n"; if ($split->[0] * $split->[1] == $target) { return ($split->[0], $split->[1]); } } } } #----------------------------------------------------------- sub splittings { my ($num) = @_; my (@digits) = split('', $num); my (@list, @useds); while (@digits > 1) { push(@useds, shift(@digits)); push (@list, [join('', @useds), join('', @digits)]); } return \@list; } #----------------------------------------------------------- sub orderings { my ($num) = @_; my (@digits) = split('', $num); my (@list, $sublists, $sub, $this); if (@digits == 1) { return [$digits[0]]; } for (1..@digits) { $this = shift(@digits); $sublists = orderings(join('', @digits)); for $sub (@{$sublists}) { push(@list, "$this$sub"); } push(@digits, $this); } return \@list; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Vampire Numbers
by Abigail-II (Bishop) on Jun 11, 2002 at 12:53 UTC | |
by Anonymous Monk on Jun 11, 2002 at 17:26 UTC | |
by Anonymous Monk on Jun 11, 2002 at 17:29 UTC | |
Re: Vampire Numbers
by particle (Vicar) on Jun 11, 2002 at 01:02 UTC | |
Factors
by gumby (Scribe) on Jun 11, 2002 at 18:19 UTC | |
by kvale (Monsignor) on Jun 12, 2002 at 21:01 UTC | |
by gumby (Scribe) on Jun 12, 2002 at 21:45 UTC | |
by YuckFoo (Abbot) on Jun 12, 2002 at 22:30 UTC | |
by kvale (Monsignor) on Jun 13, 2002 at 04:51 UTC | |
by gumby (Scribe) on Jun 13, 2002 at 15:43 UTC | |
by gumby (Scribe) on Jun 15, 2002 at 19:21 UTC | |
Re: Vampire Numbers
by gumby (Scribe) on Jun 13, 2002 at 16:19 UTC |