Skip to main content
Bounty Awarded with 50 reputation awarded by MikhailTal
Infinite loop, I now know why
Source Link
rolfl
  • 98.2k
  • 17
  • 220
  • 419
  1. 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.
  2. Your code enters an infinite loop if the user inputs 1. I have not narrowed down why, but theThe code is supposed to output the result There are no solutions for 1.. Instead, since all 5-digit numbers are less than 100000 and since helper will never be more than 5 digits, the helper * 1 < 100000 exit condition for the while loop will never fail.
  1. 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.
  2. 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.
  1. 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.
  2. Your code enters an infinite loop if the user inputs 1. The code is supposed to output the result There are no solutions for 1.. Instead, since all 5-digit numbers are less than 100000 and since helper will never be more than 5 digits, the helper * 1 < 100000 exit condition for the while loop will never fail.
code block placeholder was left behind, somehow
Source Link
rolfl
  • 98.2k
  • 17
  • 220
  • 419
  • Namespaces:

    using namespace std;
    

    Don't do that. It is too easy to have issues with pollution in your namespace. enter code here

  • Variable declarations:

    Declare variables in the scope they are used. This would have solved the bug, for the record. The j variable is declared outside it's use-scope. This is how you declare j:

    int j = 0;
    int a1,a2,a3,a4,a5;
    while (cin >> n)
    {
        while(j < 10)
        {
            i[j] = j;
            j++;
        }
    

    But this is how it should be declared:

      int a1,a2,a3,a4,a5;
      while (cin >> n)
      {
          int j = 0;
          while(j < 10)
          {
              i[j] = j;
              j++;
          }
    
  • Bracing C++ styles generally recommend the brace at line end: e.g. Google C++ style guide Local standards override this, though, so if your local dev team has a standard they follow, that's the one that should be followed.

  • Namespaces:

    using namespace std;
    

    Don't do that. It is too easy to have issues with pollution in your namespace. enter code here

  • Variable declarations:

    Declare variables in the scope they are used. This would have solved the bug, for the record. The j variable is declared outside it's use-scope. This is how you declare j:

    int j = 0;
    int a1,a2,a3,a4,a5;
    while (cin >> n)
    {
        while(j < 10)
        {
            i[j] = j;
            j++;
        }
    

    But this is how it should be declared:

      int a1,a2,a3,a4,a5;
      while (cin >> n)
      {
          int j = 0;
          while(j < 10)
          {
              i[j] = j;
              j++;
          }
    
  • Bracing C++ styles generally recommend the brace at line end: e.g. Google C++ style guide Local standards override this, though, so if your local dev team has a standard they follow, that's the one that should be followed.

  • Namespaces:

    using namespace std;
    

    Don't do that. It is too easy to have issues with pollution in your namespace.

  • Variable declarations:

    Declare variables in the scope they are used. This would have solved the bug, for the record. The j variable is declared outside it's use-scope. This is how you declare j:

    int j = 0;
    int a1,a2,a3,a4,a5;
    while (cin >> n)
    {
        while(j < 10)
        {
            i[j] = j;
            j++;
        }
    

    But this is how it should be declared:

      int a1,a2,a3,a4,a5;
      while (cin >> n)
      {
          int j = 0;
          while(j < 10)
          {
              i[j] = j;
              j++;
          }
    
  • Bracing C++ styles generally recommend the brace at line end: e.g. Google C++ style guide Local standards override this, though, so if your local dev team has a standard they follow, that's the one that should be followed.

Add code
Source Link
rolfl
  • 98.2k
  • 17
  • 220
  • 419

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

  1. 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.
  2. 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.

##Bug

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.

#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) {
  long limit = 98765 / mult;
  for (long low = 1234; low <= limit; low++) {
    if (digitsUnique(low)) {
      long high = low * mult;
      if (combinedUnique(low, high)) {
        std::cout << low << "/" << high << "\n";
      }
    }
  }
}

int main() {
  int n;
  std::cout << "Enter Multiple: ";
  while (std::cin >> n) {
    compute(n);
    std::cout << "Enter Multiple: ";
  }
  return 0;
}

##Bugs

  1. 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.
  2. 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 << high << "/" << low << " = " << 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.

Add code
Source Link
rolfl
  • 98.2k
  • 17
  • 220
  • 419
Loading
Source Link
rolfl
  • 98.2k
  • 17
  • 220
  • 419
Loading