Thread: Getting double the Vampire numbers

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    41

    Getting double the Vampire numbers

    My program finds all the 4-digit vampire numbers.
    There should be a total of only 7 numbers, but i'am getting 14.
    How to remove the duplicates?
    Code:
    #include"stdafx.h"
    #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    
    bool bothTrailingZeros(const int& x, const int& y)
    {
    	return ((x % 10) == 0) && ((y % 10) == 0);
    }
    
    
    vector<int> digits(int n)
    {
    	vector<int> ret;
    	while (n > 0)
    	{
    		ret.push_back(n % 10);
    		n /= 10;
    	}
    	return ret;
    }
    int main()
    {
    	int v;
    	vector<int> digits_v, digits_i, digits_j;
    	//fangs for 4-digit vampire numbers must have 4/2 == 2 digits 
    	for (int i = 10; i < 100; ++i)
    	{
    		for (int j = 10; j < 100; ++j)
    		{
    			if ((i*j)>9999)
    				break;
    			if (bothTrailingZeros(i, j))
    				continue;
    			v = i*j;
    			digits_v = digits(v);
    			digits_i = digits(i);
    			digits_j = digits(j);
    			for (const int& x : digits_j)		//digits of j are put together with digits of i
    				digits_i.push_back(x);
    			sort(digits_v.begin(), digits_v.end());
    			sort(digits_i.begin(), digits_i.end());
    			if (digits_i == digits_v)
    			{
    				cout << v << " = " << i << " x " << j << endl;
    			}
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    1. Define a set in which you can store unique result values.
    Code:
    #include <set>
    
    set<int> result; // at the beginning of function main
    
    if (digits_i == digits_v)
    {
        if (result.count(v) == 0)
        {
            result.insert(v);
            cout << v << " = " << i << " x " << j << endl;
        }
    }
    2. This condition
    Code:
    if ((i*j)>9999)
        break;
    is redundant as it is always false (99 * 99 == 9801).
    Last edited by DRK; 06-22-2015 at 07:43 AM.

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    Is it possible to avoid the repetitions in the loop itself?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by V8cTor
    Is it possible to avoid the repetitions in the loop itself?
    Looking at the output:
    Code:
    1395 = 15 x 93
    1260 = 21 x 60
    1827 = 21 x 87
    2187 = 27 x 81
    1530 = 30 x 51
    1435 = 35 x 41
    1435 = 41 x 35
    1530 = 51 x 30
    1260 = 60 x 21
    6880 = 80 x 86
    2187 = 81 x 27
    6880 = 86 x 80
    1827 = 87 x 21
    1395 = 93 x 15
    I observe that the repetition appears to be because the multiplications are repeated with the operands swapped. This suggests that you might be able to eliminate the repetition by starting the inner loop from the outer loop's current loop variable rather than always starting from 10. Indeed, when I changed your code to have:
    Code:
        for (int i = 10; i < 100; ++i)
        {
            for (int j = i; j < 100; ++j)
            {
    I ended up with the output:
    Code:
    1395 = 15 x 93
    1260 = 21 x 60
    1827 = 21 x 87
    2187 = 27 x 81
    1530 = 30 x 51
    1435 = 35 x 41
    6880 = 80 x 86
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Float and Double Numbers
    By luise.valencia in forum C Programming
    Replies: 7
    Last Post: 04-13-2005, 12:33 PM
  2. double numbers
    By Gil22 in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2003, 10:12 PM
  3. add 2 largest possible double numbers
    By conjurer in forum C Programming
    Replies: 3
    Last Post: 04-05-2002, 11:26 AM