Complementary Counting

Logic Level 5

From top to bottom, concatenate the nine numbers that is in the blank spaces as your answer.


The answer is 251825242324262526.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

9 solutions

I made a C program to solve the problem. The answer is

25 18 25 24 23 24 26 25 26

Here's the program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>

#define DEBUG 0

// Return number of digits other than digit b in number a
int checkDigits(int a,int b)
{
 int ans = 0;

 do {
  if (a % 10 != b)
   ans++;
 } while ( (a = a/10) != 0);

 return ans;
}

int main()
{
 int i, j, k, n, ok;
 int a[9]; // Sequence from 1 to 9
 int b[9]; // Answer
 int c[9]; // Backup of b
 int d[9]; // Buffer

 // Initialize vectors 
 for (i=1;i<=9;i++)
 {
  a[i-1] = i;
  b[i-1] = 0;
  c[i-1] = 0;
  d[i-1] = 0;
 }

 while (1)
 {
  // Loop through all 9 digits
  for (j=1;j<=9;j++)
  {
   // Count instances of digits other than j 
   k = 0; 
   for (i=1;i<=9;i++)
   {
    if (a[i-1] != j) k++; 
    if (b[i-1] != 0 && (n = checkDigits(b[i-1],j)) )
     k += n;
   }
   // Save result in buffer
   d[j-1] = k;
  }

  // Transfer buffer to b for next iteration
  for (i=0;i<9;i++)
   b[i] = d[i];

  ok = 0; // Set to 1 if answer is the same as after last iteration
  for (i=0;i<9;i++)
  {
   if (b[i] != c[i])
    break;
   if (i==8) ok = 1;
  }

  #if DEBUG == 1
  for (i=0;i<9;i++)
   printf("%i %i %i\n",a[i],b[i],c[i]);
  printf("\n");
  #endif

  if (ok) // Break if answer hasn't changed 
   break;
  else // Save answer if it has changed 
   for (i=0;i<9;i++)
    c[i] = b[i];
 }

 // Print results
 for (i=0;i<9;i++)
  printf("%i %i\n",a[i],b[i]);

 return 0;
} 

Remark the vector a[k] is not really needed.

Abdelhamid Saadi - 4 years, 8 months ago
Bufang Liang
Aug 15, 2015

For starters, let's first recognize that the number of digits is going to be at least 18, so if each of these answers is going to be double digits, the total number of digits will come out to be 9*3 = 27.

Next, we find that excluding 2, almost every digit is not going to appear very often, so all of the answers with the exception of 2 are going to be close to and at most 26. This means that the number of digits that are not 2 is going to be close to and at most 18.

This next bit helps optimize the counting a little bit. Since the total number of digits is 27, rather than counting up the number of digits that are not a particular one, we count up the number of times a digit appears and subtract that from 27. So if the digit 8 appears twice, then the count of non-8 digits is 27-2 = 25.

The digits 7 and 9 are only going to ever appear once, making the count 26. The digit 8 will probably only appear twice because of the 18 for answer 2. Similarly, the digit 1 will probably only appear twice. Both of these counts is 25.

This leaves us with the following likely scenario:

1: 25

2: 18

3: 2-

4: 2-

5: 2-

6: 2-

7: 26

8: 25

9: 26

The last 4 answers are very erratic, as changing one of them usually changes many of the others. However, we can now prove that the numbers we have fixed will not be changed by just playing with the missing digits.

At most, digits 3 and 4 can have 5 occurrences, but if one of them did have 5, the count comes to 27-5 = 22, which does not contribute the digit needed. Therefore, they can only have 4 occurrences maximum, which brings the count of a digit to 23 at the lowest, which does not change any of the other quantities we have. The only digits that can extend past 4 occurrences is 5 or 6, since they already appear three times each, meaning they start at 24, so it is not possible for them to appear more than 4 times either. (There are one or two ambiguous cases, but working through them manually shows they don't work.)

However, just proving that these missing digits will not affect our fixed digits does not mean the answer exists. Between the possibilities of 3, 4, 5, and 6, there are 4^4 = 256 possible cases to check, in which the large majority of them can be immediately or quickly discarded. This now becomes possible to guess and check by hand, which allows us to find our final answer.

Thanks! I'm sure you enjoyed this question!

Pi Han Goh - 5 years, 9 months ago

Log in to reply

what's wrong with 9 9 9 8 9 9 8 7 4.....one digit at each blank.

Chandrakant Pandey - 5 years, 9 months ago

Log in to reply

Check your working again.

Pi Han Goh - 5 years, 9 months ago

You need to count the ones already in the box as well

Vishnu Bhagyanath - 5 years, 9 months ago

What are we supposed to count, its very vague for me Can you give us some hints Thanks!!! \begin{array}{|cc|} \hline \text{What are we supposed to count, its very vague for me}\\ \text{Can you give us some hints}\\ \text { {Thanks!!!} }\\ \hline \end{array}

Syed Baqir - 5 years, 9 months ago

Log in to reply

Write down all the two digit numbers in the blanks respectively and read the question again.

Pi Han Goh - 5 years, 9 months ago

Log in to reply

@Pi Han Goh Count two digit numbers from where because in the question it just stated "Write Down two digit numbers that are not 1 __ ??? Thanks!!! \begin{array}{|cc|} \hline \text{Count two digit numbers from where because in the question it just stated}\\ \text{"Write Down two digit numbers that are not 1 \_\_ ???}\\ \text { {Thanks!!!} }\\ \hline \end{array}

Syed Baqir - 5 years, 9 months ago

Log in to reply

@Syed Baqir Have you tried writing them down first?

Pi Han Goh - 5 years, 9 months ago

Log in to reply

@Pi Han Goh I dont know from where I need to start , can you help me please ?

Syed Baqir - 5 years, 9 months ago

Log in to reply

@Syed Baqir Write 25 on the first line, 18 on the second line, 25 on the third line, and so on.

Pi Han Goh - 5 years, 9 months ago

Log in to reply

@Pi Han Goh Yes I know, But I could not understand why 25 , 18 because I cant find them in the box :P

Syed Baqir - 5 years, 9 months ago

Log in to reply

@Syed Baqir Read the question again. How many digits that are not 1? How many digits that are not 2?

Pi Han Goh - 5 years, 9 months ago

Log in to reply

@Pi Han Goh uhhh, I was searching inside the box to find the answer ,,,

Thats why I restricted myself until 9 and never thought it goes to 25 :P , :P

Syed Baqir - 5 years, 9 months ago

Dang, forgetting the 123456789 sure blows.

Xiaoying Qin - 5 years, 5 months ago

if you don't mind explaining, how do you get "at least 18" or the answers are going to be double digits?

Ash Norsidi - 3 years, 2 months ago

Another bit of help comes from the sum of all the counts. Each count is: 27 - (occurences of a digit). Sum all: 27×9 - 27 (sum of the occurences of all digits) = 216. Which leads to an average count of 24. You can use this information to reach convergence easier.

daniele gazzola - 2 years, 3 months ago

why not 9,9,9,8,9,9,8,7,4

gauransh lavania - 1 year, 12 months ago

I thought this would be much harder than the easier counterpart, but to my surprise only 11 trials is required to solve this puzzle. Nevertheless, I've written a program to solve this problem: link .

For those who can't read code or can't be bothered to click the link, here are some explanations. Some answers have already proved that all the answers must be 2 digits long and there are maximum 27 digits in the box, so my program simply starts by placing 27 on all the underlines and start counting the number of digits in the box, then simply place the new answers as 27 minus the number of digits and repeat the process.

Here are the all the trials needed (so doing it by hand is probably not that hard either):

  • 261726262626172626
  • 241926262619242626
  • 241926242621262624
  • 241826232622262625
  • 251725252522262526
  • 251726262124252626
  • 241826252422252626
  • 251726242423262526
  • 251825242423252626
  • 251825242324262526
  • 251825242324262526

Thank you for your solution.

Pi Han Goh - 5 years, 6 months ago

Remark the program should converge from whatever starting position.

Abdelhamid Saadi - 4 years, 8 months ago

Alternative solution: eighteighteighteighteighteighteighteighteight.

this is the only solution that makes sense to me.

Ash Norsidi - 3 years, 2 months ago
Abdelhamid Saadi
Oct 13, 2016
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
"Complementary Counting"
def solve():
    ll = [8] *9
    ss = "".join(map(str, ll))
    tt = ""
    while tt != ss:
        ll = [8 + len(ss.replace(str(k + 1),'')) for k in range(9)]
        tt, ss = ss, "".join(map(str, ll))
    return ss

print(solve())

Output :

1
'251825242324262526'

Thanks for posting a programming solution.

Pi Han Goh - 4 years, 8 months ago
Titouan Teyssier
May 13, 2019

Program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
prev = None
step = []
while prev != step:
    print(step) # shows the different steps
    prev = step
    step = [8] * 9
    for cnt in prev:
        for d in str(cnt):
            step = [i + 1 for i in step]
            step[int(d) - 1] -= 1
print(''.join(map(str, step)))

Output

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[]
[8, 8, 8, 8, 8, 8, 8, 8, 8]
[17, 17, 17, 17, 17, 17, 17, 8, 17]
[17, 25, 25, 25, 25, 25, 17, 24, 25]
[24, 19, 26, 25, 20, 26, 24, 26, 26]
[25, 18, 26, 24, 25, 22, 26, 26, 24]
[25, 17, 26, 24, 24, 23, 26, 25, 26]
[25, 18, 25, 24, 24, 23, 25, 26, 26]
[25, 18, 25, 24, 23, 24, 26, 25, 26]
251825242324262526

Jonathan Quarrie
May 14, 2017

As we're submitting programmable solutions, I may as well add an alternative which I achieved in LibreOffice Calc, with Iterations enabled in Tools - Options - LibreOffice Calc - Calculate.

  • Create 1-9 in cells A1:A9.
  • Copy the first formula from the table below.
  • Paste into B1.
  • Copy B1.
  • Paste into B2:B9.
  • Keep pasting back into B1 until the numbers stop iterating.
A B
1 =27-(1+LEN($B$1)-LEN(SUBSTITUTE($B$1,A1,""))+LEN($B$2)-LEN(SUBSTITUTE($B$2,A1,""))+LEN($B$3)-LEN(SUBSTITUTE($B$3,A1,""))+LEN($B$4)-LEN(SUBSTITUTE($B$4,A1,""))+LEN($B$5)-LEN(SUBSTITUTE($B$5,A1,""))+LEN($B$6)-LEN(SUBSTITUTE($B$6,A1,""))+LEN($B$7)-LEN(SUBSTITUTE($B$7,A1,""))+LEN($B$8)-LEN(SUBSTITUTE($B$8,A1,""))+LEN($B$9)-LEN(SUBSTITUTE($B$9,A1,"")))
2 =27-(1+LEN($B$1)-LEN(SUBSTITUTE($B$1,A2,""))+LEN($B$2)-LEN(SUBSTITUTE($B$2,A2,""))+LEN($B$3)-LEN(SUBSTITUTE($B$3,A2,""))+LEN($B$4)-LEN(SUBSTITUTE($B$4,A2,""))+LEN($B$5)-LEN(SUBSTITUTE($B$5,A2,""))+LEN($B$6)-LEN(SUBSTITUTE($B$6,A2,""))+LEN($B$7)-LEN(SUBSTITUTE($B$7,A2,""))+LEN($B$8)-LEN(SUBSTITUTE($B$8,A2,""))+LEN($B$9)-LEN(SUBSTITUTE($B$9,A2,"")))
3 =27-(1+LEN($B$1)-LEN(SUBSTITUTE($B$1,A3,""))+LEN($B$2)-LEN(SUBSTITUTE($B$2,A3,""))+LEN($B$3)-LEN(SUBSTITUTE($B$3,A3,""))+LEN($B$4)-LEN(SUBSTITUTE($B$4,A3,""))+LEN($B$5)-LEN(SUBSTITUTE($B$5,A3,""))+LEN($B$6)-LEN(SUBSTITUTE($B$6,A3,""))+LEN($B$7)-LEN(SUBSTITUTE($B$7,A3,""))+LEN($B$8)-LEN(SUBSTITUTE($B$8,A3,""))+LEN($B$9)-LEN(SUBSTITUTE($B$9,A3,"")))
4 =27-(1+LEN($B$1)-LEN(SUBSTITUTE($B$1,A4,""))+LEN($B$2)-LEN(SUBSTITUTE($B$2,A4,""))+LEN($B$3)-LEN(SUBSTITUTE($B$3,A4,""))+LEN($B$4)-LEN(SUBSTITUTE($B$4,A4,""))+LEN($B$5)-LEN(SUBSTITUTE($B$5,A4,""))+LEN($B$6)-LEN(SUBSTITUTE($B$6,A4,""))+LEN($B$7)-LEN(SUBSTITUTE($B$7,A4,""))+LEN($B$8)-LEN(SUBSTITUTE($B$8,A4,""))+LEN($B$9)-LEN(SUBSTITUTE($B$9,A4,"")))
5 =27-(1+LEN($B$1)-LEN(SUBSTITUTE($B$1,A5,""))+LEN($B$2)-LEN(SUBSTITUTE($B$2,A5,""))+LEN($B$3)-LEN(SUBSTITUTE($B$3,A5,""))+LEN($B$4)-LEN(SUBSTITUTE($B$4,A5,""))+LEN($B$5)-LEN(SUBSTITUTE($B$5,A5,""))+LEN($B$6)-LEN(SUBSTITUTE($B$6,A5,""))+LEN($B$7)-LEN(SUBSTITUTE($B$7,A5,""))+LEN($B$8)-LEN(SUBSTITUTE($B$8,A5,""))+LEN($B$9)-LEN(SUBSTITUTE($B$9,A5,"")))
6 =27-(1+LEN($B$1)-LEN(SUBSTITUTE($B$1,A6,""))+LEN($B$2)-LEN(SUBSTITUTE($B$2,A6,""))+LEN($B$3)-LEN(SUBSTITUTE($B$3,A6,""))+LEN($B$4)-LEN(SUBSTITUTE($B$4,A6,""))+LEN($B$5)-LEN(SUBSTITUTE($B$5,A6,""))+LEN($B$6)-LEN(SUBSTITUTE($B$6,A6,""))+LEN($B$7)-LEN(SUBSTITUTE($B$7,A6,""))+LEN($B$8)-LEN(SUBSTITUTE($B$8,A6,""))+LEN($B$9)-LEN(SUBSTITUTE($B$9,A6,"")))
7 =27-(1+LEN($B$1)-LEN(SUBSTITUTE($B$1,A7,""))+LEN($B$2)-LEN(SUBSTITUTE($B$2,A7,""))+LEN($B$3)-LEN(SUBSTITUTE($B$3,A7,""))+LEN($B$4)-LEN(SUBSTITUTE($B$4,A7,""))+LEN($B$5)-LEN(SUBSTITUTE($B$5,A7,""))+LEN($B$6)-LEN(SUBSTITUTE($B$6,A7,""))+LEN($B$7)-LEN(SUBSTITUTE($B$7,A7,""))+LEN($B$8)-LEN(SUBSTITUTE($B$8,A7,""))+LEN($B$9)-LEN(SUBSTITUTE($B$9,A7,"")))
8 =27-(1+LEN($B$1)-LEN(SUBSTITUTE($B$1,A8,""))+LEN($B$2)-LEN(SUBSTITUTE($B$2,A8,""))+LEN($B$3)-LEN(SUBSTITUTE($B$3,A8,""))+LEN($B$4)-LEN(SUBSTITUTE($B$4,A8,""))+LEN($B$5)-LEN(SUBSTITUTE($B$5,A8,""))+LEN($B$6)-LEN(SUBSTITUTE($B$6,A8,""))+LEN($B$7)-LEN(SUBSTITUTE($B$7,A8,""))+LEN($B$8)-LEN(SUBSTITUTE($B$8,A8,""))+LEN($B$9)-LEN(SUBSTITUTE($B$9,A8,"")))
9 =27-(1+LEN($B$1)-LEN(SUBSTITUTE($B$1,A9,""))+LEN($B$2)-LEN(SUBSTITUTE($B$2,A9,""))+LEN($B$3)-LEN(SUBSTITUTE($B$3,A9,""))+LEN($B$4)-LEN(SUBSTITUTE($B$4,A9,""))+LEN($B$5)-LEN(SUBSTITUTE($B$5,A9,""))+LEN($B$6)-LEN(SUBSTITUTE($B$6,A9,""))+LEN($B$7)-LEN(SUBSTITUTE($B$7,A9,""))+LEN($B$8)-LEN(SUBSTITUTE($B$8,A9,""))+LEN($B$9)-LEN(SUBSTITUTE($B$9,A9,"")))

Oh woah!!! I didn't know a simple usage of Office can solve this problem! Your patience must be endless! Thanks man.

Just curious, how many iterations did you do to get to the final answer?

Pi Han Goh - 4 years, 1 month ago

Log in to reply

Didn't take much time. I'm just familiar with Calc/Excel.

After pasting the formula into cells B2:B9, it takes 2 iterations of pasting the formula back into B1 to produce the answer, and a 3rd iteration confirms that the answer is correct (because they don't change).

Jonathan Quarrie - 4 years, 1 month ago

Log in to reply

Color me impressed!

This solution is really unexpected, man! You have my gratitude!!

Pi Han Goh - 4 years, 1 month ago

Log in to reply

@Pi Han Goh Thanks. Appreciation appreciated! :)

Jonathan Quarrie - 4 years, 1 month ago
Hadia Qadir
Aug 30, 2015

You have to include all digits in the box in your counts. This includes the digits in your answer. This means the answers will all be two digits. 25 18 25 24 23 24 26 25 26

What we have to count ?

Syed Baqir - 5 years, 9 months ago

What is it that we are counting, and how do we get 25 from 1?

Ash Norsidi - 3 years, 2 months ago
Nathan Russell
Aug 8, 2015

251825242324262526

You have to include all digits in the box in your counts. This includes the digits in your answer. This means the answers will all be two digits.

  1. 25
  2. 18
  3. 25
  4. 24
  5. 23
  6. 24
  7. 26
  8. 25
  9. 26

How did you do this?

Pi Han Goh - 5 years, 10 months ago

Log in to reply

Iteratively on paper. With just single digit answers included the largest answer would be 17 which breaks all answers being single digit. Starting with the assumption that all answers are two digits, the largest answer is 26 and the smallest is going to be two at around 18. Then you can move forward iteratively working through the answer. Start by assuming all answers are 26 except for two which is 18. The answer will stabilize on the above.

Nathan Russell - 5 years, 10 months ago

Log in to reply

Could you show more of the intermediate steps? I'm curious!

Pi Han Goh - 5 years, 10 months ago

Log in to reply

@Pi Han Goh I didn't feel it was difficult enough to require this. Working on iPad, may type out each step later on PC.

Nathan Russell - 5 years, 10 months ago

I don't understand the counting parts. What is it that we are counting?

Ash Norsidi - 3 years, 2 months ago

1 pending report

Vote up reports you agree with

×

Problem Loading...

Note Loading...

Set Loading...