1

My challenge is to output the total number of five-digit numbers that have a digit 5, but no digits 8. My only two answers so far have been 0, or 90000. Can anyone help me?

#include <iostream>
using namespace std;

int main() {
    int number;
    int counter=10000;
    int ncounter=0;
    while (counter >= 10000 && counter <= 99999) {
        int n1,n2,n3,n4,n5;
        counter = counter + 1;
        n1 = number%10;
        number /= 10;
        n2 = number%10;
        number /= 10;
        n3 = number%10;
        number /= 10;
        n4 = number%10;
        number /=10;
        n5 = number%10;
        number /= 10;
        if (n1 == 5||n2 == 5||n3 == 5||n4 == 5||n5 == 5)
            if (n1!=8)
                if (n2!=8)
                    if (n3!=8)
                        if(n4!=8)
                            if (n5!=8)
                                ncounter=ncounter+1;
    }
    cout<<ncounter<<endl;
    return 0;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Owen Pauli
  • 27
  • 3
  • 2
    Where do you first initialize `number`? – Beta Oct 17 '15 at 03:15
  • Good point. What should I initialize it to? 90000, or 99999? Does it replace counter for all intents & purposes, so the latter can be removed? – Owen Pauli Oct 17 '15 at 03:20
  • Do you mean you need to output 10005, 10015, 10050, 10051, 10055 but **not** 10058? – Tas Oct 17 '15 at 03:21
  • If you use it without initializing it, you're asking for trouble. I advise you to execute this code with pencil and paper (and a smaller range of numbers, e.g. 10005-10008). – Beta Oct 17 '15 at 03:24
  • Programming consists of both analysis and coding. For analysis, you might consider summing the number of such numbers with respectively 1, 2, 3, 4 and 5 "5"-digits. Like, ((5)/(1))8^4 + ((5*4)/(2*1))*8^3 + ((5*4*3)/(3*2*1))*8^2 + ((5*4*3*2)/(4*3*2*1))*8^1 + ((5*4*3*2*1)/(5*4*3*2*1))*8^0. I'm not sure if that's right, but it sounds like the right direction. – Cheers and hth. - Alf Oct 17 '15 at 03:25
  • Yeah, exactly that. I'm a bit confused, especially at the if/else statements. My other option looks like this: `if (n1 == 5||n2 == 5||n3 == 5||n4 == 5||n5 == 5 && n1!=8 || n2!=8 || n3!=8 || n4!=8 || n5!=8)` – Owen Pauli Oct 17 '15 at 03:27

3 Answers3

2

(num with 5 but not 8) = (num without 8) - (num with neither 8 nor 5) = 8*9*9*9*9 - 7*8*8*8*8= 23816

jetstream96
  • 144
  • 4
  • 11
1

Each number is a selection of 5 digits (with repetitions).

Since you cannot select the digit 8, you have 9 possible digits, so this problem is equivalent to the same problem, base 9 (instead of base 10).

If you make 1 digit a 5, there are 4 non-5 and non-8 digits remaining. The number of these can be calculated as 8^4 (because there are 8 available digits to choose from, and you need to choose 4 of these). With a single 5, there are 5 ways to position the 5, so multiply by 5.

Similarly with 2 5's, there are 10 ways to position the 5s relative to other digits.

Therefore, we have the following table:

number of digits==5    remaining digits    ways to position 5s
1                      8^4                 5
2                      8^3                 10 = 5*4/2
3                      8^2                 10
4                      8^1                 5
5                      8^0                 1

There are 5*8^4 + 10*8^3 + 10*8^2 + 5*8^1 + 8^0 = 26281 numbers <10^5 with a 5 but not an 8.

There are 4*8^3 + 6*8^2 + 4*8^1 + 8^0 = 2465 numbers <10^4 with a 5 but not an 8. Therefore, there are 23816 numbers satisfying your criteria.

ronalchn
  • 12,225
  • 10
  • 51
  • 61
  • Here is my code now: `#include using namespace std; int main() { int number; int counter=99999; int ncounter=5*8*8*8*8 + 10*8*8*8 + 10*8*8 + 5*8 + 1; while (number >= 10000 && counter <= 99999) { int n1,n2,n3,n4,n5; counter = counter + 1; n1 = number%10; number /= 10; n2 = number%10; number /= 10; n3 = number%10; number /= 10; n4 = number%10; number /=10; n5 = number%10; number /= 10; } cout< – Owen Pauli Oct 17 '15 at 04:50
  • You didn't take the `>=10000` condition into account. – Nick Volynkin Oct 17 '15 at 19:12
0

This is actually a mathematical problem. Here we have three conditions:

  1. First digit is not zero, as it should be a five-digit number.
  2. No digits are 8
  3. One or more digits are 5

There can be numbers with one to five 5s, where first digit is or is not 5 (except for 55555). That's nine cases to count.

If first digit is not 5, it has 7 options: [1234679]; if any other digit is not 5, it has 8 options: [12346790].

Here C(5) is number of combinations to place 5s, and C(o) - to place other digits.

N(5).  1st?    C(5)   C(o)
1        Y     1   *   8^4
1        N     4   *  7*8^3
2        Y     4   *   8^3
2        N     6   *  7*8^2
3        Y     6   *   8^2
3        N     4   *  7*8
4        Y     4   *  8
4        N     1   *  7
5        Y     1

Sum:     23816
Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67