Python 3, n≈40
def plausible_suffix(l,N):
if sum(l)>N:
return False
pairs = [(N-1-i,l[i]) for i in range(len(l))]
if sum(i*x for i,x in pairs)>N:
return False
num_remaining = N - len(l)
for index, desired_count in pairs:
count = l.count(index)
more_needed = desired_count - count
if more_needed<0:
return False
num_remaining -= more_needed
if num_remaining<0:
return False
return True
plausible_func = plausible_suffix
def generate_magic(N):
l=[0]
while l:
extend = False
if plausible_func(l,N):
if len(l)==N:
yield l[::-1]
else:
extend = True
if extend:
l.append(0)
else:
while l[-1]>=N-2:
l.pop(-1)
if not l:raise StopIteration
l[-1]+=1
n=40 #test parameter
if n>0:
for x in generate_magic(n):
print(n,x)
Does a breadth-first search of possible lists, filling in entries from right to left, stopping the search at a suffix if it isn't plausible, which can happen if:
- The sum of the entries in the suffix exceeds
n
(the sum for the whole list must be n
)
- The weighted sum of
i*l[i]
in the suffix exceeds n
(the sum for the whole list must be n
)
- Any number appears in the suffix more times that the suffix says it should
- The number of remaining unfilled spots is too small to account for all numbers that need to appear more times.
I had original tested prefixes left to right, but that went more slowly.
The outputs up to n=30
are:
4 [1, 2, 1, 0]
4 [2, 0, 2, 0]
5 [2, 1, 2, 0, 0]
7 [3, 2, 1, 1, 0, 0, 0]
8 [4, 2, 1, 0, 1, 0, 0, 0]
9 [5, 2, 1, 0, 0, 1, 0, 0, 0]
10 [6, 2, 1, 0, 0, 0, 1, 0, 0, 0]
11 [7, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0]
12 [8, 2, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0]
13 [9, 2, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
14 [10, 2, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
15 [11, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
16 [12, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
17 [13, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 1, 2, 0, 0]
, there is exactly one list of each length n>6
, and it has the form [n-4, 2, 1, ..., 0, 0, 1, 0, 0, 0]
. This pattern persists up to at least n=50
. I suspect it holds forever, in which case it's trivial to output a huge number of these. Even if not, a mathematical understanding on the possible solutions would greatly speed up a search.
5Welcome to PPCG! This is a great challenge, but you need a winning criterion. For example, you could say that the winner is the shortest program. – Ypnypn – 2015-05-14T00:12:37.217
1
Relevant: Self-descriptive number
– Sp3000 – 2015-05-14T00:23:51.187@Ypnypn thanks I've added the winning criteria – Agnishom Chattopadhyay – 2015-05-14T00:31:24.890
Sorry, I changed the winning criteria. – Agnishom Chattopadhyay – 2015-05-14T00:49:00.103
2Please don't change the winning criterion after answers have been posted. Also, this was much better as a code golf than as fastest code, at least in my opinion. – Alex A. – 2015-05-14T00:55:53.883
For fastest code, is there a reasonable approach other than enumerating all sequences and checking for magicness? – xnor – 2015-05-14T01:29:38.397
2@xnor you can start by generating the integer partitions of n and checking if they can be self-descriptive. – Martin Ender – 2015-05-14T01:36:52.233
@MartinBüttner Good point, I'm seeing some interesting ways to short-circuit the searches now. – xnor – 2015-05-14T01:49:24.947
2What's the smallest
n>5
with a solution not of the form[n-4, 2, 1, ..., 0, 0, 1, 0, 0, 0]
? I've looked up ton=20
and not found one, and wondering if I'm making an error. – xnor – 2015-05-14T02:43:50.8171Given that xnor has now established that this has a trivial solution, and is therefore not appropriate for fastest code, can we switch it back to code-golf? – isaacg – 2015-05-14T07:05:50.503
This is not appropriate as [tag:fastest-code] - you're only limited by I/O speed. – orlp – 2015-05-14T08:54:35.307