GonzaloXavier - 1 month ago 15x
Python Question

# Python vampire number

First time posting in this community. I recently started to learn Python (2 weeks) and as a way to practice a classmate gave me a task: "Write a program that checks if an integer is a vampire number.
For an integer to be a vampire number(v) it needs to meet these 4 criteria:

1)Has a pair number of digits. Lets call the number of digits:n

2)You can obtain v by multiplying two integers,x and y, each with n/2 digits. x and y are the fangs.

3)Both fangs cannot end simultaneously in 0.

4)v can be made with all digits from x and y, in any order and only using each digit once.

Example:

21*60=1260 <--Vampire number

210*600=126000 <--Not a vampire number

The first vampire numbers are the following: 1260, 1395, 1435, 1530, 1827, 2187, 6880, 102510, 104260, 105210, 105264, 105750, 108135, 110758, 115672, 116725, 117067, 118440, 120600, 123354, 124483, 125248, 125433, 125460, 125500, 126027, 126846, 129640, ...

So far I've made a program that can achieve the first 3 criteria(I think). I'm looking for help on the last one.

This is what I've got: (Sorry for some stuff in Spanish)

``````v=int(input("Enter number to test for vampire:"))
#Test for pair number of digits
def nd(a):
nd = 0
while a != 0:
d = a % 10
if d != 0:
nd += 1
a = a // 10
return nd
def DigitosPar(a):
if nd(a)%2==0:
return 1
else:
return 0
#Last digit is 0
def UltimoDigCero(b):
ud = 0
ud = b % 10
if ud==0:
return 1
else:
return 0

if DigitosPar(v)==1:
x=[]
for i in range(int(10**(nd(v)/2-1)),int(10**(int(nd(v))/2))):
x.append(i)
y=x
z=0
posiblex=0
posibley=0
for ia in range(0,len(y)):
for ib in range(0,len(x)):
z=y[ia]*x[ib]
if z==v and not((UltimoDigCero(x[ib])==1 and UltimoDigCero(y[ia])==1)):
posiblex=x[ib]
posibley=y[ia]
print(v,"has as fangs",posiblex,posibley)
if posiblex==0:
print(v, "not a vampire")
else:
print(v, "not a vampire")
``````

1260 is a vampire number because the divisors can be concatenated into a permutation of 1260. You can do it this way

``````v=int(input("Enter number to test for vampire:"))

from collections import Counter

def is_anagram(a, b):
if len(a) != len(b):
return False
return Counter(a) == Counter(b)

import math
for x in range(0,int(math.pow(10, len(str(v))/2))):
for y in range(0,int(math.pow(10, len(str(v))/2))):
if (x*y == v):
#print('Fangs: %d %d' % (x, y))
if (is_anagram(str(str(x)+''+str(y)), str(v)) ):
print('Vampire')
``````

To list the first `n`vampire numbers, you can use the above code as a function and incrementally test the integers.

Source (Stackoverflow)