Post Reply 
July 2018 little math problem
07-31-2018, 10:49 PM
Post: #27
RE: July 2018 little math problem
Hi, Komanguy

I did a Python version of the zigzag Hp Prime program. With comments, perhaps this is easier to follow:

Code:
def zigzag_prime(slot, filled):
    if filled >= 5:             # 2 or more sums to match
        sum = slot[1] + slot[2] + slot[3]
        if filled == 5:
            if sum != slot[3] + slot[4] + slot[5]: return
        elif filled == 7:
            if sum != slot[5] + slot[6] + slot[7]: return
        elif filled == 8:       # same as filled == 9, but faster
            if sum == slot[7] + slot[8] + slot[9]: print slot[1:]
            return              # success or not, quit

    next = filled + 1           # validated, add next digit
    old = slot[next]
    for i in range(next, 10):   # slot[1 to filled] is FIXED
        new = slot[i]
        slot[next], slot[i] = new, old      # try it all
        zigzag_prime(slot, next)            # recurse
        slot[next], slot[i] = old, new      # restore slot
        
zigzag_prime(range(10), 0)      # nothing filled yet
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
July 2018 little math problem - pier4r - 07-25-2018, 08:52 PM
RE: July 2018 little math problem - DavidM - 07-26-2018, 04:03 AM
RE: July 2018 little math problem - DavidM - 07-26-2018, 03:38 PM
RE: July 2018 little math problem - pier4r - 07-26-2018, 12:36 PM
RE: July 2018 little math problem - pier4r - 07-27-2018, 10:03 AM
RE: July 2018 little math problem - DavidM - 07-28-2018, 04:22 PM
RE: July 2018 little math problem - Albert Chan - 07-31-2018 10:49 PM
RE: July 2018 little math problem - pier4r - 08-01-2018, 02:13 PM



User(s) browsing this thread: 1 Guest(s)