Unknown string of numbers..

Started by
14 comments, last by bratiefanut 10 years, 1 month ago

Your method would produce 877, but that wasn't in the list.
All numbers should again not have two times the same number except for the nine.

If you quote from unrelated posts, you'll confuse everybody. In the post where I object to your method, I complain about 799. I was complaining about 877 for Nypyren's method.

Advertisement

Alvaro, I still can't really code a program to generate this numbers in c++. Why your method produce 9988?

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

Alvaro, I still can't really code a program to generate this numbers in c++.


Here it is in C++:

#include <iostream>

bool check_histogram_of_digits_is_weakly_increasing(unsigned long x) {
  int histogram[10] = {0};
  
  do {
    histogram[x % 10]++;
    x /= 10;
  } while (x > 0);
  
  for (int i=0; i<9; ++i) {
    if (histogram[i] > histogram[i+1])
      return false;
  }
  
  return true;
}

int main() {
  for (int i=1; i<10000; ++i) {
    if (check_histogram_of_digits_is_weakly_increasing(i))
      std::cout << i << '\n';
  }
}

Why your method produce 9988?


Because its histogram of digits is (0,0,0,0,0,0,0,0,2,2), which is weakly increasing.

Thank you very much Alvaro. I don't really understand what " histogram of digits is weakly increasing" means but I found an article about histograms on wikipedia. I will read it and hopefully understand it. Thank you.

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

Thank you very much Alvaro. I don't really understand what " histogram of digits is weakly increasing" means but I found an article about histograms on wikipedia. I will read it and hopefully understand it. Thank you.

"Histogram" is just a fancy name for "one counter for each possible value". I think my C++ code should be fairly easy to read, but I'll give you a couple of examples in English.

Let's see why the number 799 is not in the list. Let's count how many times each digit appear in 799: "0" appears 0 times, "1" appears 0 times, ..., "7" appears 1 time, "8" appears 0 times and "9" appears 2 times. Putting all ten counters in a row, it's (0,0,0,0,0,0,0,1,0,2), and this is what I am calling the "histogram". As you see there are more "7"s than "8"s, so this sequence is not weakly increasing.

Let's now see why the number 989 is in the list. The histogram is (0,0,0,0,0,0,0,0,1,2). This is weakly increasing (i.e., every number is either the same as the one before it or larger).

Thank you very much Alvaro. I don't really understand what " histogram of digits is weakly increasing" means but I found an article about histograms on wikipedia. I will read it and hopefully understand it. Thank you.

"Histogram" is just a fancy name for "one counter for each possible value". I think my C++ code should be fairly easy to read, but I'll give you a couple of examples in English.

Let's see why the number 799 is not in the list. Let's count how many times each digit appear in 799: "0" appears 0 times, "1" appears 0 times, ..., "7" appears 1 time, "8" appears 0 times and "9" appears 2 times. Putting all ten counters in a row, it's (0,0,0,0,0,0,0,1,0,2), and this is what I am calling the "histogram". As you see there are more "7"s than "8"s, so this sequence is not weakly increasing.

Let's now see why the number 989 is in the list. The histogram is (0,0,0,0,0,0,0,0,1,2). This is weakly increasing (i.e., every number is either the same as the one before it or larger).

Oohh.. I understand now :) And understand why on wikipedia on histogram article was a lot of graphics and statistics laugh.png Thank you very much.

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

This topic is closed to new replies.

Advertisement