##Bug##Bugs
The first obvious bug is that your code does not run multiple times. You need to reset the j variable in the outside loop, because the system will otherwise never start again from the beginning.
- The first obvious bug is that your code does not run multiple times. You need to reset the
j variable in the outside loop, because the system will otherwise never start again from the beginning.
- Your code enters an infinite loop if the user inputs
1. I have not narrowed down why, but the code is supposed to output the result There are no solutions for 1.
#include <iostream>
bool digitsUnique(long value) {
int count[10] = {};
// Move 0 digits to the end
while (value < 10000) {
value *= 10;
}
while (value > 0) {
if (count[value % 10]++ > 0) {
return false;
}
value /= 10;
}
return true;
}
bool combinedUnique(long low, long high) {
int count[10] = {};
// Move 0 digits to the end
while (low < 10000) {
low *= 10;
}
while (high < 10000) {
high *= 10;
}
while (low > 0) {
if (count[low % 10]++ > 0) {
return false;
}
low /= 10;
}
while (high > 0) {
if (count[high % 10]++ > 0) {
return false;
}
high /= 10;
}
return true;
}
void compute(int mult) {
int count = 0;
// 10 will require releating digits,
// and more than 10 requires multiple 0 digits
if (mult > 1 && mult < 10) {
long limit = 98765 / mult;
for (long low = 1234; low <= limit; low++) {
if (digitsUnique(low)) {
long high = low * mult;
if (combinedUnique(low, high)) {
count++;
std::cout << lowhigh << "/" << highlow << " = " << mult << "\n";
}
}
}
}
if (count == 0) {
std::cout << "There are no solutions for " << mult << ".\n";
}
}
int main() {
int n;
// std::cout << "Enter Multiple: ";
while (std::cin >> n) {
compute(n);
// std::cout << "Enter Multiple: ";
}
return 0;
}
Note, when I run your solution on my computer, and use the input values 2 through 11 like:
time ./nfact < inputs
I get the time result of:
.....
97524/10836
There are no solutions for 10.
There are no solutions for 11.
real 0m0.815s
user 0m0.808s
sys 0m0.008s
When I run my version above, I get:
95823/10647 = 9
97524/10836 = 9
There are no solutions for 10.
There are no solutions for 11.
real 0m0.019s
user 0m0.016s
sys 0m0.000s
Note that it is about 42 times faster.... I like 42.