More recreational math here. Again I was reading some
book by
Clifford Pickover,
and heard about Vampire Numbers.
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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.