Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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
share|improve this question
I've decided not to include Prime vampire's, i won't go overboard with it. – st0le Jan 28 '11 at 11:05
Maybe i should add Printing if its a Vampire or a PseudoVampire, what do you guys say? – st0le Jan 28 '11 at 11:07
What about multiple pairs of fangs? – gnibbler Jan 28 '11 at 11:42
@gnibbler, i'll ammend it. – st0le Jan 28 '11 at 13:28
If number has several pair, does their order important? I mean 125460 = 204 * 615 = 246 * 510 or 125460 = 246 * 510 = 204 * 615? Or doesn't matter? – Nakilon Jan 28 '11 at 19:53
show 2 more comments

2 Answers

up vote 3 down vote accepted

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"
share|improve this answer
Also doesn't handle multiple pairs (for 125460) and space missed after =. – Nakilon Jan 28 '11 at 20:12
(checked here) – Nakilon Jan 28 '11 at 20:22
@Nakilon, this answer predates the amendment for multiple pairs – gnibbler Jan 29 '11 at 0:02

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.")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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