# Thread: Getting double the Vampire numbers

1. ## 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. 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).

3. Is it possible to avoid the repetitions in the loop itself?

4. 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```