Open In App

Vampire Number

Improve
Improve
Like Article
Like
Save
Share
Report

Introduction to Vampire Number and its implementation using python. Introduction 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. [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:

  1. Has a pair number of digits. Lets call the number of digits : n
  2. You can obtain the number 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. 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

C++14




#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
 
using namespace std;
 
// function to get the required fangs of the vampire number
pair<string, string> getFangs(string num_str) {
 
    // to get all possible orderings of order that is equal to the number of digits of the vampire number
    vector<string> num_vec;
    for (char c : num_str) {
        num_vec.push_back(string(1, c));
    }
    sort(num_vec.begin(), num_vec.end());
    do {
        string x_str = "", y_str = "";
        for (int i = 0; i < num_str.size() / 2; i++) {
            x_str += num_vec[i];
        }
        for (int i = num_str.size() / 2; i < num_str.size(); i++) {
            y_str += num_vec[i];
        }
 
        // if numbers have trailing zeroes then skip
        if (x_str[x_str.size() - 1] == '0' && y_str[y_str.size() - 1] == '0') {
            continue;
        }
 
        // if x * y is equal to the vampire number then return the numbers as its fangs
        if (stoi(x_str) * stoi(y_str) == stoi(num_str)) {
            return make_pair(x_str, y_str);
        }
    } while (next_permutation(num_vec.begin(), num_vec.end()));
 
    return make_pair("", "");
}
 
// function to check whether the given number is vampire or not
bool isVampire(int m_int) {
 
    // converting the vampire number to string
    string n_str = to_string(m_int);
 
    // if no of digits in the number is odd then return false
    if (n_str.size() % 2 == 1) {
        return false;
    }
 
    // getting the fangs of the number
    pair<string, string> fangs = getFangs(n_str);
    if (fangs.first == "" && fangs.second == "") {
        return false;
    }
    return true;
}
 
// main driver program
int main() {
    int n = 16000;
    for (int test_num = 10; test_num < n; test_num++) {
        if (isVampire(test_num)) {
            cout << test_num << ", ";
        }
    }
    return 0;
}


Java




import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
class Main {
 
    // function to get the required fangs of the vampire number
    static Pair<String, String> getFangs(String num_str) {
 
        // to get all possible orderings of order that is equal to the number of digits of the vampire number
        List<String> num_vec = new ArrayList<>();
        for (char c : num_str.toCharArray()) {
            num_vec.add(String.valueOf(c));
        }
        Collections.sort(num_vec);
        do {
            String x_str = "", y_str = "";
            for (int i = 0; i < num_str.length() / 2; i++) {
                x_str += num_vec.get(i);
            }
            for (int i = num_str.length() / 2; i < num_str.length(); i++) {
                y_str += num_vec.get(i);
            }
 
            // if numbers have trailing zeroes then skip
            if (x_str.charAt(x_str.length() - 1) == '0' && y_str.charAt(y_str.length() - 1) == '0') {
                continue;
            }
 
            // if x * y is equal to the vampire number then return the numbers as its fangs
            if (Integer.parseInt(x_str) * Integer.parseInt(y_str) == Integer.parseInt(num_str)) {
                return new Pair<>(x_str, y_str);
            }
        } while (Collections2.nextPermutation(num_vec));
 
        return new Pair<>("", "");
    }
 
    // function to check whether the given number is vampire or not
    static boolean isVampire(int m_int) {
 
        // converting the vampire number to string
        String n_str = Integer.toString(m_int);
 
        // if no of digits in the number is odd then return false
        if (n_str.length() % 2 == 1) {
            return false;
        }
 
        // getting the fangs of the number
        Pair<String, String> fangs = getFangs(n_str);
        if (fangs.first.isEmpty() && fangs.second.isEmpty()) {
            return false;
        }
        return true;
    }
 
    // main driver program
    public static void main(String[] args) {
        int n = 16000;
        for (int test_num = 10; test_num < n; test_num++) {
            if (isVampire(test_num)) {
                System.out.print(test_num + ", ");
            }
        }
    }
 
    // helper class to return a pair of values
    static class Pair<U, V> {
        public final U first;
        public final V second;
 
        private Pair(U first, V second) {
            this.first = first;
            this.second = second;
        }
    }
 
    // helper class to get next permutation of list of strings
    static class Collections2 {
        static boolean nextPermutation(List<String> list) {
            int i = list.size() - 2;
            while (i >= 0 && list.get(i).compareTo(list.get(i + 1)) >= 0) {
                i--;
            }
            if (i < 0) {
                return false;
            }
            int j = list.size() - 1;
            while (list.get(j).compareTo(list.get(i)) <= 0) {
                j--;
            }
            Collections.swap(list, i, j);
            Collections.reverse(list.subList(i + 1, list.size()));
            return true;
        }
    }
}
 
 
// This code is contrinuted by 'shiv1o43g'


Python3




# Python code to check if a number is Vampire
# and printing Vampire numbers upto n using it
import itertools as it
 
# function to get the required fangs of the
# vampire number
 
 
def 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_tuple in num_iter:
 
        x, y = num_tuple[:int(len(num_tuple)/2)
                         ], num_tuple[int(len(num_tuple)/2):]
 
        x_str, y_str = ''.join(x), ''.join(y)
 
        # if numbers have trailing zeroes then skip
        if x_str[-1] == '0' and y_str[-1] == '0':
            continue
 
        # if x * y is equal to the vampire number
        # then return the numbers as its fangs
        if int(x_str) * int(y_str) == int(num_str):
            return x_str, y_str
 
    return None
 
# function to check whether the given number is
# vampire or not
 
 
def 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 program
n = 16000
for test_num in range(10, n):
    if isVampire(test_num):
        print("{}".format(test_num), end=", ")


C#




using System;
using System.Collections.Generic;
using System.Linq;
 
public class Program {
    // function to get the required fangs of the vampire number
    static Tuple<string, string> GetFangs(string num_str) {
 
        // to get all possible orderings of order that is equal to the number of digits of the vampire number
        var num_vec = num_str.Select(c => c.ToString()).ToList();
        num_vec.Sort();
        do {
            var x_str = "";
            var y_str = "";
            for (int i = 0; i < num_str.Length / 2; i++) {
                x_str += num_vec[i];
            }
            for (int i = num_str.Length / 2; i < num_str.Length; i++) {
                y_str += num_vec[i];
            }
 
            // if numbers have trailing zeroes then skip
            if (x_str[x_str.Length - 1] == '0' && y_str[y_str.Length - 1] == '0') {
                continue;
            }
 
            // if x * y is equal to the vampire number then return the numbers as its fangs
            if (int.Parse(x_str) * int.Parse(y_str) == int.Parse(num_str)) {
                return Tuple.Create(x_str, y_str);
            }
        } while (next_permutation(num_vec));
        return Tuple.Create("", "");
    }
 
    // function to check whether the given number is vampire or not
    static bool IsVampire(int m_int) {
 
        // converting the vampire number to string
        var n_str = m_int.ToString();
 
        // if no of digits in the number is odd then return false
        if (n_str.Length % 2 == 1) {
            return false;
        }
 
        // getting the fangs of the number
        var fangs = GetFangs(n_str);
        if (fangs.Item1 == "" && fangs.Item2 == "") {
            return false;
        }
        return true;
    }
 
    // main driver program
    static void Main() {
        int n = 16000;
        for (int test_num = 10; test_num < n; test_num++) {
            if (IsVampire(test_num)) {
                Console.Write(test_num + ", ");
            }
        }
    }
 
    // helper function to generate permutations
    static bool next_permutation(List<string> arr) {
        int i = arr.Count - 2;
        while (i >= 0 && arr[i].CompareTo(arr[i + 1]) >= 0) {
            i--;
        }
        if (i < 0) {
            return false;
        }
        int j = arr.Count - 1;
        while (arr[j].CompareTo(arr[i]) <= 0) {
            j--;
        }
        var temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        int left = i + 1;
        int right = arr.Count - 1;
        while (left < right) {
            temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }
        return true;
    }
}


Javascript




function getFangs(num_str) {
    // to get all possible orderings of order
    // that is equal to the number of digits of the vampire number
    let num_vec = [];
    for (let c of num_str) {
        num_vec.push(c);
    }
    num_vec.sort();
    let permutations = [];
    do {
        permutations.push(num_vec.join(""));
    } while (nextPermutation(num_vec));
     
    // iterate over all permutations to get the fangs
    for (let i = 0; i < permutations.length; i++) {
        let x_str = "", y_str = "";
        for (let j = 0; j < num_str.length / 2; j++) {
            x_str += permutations[i][j];
        }
        for (let j = num_str.length / 2; j < num_str.length; j++) {
            y_str += permutations[i][j];
        }
 
        // if numbers have trailing zeroes then skip
        if (x_str[x_str.length - 1] == '0' && y_str[y_str.length - 1] == '0') {
            continue;
        }
 
        // if x * y is equal to the vampire number then return the numbers as its fangs
        if (parseInt(x_str) * parseInt(y_str) == parseInt(num_str)) {
            return [x_str, y_str];
        }
    }
 
    return ["", ""];
}
 
function isVampire(m_int) {
    // converting the vampire number to string
    let n_str = m_int.toString();
 
    // if no of digits in the number is odd then return false
    if (n_str.length % 2 == 1) {
        return false;
    }
 
    // getting the fangs of the number
    let fangs = getFangs(n_str);
    if (fangs[0] == "" && fangs[1] == "") {
        return false;
    }
    return true;
}
 
function nextPermutation(arr) {
    let i = arr.length - 2;
    while (i >= 0 && arr[i] >= arr[i + 1]) {
        i--;
    }
     
    if (i < 0) {
        return false;
    }
     
    let j = arr.length - 1;
    while (arr[j] <= arr[i]) {
        j--;
    }
     
    swap(arr, i, j);
     
    reverse(arr, i + 1);
     
    return true;
}
 
function swap(arr, i, j) {
    let temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}
 
function reverse(arr, start) {
    let end = arr.length - 1;
     
    while (start < end) {
        swap(arr, start++, end--);
    }
}
 
// main driver program
let n = 16000;
let outputString = "";
for (let test_num = 10; test_num < n; test_num++) {
    if (isVampire(test_num)) {
        outputString += test_num + ", ";
    }
}
console.log(outputString.slice(0, -2));


Output:

1260, 1395, 1435, 1530, 1827, 2187, 6880, 

Refer to numberphile for more details: References:

  1. Rossetta Code
  2. Wikipedia – Vampire Number
  3. Stackoverflow
  4. Python Docs on itertools


Last Updated : 31 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads