Write an algorithm accepts a positive integer n as input, when executed, prints out a list of all self-aware arrays of length n
Self-Aware Arrays.
An integer array int A is self-aware if for each i < A.length, A[i] is the exact number of occurrences of i in A.
For example, [2, 0, 2, 0]
is self-aware.
Write an algorithm accepts a positive integer n as input, when executed, prints out a list of all self-aware arrays of that length.
Here are some Self-aware arrays have been found. But I haven't found an algorithm.
N = 4
2, 0, 2, 0
1, 2, 1, 0
N = 5
2, 1, 2, 0, 0
N = 6
None
N = 7
3, 2, 1, 1, 0, 0, 0
N = 8
4, 2, 1, 0, 1, 0, 0, 0
N = 9
5, 2, 1, 0, 0, 1, 0, 0, 0
N = 10
6, 2, 1, 0, 0, 0, 1, 0, 0, 0
N = 11
7, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0
N = a
a-4, 2, 1, <a-7 0s>, 1, 0, 0, 0
Seems we have the properties
sum(A[i]) = n = sum(i * A[i])
A[0] = sum( (i-1) * A[i] ) i>=2
arrays algorithm
add a comment |
Self-Aware Arrays.
An integer array int A is self-aware if for each i < A.length, A[i] is the exact number of occurrences of i in A.
For example, [2, 0, 2, 0]
is self-aware.
Write an algorithm accepts a positive integer n as input, when executed, prints out a list of all self-aware arrays of that length.
Here are some Self-aware arrays have been found. But I haven't found an algorithm.
N = 4
2, 0, 2, 0
1, 2, 1, 0
N = 5
2, 1, 2, 0, 0
N = 6
None
N = 7
3, 2, 1, 1, 0, 0, 0
N = 8
4, 2, 1, 0, 1, 0, 0, 0
N = 9
5, 2, 1, 0, 0, 1, 0, 0, 0
N = 10
6, 2, 1, 0, 0, 0, 1, 0, 0, 0
N = 11
7, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0
N = a
a-4, 2, 1, <a-7 0s>, 1, 0, 0, 0
Seems we have the properties
sum(A[i]) = n = sum(i * A[i])
A[0] = sum( (i-1) * A[i] ) i>=2
arrays algorithm
What have you tried so far?
– jrook
Nov 14 '18 at 22:02
See Self-descriptive number for a reference. en.m.wikipedia.org/wiki/Self-descriptive_number
– Shang Gao
Nov 15 '18 at 15:51
add a comment |
Self-Aware Arrays.
An integer array int A is self-aware if for each i < A.length, A[i] is the exact number of occurrences of i in A.
For example, [2, 0, 2, 0]
is self-aware.
Write an algorithm accepts a positive integer n as input, when executed, prints out a list of all self-aware arrays of that length.
Here are some Self-aware arrays have been found. But I haven't found an algorithm.
N = 4
2, 0, 2, 0
1, 2, 1, 0
N = 5
2, 1, 2, 0, 0
N = 6
None
N = 7
3, 2, 1, 1, 0, 0, 0
N = 8
4, 2, 1, 0, 1, 0, 0, 0
N = 9
5, 2, 1, 0, 0, 1, 0, 0, 0
N = 10
6, 2, 1, 0, 0, 0, 1, 0, 0, 0
N = 11
7, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0
N = a
a-4, 2, 1, <a-7 0s>, 1, 0, 0, 0
Seems we have the properties
sum(A[i]) = n = sum(i * A[i])
A[0] = sum( (i-1) * A[i] ) i>=2
arrays algorithm
Self-Aware Arrays.
An integer array int A is self-aware if for each i < A.length, A[i] is the exact number of occurrences of i in A.
For example, [2, 0, 2, 0]
is self-aware.
Write an algorithm accepts a positive integer n as input, when executed, prints out a list of all self-aware arrays of that length.
Here are some Self-aware arrays have been found. But I haven't found an algorithm.
N = 4
2, 0, 2, 0
1, 2, 1, 0
N = 5
2, 1, 2, 0, 0
N = 6
None
N = 7
3, 2, 1, 1, 0, 0, 0
N = 8
4, 2, 1, 0, 1, 0, 0, 0
N = 9
5, 2, 1, 0, 0, 1, 0, 0, 0
N = 10
6, 2, 1, 0, 0, 0, 1, 0, 0, 0
N = 11
7, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0
N = a
a-4, 2, 1, <a-7 0s>, 1, 0, 0, 0
Seems we have the properties
sum(A[i]) = n = sum(i * A[i])
A[0] = sum( (i-1) * A[i] ) i>=2
arrays algorithm
arrays algorithm
edited Nov 15 '18 at 15:53
Shang Gao
asked Nov 14 '18 at 21:59
Shang GaoShang Gao
312
312
What have you tried so far?
– jrook
Nov 14 '18 at 22:02
See Self-descriptive number for a reference. en.m.wikipedia.org/wiki/Self-descriptive_number
– Shang Gao
Nov 15 '18 at 15:51
add a comment |
What have you tried so far?
– jrook
Nov 14 '18 at 22:02
See Self-descriptive number for a reference. en.m.wikipedia.org/wiki/Self-descriptive_number
– Shang Gao
Nov 15 '18 at 15:51
What have you tried so far?
– jrook
Nov 14 '18 at 22:02
What have you tried so far?
– jrook
Nov 14 '18 at 22:02
See Self-descriptive number for a reference. en.m.wikipedia.org/wiki/Self-descriptive_number
– Shang Gao
Nov 15 '18 at 15:51
See Self-descriptive number for a reference. en.m.wikipedia.org/wiki/Self-descriptive_number
– Shang Gao
Nov 15 '18 at 15:51
add a comment |
1 Answer
1
active
oldest
votes
Outer loop: Generate all candidates of length n
. This can be done with any convenient set product software; you need n
elements in the range 0:n-1. For each candidate, filter and check (below).
Filter:
- The sum of the candidates elements must equal
n
. Note that if you're writing your own candidate generator, you can use this to greatly reduce the quantity of candidates. - The first element can never be 0.
Check:
Count the quantity of each number 0:n-1 in order. The resulting list is your check count. If this list is equal to the original candidate, you have a solution: print it.
Shortcuts:
Are you allowed to use known results? It's long proved that for n >= 7, the formula you give is the only solution: you can directly generate the one solution and return. For n < 7, brute force will produce all valid solutions quite quickly.
How can we prove for n >= 7, the formul is the only solution ?
– Shang Gao
Nov 15 '18 at 1:51
The first place I saw it proved was, I think, in Hofstadter's "MetaMagical Themas" column in Scientific American. I've seen it in other places. You can either research the known solutions, or work through the combinatorics. There are some early restrictions that make it easier: the sum of the array must ben
, and the first element cannot be0
. Once you get up to 7 and 8, I recall that there's an inductive proof for the others.
– Prune
Nov 15 '18 at 16:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309363%2fwrite-an-algorithm-accepts-a-positive-integer-n-as-input-when-executed-prints%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Outer loop: Generate all candidates of length n
. This can be done with any convenient set product software; you need n
elements in the range 0:n-1. For each candidate, filter and check (below).
Filter:
- The sum of the candidates elements must equal
n
. Note that if you're writing your own candidate generator, you can use this to greatly reduce the quantity of candidates. - The first element can never be 0.
Check:
Count the quantity of each number 0:n-1 in order. The resulting list is your check count. If this list is equal to the original candidate, you have a solution: print it.
Shortcuts:
Are you allowed to use known results? It's long proved that for n >= 7, the formula you give is the only solution: you can directly generate the one solution and return. For n < 7, brute force will produce all valid solutions quite quickly.
How can we prove for n >= 7, the formul is the only solution ?
– Shang Gao
Nov 15 '18 at 1:51
The first place I saw it proved was, I think, in Hofstadter's "MetaMagical Themas" column in Scientific American. I've seen it in other places. You can either research the known solutions, or work through the combinatorics. There are some early restrictions that make it easier: the sum of the array must ben
, and the first element cannot be0
. Once you get up to 7 and 8, I recall that there's an inductive proof for the others.
– Prune
Nov 15 '18 at 16:44
add a comment |
Outer loop: Generate all candidates of length n
. This can be done with any convenient set product software; you need n
elements in the range 0:n-1. For each candidate, filter and check (below).
Filter:
- The sum of the candidates elements must equal
n
. Note that if you're writing your own candidate generator, you can use this to greatly reduce the quantity of candidates. - The first element can never be 0.
Check:
Count the quantity of each number 0:n-1 in order. The resulting list is your check count. If this list is equal to the original candidate, you have a solution: print it.
Shortcuts:
Are you allowed to use known results? It's long proved that for n >= 7, the formula you give is the only solution: you can directly generate the one solution and return. For n < 7, brute force will produce all valid solutions quite quickly.
How can we prove for n >= 7, the formul is the only solution ?
– Shang Gao
Nov 15 '18 at 1:51
The first place I saw it proved was, I think, in Hofstadter's "MetaMagical Themas" column in Scientific American. I've seen it in other places. You can either research the known solutions, or work through the combinatorics. There are some early restrictions that make it easier: the sum of the array must ben
, and the first element cannot be0
. Once you get up to 7 and 8, I recall that there's an inductive proof for the others.
– Prune
Nov 15 '18 at 16:44
add a comment |
Outer loop: Generate all candidates of length n
. This can be done with any convenient set product software; you need n
elements in the range 0:n-1. For each candidate, filter and check (below).
Filter:
- The sum of the candidates elements must equal
n
. Note that if you're writing your own candidate generator, you can use this to greatly reduce the quantity of candidates. - The first element can never be 0.
Check:
Count the quantity of each number 0:n-1 in order. The resulting list is your check count. If this list is equal to the original candidate, you have a solution: print it.
Shortcuts:
Are you allowed to use known results? It's long proved that for n >= 7, the formula you give is the only solution: you can directly generate the one solution and return. For n < 7, brute force will produce all valid solutions quite quickly.
Outer loop: Generate all candidates of length n
. This can be done with any convenient set product software; you need n
elements in the range 0:n-1. For each candidate, filter and check (below).
Filter:
- The sum of the candidates elements must equal
n
. Note that if you're writing your own candidate generator, you can use this to greatly reduce the quantity of candidates. - The first element can never be 0.
Check:
Count the quantity of each number 0:n-1 in order. The resulting list is your check count. If this list is equal to the original candidate, you have a solution: print it.
Shortcuts:
Are you allowed to use known results? It's long proved that for n >= 7, the formula you give is the only solution: you can directly generate the one solution and return. For n < 7, brute force will produce all valid solutions quite quickly.
answered Nov 14 '18 at 23:20
PrunePrune
45.4k143559
45.4k143559
How can we prove for n >= 7, the formul is the only solution ?
– Shang Gao
Nov 15 '18 at 1:51
The first place I saw it proved was, I think, in Hofstadter's "MetaMagical Themas" column in Scientific American. I've seen it in other places. You can either research the known solutions, or work through the combinatorics. There are some early restrictions that make it easier: the sum of the array must ben
, and the first element cannot be0
. Once you get up to 7 and 8, I recall that there's an inductive proof for the others.
– Prune
Nov 15 '18 at 16:44
add a comment |
How can we prove for n >= 7, the formul is the only solution ?
– Shang Gao
Nov 15 '18 at 1:51
The first place I saw it proved was, I think, in Hofstadter's "MetaMagical Themas" column in Scientific American. I've seen it in other places. You can either research the known solutions, or work through the combinatorics. There are some early restrictions that make it easier: the sum of the array must ben
, and the first element cannot be0
. Once you get up to 7 and 8, I recall that there's an inductive proof for the others.
– Prune
Nov 15 '18 at 16:44
How can we prove for n >= 7, the formul is the only solution ?
– Shang Gao
Nov 15 '18 at 1:51
How can we prove for n >= 7, the formul is the only solution ?
– Shang Gao
Nov 15 '18 at 1:51
The first place I saw it proved was, I think, in Hofstadter's "MetaMagical Themas" column in Scientific American. I've seen it in other places. You can either research the known solutions, or work through the combinatorics. There are some early restrictions that make it easier: the sum of the array must be
n
, and the first element cannot be 0
. Once you get up to 7 and 8, I recall that there's an inductive proof for the others.– Prune
Nov 15 '18 at 16:44
The first place I saw it proved was, I think, in Hofstadter's "MetaMagical Themas" column in Scientific American. I've seen it in other places. You can either research the known solutions, or work through the combinatorics. There are some early restrictions that make it easier: the sum of the array must be
n
, and the first element cannot be 0
. Once you get up to 7 and 8, I recall that there's an inductive proof for the others.– Prune
Nov 15 '18 at 16:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309363%2fwrite-an-algorithm-accepts-a-positive-integer-n-as-input-when-executed-prints%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What have you tried so far?
– jrook
Nov 14 '18 at 22:02
See Self-descriptive number for a reference. en.m.wikipedia.org/wiki/Self-descriptive_number
– Shang Gao
Nov 15 '18 at 15:51