-5

I need help for a math problem concerning permutations. The task is:

How many four-digit even numbers are there where no digit appears more than once.

I've come this far:

   gerade = set()
   for c in range(1000,9999):
       for d in str(c):
           if int(d) %2 !=0:
               gerade.add(c)
           if 
               break
        print (c)
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
AlKurdi
  • 47
  • 5
  • 2
    When asking homework type questions, rather than simply dumping your assignment, please show your best good faith attempt to solve it and tell what problems you are having. This will give us a better understanding of what you're trying to do, what you might be doing wrong, what you need help with. Please go through the [tour](https://stackoverflow.com/tour), the [help center](https://stackoverflow.com/help) and the [How to Ask](https://stackoverflow.com/questions/how-to-ask sections to see how this site works and to help you improve your current and future questions. – azro Dec 25 '19 at 21:52
  • You're right. I did fix it. @azro – AlKurdi Dec 25 '19 at 21:59
  • 1
    Is writing a script that iterates required? Because this answer can be given relatively straightforward using (very) basic combinatorics methods. – Milan Velebit Dec 25 '19 at 22:04
  • 2
    *...where no digit is double...* What does this mean? Double means 2x, so what is x? – President James K. Polk Dec 25 '19 at 22:04
  • 2
    First, calculate the number of *4-permutations of 10*. Then figure out how many of them begin with `0`. Then divide by 2 to count only the even numbers. Under no circumstances should you iterate through the numbers `1023..9876` and check each one. – High Performance Mark Dec 25 '19 at 22:05
  • with the 4-digit numbers that are issued, no digits may be doubled. Example for correct would be: 1468 Wrong would be: 2222 @JamesReinstateMonicaPolk – AlKurdi Dec 25 '19 at 22:23
  • Still makes no sense to me. Are you trying to saying that no digit may equal 2? That's certainly not the same as "doubled". – President James K. Polk Dec 25 '19 at 22:51
  • Ahhhh. You're saying no digit may appear more than once. Right? – President James K. Polk Dec 25 '19 at 22:53
  • haha Yeah, right. I'm sorry, my english is not the best. I have now, thanks to @azro, the result – AlKurdi Dec 25 '19 at 23:09

2 Answers2

3

We also can solve this problem using math, without checking all possible numbers (a kind of brutforce, not suitable for wider range)

We can choose the fourth digit from 5 variants (even digits)
We can choose the third digit from 9 variants (excluding used digit)
We can choose the second digit from 8 variants (excluding used digits)
We can choose the first digit from 7 variants (excluding used digits)

So we have 5*9*8*7 = 2520 variants with distinct digits.
But some of them are three-digits numbers because the first digit is zero. We must exclude them.
There are 4*8*7 = 224 such numbers and final result is:

  2296 variants = 2520 - 224
MBo
  • 77,366
  • 5
  • 53
  • 86
2
  1. To generate only even numbers, you may just use a step of 2

    for c in range(1000, 9999, 2)
    
  2. Then to count unique digits you may use a set and check the length, if they are the same, keep the value

    gerade = set()
    for c in range(1000, 9999, 2):
        if len(str(c)) == len(set(str(c))):
            gerade.add(c)
    
azro
  • 53,056
  • 7
  • 34
  • 70