Skip to main content
minor optimization of divmods
Source Link
tevemadar
  • 13.4k
  • 3
  • 27
  • 64
rnd=random.randint(0,4535)
(rnd,d1)=divmod(rnd,9)
(rnd,d2)=divmod(rnd,9)
#(rnd,d3)=divmod(rnd,8)
#(rnd,d4)=divmod(rnd,7)
(d4,d3)=divmod(rnd,8) # miracle found: 1 divmod happens to run faster than 2
rnd=random.randint(0,4535)
(rnd,d1)=divmod(rnd,9)
(rnd,d2)=divmod(rnd,9)
(rnd,d3)=divmod(rnd,8)
(rnd,d4)=divmod(rnd,7)
rnd=random.randint(0,4535)
(rnd,d1)=divmod(rnd,9)
(rnd,d2)=divmod(rnd,9)
#(rnd,d3)=divmod(rnd,8)
#(rnd,d4)=divmod(rnd,7)
(d4,d3)=divmod(rnd,8) # miracle found: 1 divmod happens to run faster than 2
added 13 characters in body
Source Link
tevemadar
  • 13.4k
  • 3
  • 27
  • 64

Now we have d1=0..8, d2=0..8, d3=0..7, d4=0..6, it can be tested via running the snippet with rnd=4535 (4535=998*7-1, by the way) First

First, d1 has to be patched up

This is an approach maintaining an 1:1 relation between the minimal set of valid inputs (0...4535) and valid outputs (the 998*7 possible 4-digit numbers with distinct digits, not-starting-with-0). So a simple loop can and should generate all the numbers, they can be checked one-by-one and they can be collected into a set for example in order to see if they are all distinct results

Now we have d1=0..8, d2=0..8, d3=0..7, d4=0..6, it can be tested via running the snippet with rnd=4535 (4535=998*7-1, by the way) First, d1 has to be patched up

This is an approach maintaining an 1:1 relation between the minimal set of valid inputs (0...4535) and valid outputs (the 998*7 possible 4-digit numbers with distinct digits, not-starting-with-0). So a simple loop can generate all the numbers, they can be checked one-by-one and they can be collected into a set for example in order to see if they are all distinct results

Now we have d1=0..8, d2=0..8, d3=0..7, d4=0..6, it can be tested via running the snippet with rnd=4535 (4535=998*7-1, by the way)

First, d1 has to be patched up

This is an approach maintaining an 1:1 relation between the minimal set of valid inputs (0...4535) and valid outputs (the 998*7 possible 4-digit numbers with distinct digits, not-starting-with-0). So a simple loop can and should generate all the numbers, they can be checked one-by-one and they can be collected into a set for example in order to see if they are all distinct results

about checking if it works correctly
Source Link
tevemadar
  • 13.4k
  • 3
  • 27
  • 64

EDIT: about checking this and that

This is an approach maintaining an 1:1 relation between the minimal set of valid inputs (0...4535) and valid outputs (the 998*7 possible 4-digit numbers with distinct digits, not-starting-with-0). So a simple loop can generate all the numbers, they can be checked one-by-one and they can be collected into a set for example in order to see if they are all distinct results

Practically:

collect=set()
for rnd in range(0,4536):
    (rnd,d1)=divmod(rnd,9)
    ... rest of the code, also the verification step kept active ...
    collect.add(val)
print(len(collect))
  1. It will not print anything in the loop (all results are 4-digit numbers with distinct digits)

  2. It will print 4536 at the end (all results are distinct)

One can add a verification for the first digit (d1), here and now I just assume that
"(something mod 9)+1" will not be 0.

EDIT: about checking this and that

This is an approach maintaining an 1:1 relation between the minimal set of valid inputs (0...4535) and valid outputs (the 998*7 possible 4-digit numbers with distinct digits, not-starting-with-0). So a simple loop can generate all the numbers, they can be checked one-by-one and they can be collected into a set for example in order to see if they are all distinct results

Practically:

collect=set()
for rnd in range(0,4536):
    (rnd,d1)=divmod(rnd,9)
    ... rest of the code, also the verification step kept active ...
    collect.add(val)
print(len(collect))
  1. It will not print anything in the loop (all results are 4-digit numbers with distinct digits)

  2. It will print 4536 at the end (all results are distinct)

One can add a verification for the first digit (d1), here and now I just assume that
"(something mod 9)+1" will not be 0.

Source Link
tevemadar
  • 13.4k
  • 3
  • 27
  • 64
Loading