11
\$\begingroup\$

EDIT: In the interest of increasing the complexity, i've added more to the challenge.

In mathematics, a vampire number (or true vampire number) is a composite natural number v, with an even number of digits n, that can be factored into two integers x and y each with n/2 digits and not both with trailing zeroes, where v contains precisely all the digits from x and from y, in any order, counting multiplicity. x and y are called the fangs.

More about Vampire Number

Pseudovampire numbers

Pseudovampire numbers are similar to vampire numbers, except that the fangs of an n-digit pseudovampire number need not be of length n/2 digits. Pseudovampire numbers can have an odd number of digits, for example 126 = 6×21.

Input

Accept Numbers from command line or stdin

Output

  • "1260 = 21 * 60" (smaller fang first if the number is a Vampire.)
  • "1261 is not a Vampire Number." (if the number is not a Vampire number)
  • "126 = 6 * 21". (if the number is a Pseudovampire number)

EDIT: If the number has multiple fangs, display it so.

x = fang1a * fang1b = fang2a * fang2b
\$\endgroup\$
7
  • \$\begingroup\$ I've decided not to include Prime vampire's, i won't go overboard with it. \$\endgroup\$
    – st0le
    Commented Jan 28, 2011 at 11:05
  • \$\begingroup\$ Maybe i should add Printing if its a Vampire or a PseudoVampire, what do you guys say? \$\endgroup\$
    – st0le
    Commented Jan 28, 2011 at 11:07
  • \$\begingroup\$ What about multiple pairs of fangs? \$\endgroup\$
    – gnibbler
    Commented Jan 28, 2011 at 11:42
  • \$\begingroup\$ @gnibbler, i'll ammend it. \$\endgroup\$
    – st0le
    Commented Jan 28, 2011 at 13:28
  • 1
    \$\begingroup\$ @Nakilon, print them in the increasing order. fang1a appears before fang2a when fang1a < fang2a \$\endgroup\$
    – st0le
    Commented Jan 29, 2011 at 5:58

2 Answers 2

5
\$\begingroup\$

Python - 188 chars

Doesn't do Pseudovampire numbers

from itertools import*
n=input()
a=[]
for i in map("".join,permutations(`n`)):x,y=int(i[::2]),int(i[::-2]);a+=[(x,y)]*(x*y==n)
print n,a and"=%s*%s"%min(a)or"is not a Vampire Number"
\$\endgroup\$
3
  • \$\begingroup\$ Also doesn't handle multiple pairs (for 125460) and space missed after =. \$\endgroup\$
    – Nakilon
    Commented Jan 28, 2011 at 20:12
  • \$\begingroup\$ (checked here) \$\endgroup\$
    – Nakilon
    Commented Jan 28, 2011 at 20:22
  • \$\begingroup\$ @Nakilon, this answer predates the amendment for multiple pairs \$\endgroup\$
    – gnibbler
    Commented Jan 29, 2011 at 0:02
4
\$\begingroup\$

Ruby, 190 chars

o=[]
[*x.chars].permutation{|r|a=r.pop(x.size/2).join.to_i
r=r.join.to_i
o|=[[a,r]]if a<=r&&a*r==x.to_i}
puts x+(o.any? ? o.map{|i|" = "+i*" * "}*"":" is not a Vampire Number.")
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.