Post Reply 
Puzzle - RPL and others
05-06-2021, 09:09 PM
Post: #29
RE: Puzzle - RPL and others
(05-05-2021 06:29 PM)Albert Chan Wrote:  With this, I confirmed there is no solution for 16 ≤ n ≤ 44
Interestingly, it take less time to run n=42, than n=40

I found out the reason n=42 take less time than n=40. Think non-buckets.

Example, first digit does not belong to any buckets.

>>> n = 10 # = 2 * 5
>>> sorted(set(range(1,n)) - set(range(2,n,2)) - set(range(5,n,5)))
[1, 3, 7, 9]

>>> n = 40 # = 2**3 * 5
>>> sorted(set(range(1,n)) - set(range(2,n,2)) - set(range(5,n,5)))
[1, 3, 7, 9, 11, 13, 17, 19, 21, 23, 27, 29, 31, 33, 37, 39]

>>> n = 42 # = 2 * 3 * 7
>>> sorted(set(range(1,n)) - set(range(2,n,2)) - set(range(3,n,3)) - set(range(7,n,7)))
[1, 5, 11, 13, 17, 19, 23, 25, 29, 31, 37, 41]

This is the set of digits for d1, and n=42 has less elements than n=40.
(looping anything else is just a waste of time)

And, this set applied not just d1, but any dk that is coprime to n.
Conversely, if k is not coprime to n, dk must not be above sets of values.

Combined, we have invariant: coprime(n,k) = coprime(n,dk)

Code:
from euclid import gcd

def puzzle5(n):
    lst = range(n)  # assume even n
    lst[n//2] = 0   # skip center
    return recurse5(lst + [gcd(n,d)==1 for d in lst], n)

def recurse5(lst, n, k=1, x=0):
    if k==n: print x; return
    if k+k==n: x=n*x+k; k+=1    # skip center
    x, step = n*x, k+k
    d1 = k-x%k  # first valid (mod k)
    if k&1:     # odd k
        if not (d1&1): d1 += k  # d1 also odd
    elif k==2:
        d1 = 4 if (n&3) else 2
    else:       # even k >= 4
        bad = (n+k-d1) & 3
        if k&3: d1 += bad and k # d1 (mod4) = n+k
        elif bad: return        # can't fix by +k
        else: step = k          # step (mod4) = 0
    for d in xrange(d1, n, step):
        if not lst[d]: continue
        if lst[n+d] != lst[n+k]: continue
        lst[d] = 0              # mark taken
        recurse5(lst, n, k+1, x+d)
        lst[d] = d              # restore

I doubled up lst for 2 purpose. (lst combined to reduce stress of arguments passing)
First half is to check duplicates, like before.
Second half holds coprime(n,d) table: lst[n+d] ≡ coprime(n,d)

Coprime test benefited if base n has many different factors.
Code:
       time (sec), on my Toshiba A665 laptop
n      puzzle4(n)  puzzle5(n)
32     2           2
34     8           9
36     11          3
38     55          60
40     114         46
42     110         31
44     843         590
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Puzzle - RPL and others - Gene - 04-22-2021, 06:55 PM
RE: Puzzle - RPL and others - rprosperi - 04-23-2021, 04:21 PM
RE: Puzzle - RPL and others - EdS2 - 04-23-2021, 07:30 AM
RE: Puzzle - RPL and others - Dave Britten - 04-23-2021, 12:06 PM
RE: Puzzle - RPL and others - 3298 - 04-23-2021, 09:17 AM
RE: Puzzle - RPL and others - ijabbott - 04-23-2021, 03:57 PM
RE: Puzzle - RPL and others - Albert Chan - 04-23-2021, 04:08 PM
RE: Puzzle - RPL and others - Albert Chan - 04-27-2021, 12:14 PM
RE: Puzzle - RPL and others - 3298 - 04-23-2021, 09:05 PM
RE: Puzzle - RPL and others - C.Ret - 04-24-2021, 04:40 PM
RE: Puzzle - RPL and others - C.Ret - 04-25-2021, 09:25 AM
RE: Puzzle - RPL and others - Claudio L. - 04-26-2021, 04:56 PM
RE: Puzzle - RPL and others - 3298 - 04-27-2021, 08:16 PM
RE: Puzzle - RPL and others - Albert Chan - 04-28-2021, 02:33 AM
RE: Puzzle - RPL and others - Albert Chan - 04-28-2021, 03:30 AM
RE: Puzzle - RPL and others - 3298 - 04-28-2021, 10:14 PM
RE: Puzzle - RPL and others - Albert Chan - 04-29-2021, 03:25 AM
RE: Puzzle - RPL and others - Allen - 04-28-2021, 08:45 PM
RE: Puzzle - RPL and others - Albert Chan - 04-29-2021, 05:16 PM
RE: Puzzle - RPL and others - Allen - 04-29-2021, 07:03 PM
RE: Puzzle - RPL and others - C.Ret - 05-02-2021, 06:40 AM
RE: Puzzle - RPL and others - 3298 - 05-03-2021, 03:43 PM
RE: Puzzle - RPL and others - Albert Chan - 05-04-2021, 03:29 AM
RE: Puzzle - RPL and others - 3298 - 05-04-2021, 06:48 AM
RE: Puzzle - RPL and others - Albert Chan - 05-05-2021, 06:29 PM
RE: Puzzle - RPL and others - 3298 - 05-06-2021, 04:24 PM
RE: Puzzle - RPL and others - Albert Chan - 05-06-2021 09:09 PM
RE: Puzzle - RPL and others - Albert Chan - 05-07-2021, 10:35 AM
RE: Puzzle - RPL and others - 3298 - 05-07-2021, 04:17 PM
RE: Puzzle - RPL and others - Albert Chan - 05-09-2021, 01:21 AM
RE: Puzzle - RPL and others - 3298 - 05-09-2021, 01:39 PM
RE: Puzzle - RPL and others - Albert Chan - 05-10-2021, 03:57 AM
RE: Puzzle - RPL and others - Albert Chan - 05-07-2021, 02:56 AM
RE: Puzzle - RPL and others - Albert Chan - 05-10-2021, 05:13 PM
RE: Puzzle - RPL and others - 3298 - 05-10-2021, 08:23 PM
RE: Puzzle - RPL and others - Albert Chan - 05-11-2021, 11:58 AM
RE: Puzzle - RPL and others - 3298 - 05-11-2021, 02:14 PM
RE: Puzzle - RPL and others - John Keith - 05-11-2021, 03:55 PM
RE: Puzzle - RPL and others - ijabbott - 05-11-2021, 10:37 PM
RE: Puzzle - RPL and others - Albert Chan - 05-13-2021, 11:38 PM



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