In this post, we will explore the concept of Vampire Numbers in C++, highlighting their characteristics and providing an illustrative example.
What is the Vampire Number in C++?
A vampire number is a positive integer that possesses an even number of digits and is split into two integers known as a pair of fangs. Each fang has half the number of digits as the original integer, and when combined, they contain all the digits of the original number rearranged in all possible ways. It is essential that neither of the fangs ends with a zero. For example, 1260 qualifies as a vampire number since it can be expressed as 21 × 60, incorporating all the digits 1, 2, 6, and 0 in both the product and the factors.
Clifford A. Pickover initially unearthed vampire numbers in his publication released in 1994. These unique numbers have since garnered attention from mathematicians and fans alike due to their distinctive characteristics, particularly the challenge of identifying them.
Properties of Vampire Numbers:
Several properties of Vampire Numbers in C++ are as follows:
- Even Number of Digits: Vampire numbers are those numbers where if we subtract the product of two single-digit numbers from it, the result is zero. Vampire numbers will always be an even number of digits. It is done to make sure that each fang can be of similar digit to the other.
- Fangs: The two factors (fangs) must essentially be half the number of digits of the original one. For example, a 4-digit vampire number is composed of two 2-digit fangs.
- Digit Permutation: However, the digits of the obtained vampire number are a permutation of all digits whether single or combined with other digits of the fangs number. It implies that if the digits in the fangs are regrouped, then they can reformat the first number.
- No Trailing Zeroes: We cannot have trailing zeroes in both fangs at the same time. This restriction eliminates cases where two factors are similar to 126000 = 210 × 600, where both the factors end in zero.
Identifying Vampire Numbers in C++:
In order to determine whether a given number is a vampire number in C++, follow these steps:
- Check for an Even Number of Digits: Make sure the number consists of an even number of digits. It can always be done by casting the number to a string and then checking the string'
- Generate Potential Fangs: Divide the given number into all possible suitable factors, which should have equal or equal one digit each in a pair. For a given 4-digit number lying between 1000 and 9999, we take all possible combinations of two-digit numbers.
- Check for Trailing Zeroes: Do not use those pairs that both factors consist of the last digit as zero.
- Verify Digit Permutation: Make sure that the number of factors in the given numeral as and when they are factored can be sorted in the same order as the sorted numerals of the given number.
Example:
Let's consider an example to demonstrate the concept of a Vampire Number in C++.
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
// Function to check whether the number is a vampire or not
bool isVampireNum(int number) {
std::string numStrs = std::to_string(number);
int slength = numStrs.length();
// The condition to check if the number has even length
if (slength % 2 != 0) return false;
int half_len = slength / 2;
// Pairs generation
int fst = std::pow(10, half_len - 1);
int lst = std::pow(10, half_len);
for (int i = fst; i < lst; ++i) {
for (int j = i; j < lst; ++j) {
// trailing zeros condition
if (i % 10 == 0 && j % 10 == 0) continue;
// checking if the product matches the original string
if (i * j == number) {
// digits cobining
std::string fangStrs = std::to_string(i) + std::to_string(j);
std::sort(fangStrs.begin(), fangStrs.end());
std::sort(numStrs.begin(), numStrs.end());
// Check if sorted digits match
if (fangStrs == numStrs) {
std::cout << number << " = " << i << " * " << j << std::endl;
return true;
}
}
}
}
return false;
}
int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
if (isVampireNum(num)) {
std::cout<<"The given num " << num << " is a vampire number." << std::endl;
} else {
std::cout<<"The given num " << num << " is not a vampire number." << std::endl;
}
return 0;
}
Output:
Enter a number: 1260
1260 = 21 * 60
The given num 1260 is a vampire number.
Explanation:
- function Definition: The isVampireNum function analyses an input integer number and returns true if it is a vampire number. Otherwise returns False.
- Length Check: The variables procnum and plen are checked. The Length of the number is even, and if yes, the series is formed. If it is odd, the function proceeds to return false because to be a vampire number, the precision must consist of an even number of digits.
- Fang Generation: The function determines the variation of possible fang values. It implies that each of the fang must contain a number with half the number of digits as the vampire number.
- Nested Loops for Pair Generation: It uses a pair of fangs (i, j) or two nested loops in order to generate desired pairs from between the start and the end. The first loop goes through all possible values of i the second one goes through all possible values of j. If both fangs are zeros, both are omitted so as not to form an invalid pair to confuse those involved.
- Product Check: For every i and j, the function checks whether the two together can make the result of the multiplication equal to some number. It concatenates the digits in both fangs together into a single string and compares or rather checks whether the digits of both its fangs as well as the other digits, including the numbers of fangs, were sorted out in the same manner.
Conclusion:
In summary, the numerical aspects associated with vampire numbers present a captivating subject within number theory. These numbers possess unique characteristics that captivate researchers, who utilize computational techniques to identify them. The C++ code provided effectively checks if a given number qualifies as a vampire number by ensuring specific criteria are met: the number of digits must be divisible by 2, the fangs are correctly generated without any decimal points, and there is a match between the permuted digits and the fangs. This methodology not only reinforces the understanding of conditionals, loops, and string manipulations - fundamental concepts in programming languages - but also finds application in the realm of pure mathematics concerning vampire numbers.