How to resolve the algorithm Vampire number step by step in the C++ programming language
How to resolve the algorithm Vampire number step by step in the C++ programming language
Table of Contents
Problem Statement
A vampire number is a natural decimal number with an even number of digits, that can be factored into two integers. These two factors are called the fangs, and must have the following properties:
An example of a vampire number and its fangs: 1260 : (21, 60)
16758243290880, 24959017348650, 14593825548650
Note that a vampire number can have more than one pair of fangs.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Vampire number step by step in the C++ programming language
This code implements a program that checks if a given number is a vampire number, and if so, it prints the "fangs" of the number.
A vampire number is a number that can be factored into two other numbers, called fangs. The number of digits in each fang must be half the number of digits in the original number. For example, 1260 is a vampire number because it can be factored into 21 and 60, each of which has half the number of digits as 1260.
The code first takes in a number as input and then calls the isVampireNumber function to check if the number is a vampire number. The isVampireNumber function takes two arguments: the number to check and a reference to a vector that will store the fangs of the number if it is a vampire number.
The isVampireNumber function first converts the number to a string and then sorts the digits of the string. It then finds the length of the string and divides it by 2 to get the length of the fangs. The function then loops through all the numbers from 10^(fanglength-1) to the square root of the number. For each number, the function checks if the number divides evenly into the number being checked. If it does, the function checks if the quotient of the division is also a vampire number. If it is, the function adds the fangs of the quotient to the vector of fangs and returns true.
If the isVampireNumber function returns true, the code prints the number and its fangs. If the function returns false, the code prints a message saying that the number is not a vampire number.
The code then loops through a list of test numbers and checks if each number is a vampire number. If a number is a vampire number, the code prints the number and its fangs. If a number is not a vampire number, the code prints a message saying that the number is not a vampire number.
Source code in the cpp programming language
#include <vector>
#include <utility>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
bool isVampireNumber( long number, std::vector<std::pair<long, long> > & solution ) {
std::ostringstream numberstream ;
numberstream << number ;
std::string numberstring( numberstream.str( ) ) ;
std::sort ( numberstring.begin( ) , numberstring.end( ) ) ;
int fanglength = numberstring.length( ) / 2 ;
long start = static_cast<long>( std::pow( 10 , fanglength - 1 ) ) ;
long end = sqrt(number) ;
for ( long i = start ; i <= end ; i++ ) {
if ( number % i == 0 ) {
long quotient = number / i ;
if ( ( i % 10 == 0 ) && ( quotient % 10 == 0 ) )
continue ;
numberstream.str( "" ) ; //clear the number stream
numberstream << i << quotient ;
std::string divisorstring ( numberstream.str( ) ) ;
std::sort ( divisorstring.begin( ) , divisorstring.end( ) ) ;
if ( divisorstring == numberstring ) {
std::pair<long , long> divisors = std::make_pair( i, quotient ) ;
solution.push_back( divisors ) ;
}
}
}
return !solution.empty( ) ;
}
void printOut( const std::pair<long, long> & solution ) {
std::cout << "[ " << solution.first << " , " << solution.second << " ]" ;
}
int main( ) {
int vampireNumbersFound = 0 ;
std::vector<std::pair<long , long> > solutions ;
double i = 1.0 ;
while ( vampireNumbersFound < 25 ) {
long start = static_cast<long>( std::pow( 10 , i ) ) ;
long end = start * 10 ;
for ( long num = start ; num < end ; num++ ) {
if ( isVampireNumber( num , solutions ) ) {
vampireNumbersFound++ ;
std::cout << vampireNumbersFound << " :" << num << " is a vampire number! These are the fangs:\n" ;
std::for_each( solutions.begin( ) , solutions.end( ) , printOut ) ;
std::cout << "\n_______________" << std::endl ;
solutions.clear( ) ;
if ( vampireNumbersFound == 25 )
break ;
}
}
i += 2.0 ;
}
std::vector<long> testnumbers ;
testnumbers.push_back( 16758243290880 ) ;
testnumbers.push_back( 24959017348650 ) ;
testnumbers.push_back( 14593825548650 ) ;
for ( std::vector<long>::const_iterator svl = testnumbers.begin( ) ;
svl != testnumbers.end( ) ; svl++ ) {
if ( isVampireNumber( *svl , solutions ) ) {
std::cout << *svl << " is a vampire number! The fangs:\n" ;
std::for_each( solutions.begin( ) , solutions.end( ) , printOut ) ;
std::cout << std::endl ;
solutions.clear( ) ;
} else {
std::cout << *svl << " is not a vampire number!" << std::endl ;
}
}
return 0 ;
}
You may also check:How to resolve the algorithm Binary strings step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Octave programming language
You may also check:How to resolve the algorithm Call a function in a shared library step by step in the Perl programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the Mercury programming language
You may also check:How to resolve the algorithm Ulam spiral (for primes) step by step in the Kotlin programming language