Search code examples
pythonsudokuoperation

How to improve speed creating really big groups and searching through them in Python 3


So, I need some help speeding up my program and making it more efficient, so that it doesn't take so much memory a laptop can't handle it. First of all, I'm creating a program to solve sudokus. You enter a plain value (1111111111111111) then, with some manipulations, change some numbers, and then it start to check the validity of the sudoku. If it's not a legal one, it searches if the variable "y", a counter/index like thing, is in one of the group and in the first one it is, it operates some summs. You'll see:

tablero = 1111111111111111
g1 = range(4,4294967296,4)
g2 = range(16,4294967296,16)
g3 = range(64,4294967296,64)
g4 = range(256,4294967296,256)
g5 = range(1024,4294967296,1024)
g6 = range(4096,4294967296,4096)
g7 = range(16384,4294967296,16384)
g8 = range(65536,4294967296,65536)
g9 = range(262144,4294967296,262144)
g10 = range(1048576,4294967296,1048576)
g11 = range(4194304,4294967296,4194304)
g12 = range(16777216,4294967296,16777216)
g13 = range(67108864,4294967296,67108864)
g14 = range(268435456,4294967296,268435456)
g15 = range(1073741824,4294967296,1073741824)
g16 = range(500000,4294967296,500000)


y = 1
def nueva_conf():
    global tablero
    global y
    if y in g15:
        tablero = tablero + 700000000000000 - 33333333333333
    elif y in g14:
        tablero = tablero + 70000000000000 - 3333333333333
    elif y in g13:
        tablero = tablero + 7000000000000 - 333333333333
    elif y in g12:
        tablero = tablero + 700000000000 - 33333333333
    elif y in g11:
        tablero = tablero + 70000000000 - 3333333333
    elif y in g10:
        tablero = tablero + 7000000000 - 333333333

    ...

    elif y in g3:
        tablero = tablero + 700 - 33
    elif y in g2:
        tablero = tablero + 70 - 3
    elif y in g1:
        tablero = tablero + 7
    else:
        tablero = tablero + 1

    tablero_copy = tablero
    y = y+1

That's the part I need to improve, as it is the one that I think eats a big chunk of my memory. I really don't know how to make it be more efficient. I really want it to work this way, with the operations and all that miscellaneous. I'm already working on another way to solve sudokus, but that's the one I've made independently, without help, and I feel it has it's possibility. What do you think, Python programmers?


Solution

  • Instead of checking against a list of numbers, just check if the rule is true. That's much faster, and can be proceduralized:

    tablero = 1111111111111111
    g_values = [500000, 1073741824, 268435456, 67108864, 16777216, 4194304, 1048576, 262144, 65536, 16384, 4096, 1024, 256, 64, 16, 4, 1]
    d_values = [6666666666666667, 666666666666667, 66666666666667, 6666666666667, 666666666667, 66666666667, 6666666667, 666666667, 66666667, 6666667, 666667, 66667, 6667, 667, 67, 7, 1]
    y = 1
    def nueva_conf():
        global tablero
        global y
        if y > 0 and y <= 4294967296:  # required for all g ranges
            for d, g, i in reversed(zip(d_values, g_values, range(17)):
                if i == 0:
                    continue # skip g=500000
                if (y % g) == 0:  # check for divisibility
                    tablero += d  # add value
                    break         # break out of loop
        tablero_copy = tablero
        y += 1