Vampire number: Difference between revisions
→{{header|C++}}: Fixed the problems that had been flagged up for this example.
(added Factor) |
(→{{header|C++}}: Fixed the problems that had been flagged up for this example.) |
||
Line 482:
=={{header|C++}}==
<lang cpp>#include <vector>
#include <utility>
Line 492 ⟶ 489:
#include <string>
#include <cmath>
bool isVampireNumber( long number, std::vector<std::pair<long, long> > & solution ) {
std::ostringstream numberstream ;
Line 500 ⟶ 497:
int fanglength = numberstring.length( ) / 2 ;
long start = static_cast<long>( std::pow( 10 , fanglength - 1 ) ) ;
long end =
for ( long i = start ; i <
if ( number % i == 0 ) {
long quotient = number / i ;
if ( ( i % 10 == 0 ) && ( quotient % 10 == 0 ) )
numberstream.str( "" ) ; //clear the number stream
numberstream << i << quotient ;
Line 518 ⟶ 515:
return !solution.empty( ) ;
}
void printOut( const std::pair<long, long> & solution ) {
std::cout << "[ " << solution.first << " , " << solution.second << " ]" ;
}
int main( ) {
int vampireNumbersFound = 0 ;
Line 532 ⟶ 529:
for ( long num = start ; num < end ; num++ ) {
if ( isVampireNumber( num , solutions ) ) {
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( ) ;
▲ vampireNumbersFound++ ;
if ( vampireNumbersFound == 25 )
break ;
Line 545 ⟶ 542:
std::vector<long> testnumbers ;
testnumbers.push_back( 16758243290880 ) ;
testnumbers.push_back(
testnumbers.push_back( 14593825548650 ) ;
for ( std::vector<long>::const_iterator svl = testnumbers.begin( ) ;
Line 561 ⟶ 558:
}</lang>
{{out}}
<pre>
[ 21 , 60 ]
_______________
[ 15 , 93 ]
_______________
[ 35 , 41
_______________
[ 30 , 51 ]
_______________
[ 21 , 87 ]
_______________
[ 27 , 81 ]
_______________
_______________▼
[ 201 , 510 ]
_______________
[ 260 , 401
_______________
[ 210 , 501 ]
_______________
[ 204 , 516 ]
_______________
[ 150 , 705 ]
_______________
[ 135 , 801 ]
_______________
[ 158 , 701 ]
_______________
[ 152 , 761 ]
_______________
[ 161 , 725 ]
_______________
[ 167 , 701 ]
_______________
[ 141 , 840 ]
_______________
_______________▼
[ 231 , 534 ]
_______________
[ 281 , 443
_______________
[ 152 , 824 ]
_______________
[ 231 , 543 ]
_______________
[ 204 , 615 ][ 246 , 510 ]
_______________
[ 251 , 500 ]
▲_______________
▲23 :126027 is a vampire number! These are the fangs:
▲[ 201 , 627 ]
▲_______________
▲24 :126846 is a vampire number! These are the fangs:
▲[ 261 , 486 ]
_______________
16758243290880 is a vampire number! The fangs:
[ 1982736 , 8452080 ][ 2123856 , 7890480 ][ 2751840 , 6089832 ][ 2817360 , 5948208 ]
[
14593825548650 is not a vampire number!
</pre>
|