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:
- 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
C++14
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
pair<string, string> getFangs(string num_str) {
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 (x_str[x_str.size() - 1] == '0' && y_str[y_str.size() - 1] == '0' ) {
continue ;
}
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( "" , "" );
}
bool isVampire( int m_int) {
string n_str = to_string(m_int);
if (n_str.size() % 2 == 1) {
return false ;
}
pair<string, string> fangs = getFangs(n_str);
if (fangs.first == "" && fangs.second == "" ) {
return false ;
}
return true ;
}
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 {
static Pair<String, String> getFangs(String num_str) {
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 (x_str.charAt(x_str.length() - 1 ) == '0' && y_str.charAt(y_str.length() - 1 ) == '0' ) {
continue ;
}
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<>( "" , "" );
}
static boolean isVampire( int m_int) {
String n_str = Integer.toString(m_int);
if (n_str.length() % 2 == 1 ) {
return false ;
}
Pair<String, String> fangs = getFangs(n_str);
if (fangs.first.isEmpty() && fangs.second.isEmpty()) {
return false ;
}
return true ;
}
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 + ", " );
}
}
}
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;
}
}
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 ;
}
}
}
|
Python3
import itertools as it
def getFangs(num_str):
num_iter = it.permutations(num_str, len (num_str))
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 x_str[ - 1 ] = = '0' and y_str[ - 1 ] = = '0' :
continue
if int (x_str) * int (y_str) = = int (num_str):
return x_str, y_str
return None
def isVampire(m_int):
n_str = str (m_int)
if len (n_str) % 2 = = 1 :
return False
fangs = getFangs(n_str)
if not fangs:
return False
return True
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 {
static Tuple< string , string > GetFangs( string num_str) {
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 (x_str[x_str.Length - 1] == '0' && y_str[y_str.Length - 1] == '0' ) {
continue ;
}
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( "" , "" );
}
static bool IsVampire( int m_int) {
var n_str = m_int.ToString();
if (n_str.Length % 2 == 1) {
return false ;
}
var fangs = GetFangs(n_str);
if (fangs.Item1 == "" && fangs.Item2 == "" ) {
return false ;
}
return true ;
}
static void Main() {
int n = 16000;
for ( int test_num = 10; test_num < n; test_num++) {
if (IsVampire(test_num)) {
Console.Write(test_num + ", " );
}
}
}
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) {
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));
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 (x_str[x_str.length - 1] == '0' && y_str[y_str.length - 1] == '0' ) {
continue ;
}
if (parseInt(x_str) * parseInt(y_str) == parseInt(num_str)) {
return [x_str, y_str];
}
}
return [ "" , "" ];
}
function isVampire(m_int) {
let n_str = m_int.toString();
if (n_str.length % 2 == 1) {
return false ;
}
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--);
}
}
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:
- Rossetta Code
- Wikipedia – Vampire Number
- Stackoverflow
- Python Docs on itertools
Last Updated :
31 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...