Vampire Number

14 Jun, 2019
Introduction to Vampire Number and its implementation using python.
IntroductionIn 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. [Source Wiki]
Examples:
1260 is a vampire number, with 21 and 60 as fangs, since 21 × 60 = 1260.
126000 (which can be expressed as 21 × 6000 or 210 × 600) is not, as 21 and 6000 do not have the correct length, and both 210 and 600 have trailing zeroes
The vampire numbers are: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, ... (sequence A014575 in the OEIS)
There are many known sequences of infinitely many vampire numbers following a pattern, such as:1530 = 30×51, 150300 = 300×501, 15003000 = 3000×5001, ...
Condition for a number to be Vampire Number:
Has a pair number of digits. Lets call the number of digits : nYou can obtain the number by multiplying two integers, x and y, each with n/2 digits. x and y are the fangs.Both fangs cannot end simultaneously in 0.The number can be made with all digits from x and y, in any order and only using each digit once.
Has a pair number of digits. Lets call the number of digits : n
You can obtain the number by multiplying two integers, x and y, each with n/2 digits. x and y are the fangs.
Both fangs cannot end simultaneously in 0.
The number can be made with all digits from x and y, in any order and only using each digit once.
Pseudocode
if digitcount is odd return false
if digitcount is 2 return false
for A = each permutation of length digitcount/2 
        selected from all the digits,
  for B = each permutation of the remaining digits,
    if either A or B starts with a zero, continue
    if both A and B end in a zero, continue
    if A*B == the number, return true
# Python code to check if a number is Vampire# and printing Vampire numbers upto n using# itimport itertools as it  # function to get the required fangs of the# vampire numberdef getFangs(num_str):       # to get all possible orderings of order that    # is equal to the number of digits of the     # vampire number    num_iter = it.permutations(num_str, len(num_str))      # creating the possible pairs of number by     # brute forcing, then checking the condition     # if it satisfies what it takes to be the fangs     # of a vampire number    for num_list in num_iter:                v = ''.join(num_list)        x, y = v[:int(len(v)/2)], v[int(len(v)/2):]          # if numbers have trailing zeroes then skip        if x[-1] == '0' and y[-1] == '0':            continue          # if x * y is equal to the vampire number        # then return the numbers as its fangs        if int(x) * int(y) == int(num_str):            return x,y    return False  # function to check whether the given number is # vampire or notdef isVampire(m_int):      # converting the vampire number to string    n_str = str(m_int)      # if no of digits in the number is odd then     # return false    if len(n_str) % 2 == 1:        return False      # getting the fangs of the number    fangs = getFangs(n_str)    if not fangs:        return False    return True  # main driver programn = 16000for test_num in range(n):    if isVampire(test_num):        print ("{}".format(test_num), end = ", ")
Output:
1260, 1395, 1435, 1530, 1827, 2187, 6880, 

Refer to numberphile for more details:Vampire Numbers - Numberphile - YouTubeNumberphile4.12M subscribersVampire Numbers - NumberphileWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 5:40•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=3ZMnVd4ivKQ" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
References:
Rossetta CodeWikipedia – Vampire NumberStackoverflowPython Docs on itertools
Rossetta Code
Wikipedia – Vampire Number
Stackoverflow
Python Docs on itertools
This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Akanksha_Rai
number-digits
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Minimum number of jumps to reach end
Find minimum number of coins that make a given value
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Modulo 10^9+7 (1000000007)
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number