Eight coins for the fair king
This is a "counterpart" of another puzzle, Eight coins for the fair king on Puzzling.SE.
You can read the above puzzle for the background. The details about this puzzle are as follows.
A set of 8 kinds of coins of different values are created, the king wants you to find out the maximum N such that any number of price from 0 to N can be paid with a combination no more than 8 coins and without charges.
For example, (taken from Glorfindel's answer). If a set of coins of values 1, 2, 5, 13, 34, 89, 233, 610 are given, your program should output 1596, because every number between 0 and 1596 (inclusive) can be represented by the sum of no more than 8 numbers from the given list (numbers may repeat), while 1597 cannot be represented in that way.
In a mathematical way, if the input is a set S consisting of 8 positive integers, the desired output N satisfies that for any number n between 0 and N, there exists x1, x2, x3, ..., x8 such that
$$x_1 + x_2 + ... + x_8 = n quadtextrm{and}quad x_1, x_2, ...,x_8 in {0} cup S$$
Your goal is to write a program, a function, or a snippet that takes 8 numbers as input, and output the maximum N as described above.
Rules:
- Flexible I/O allowed, so your program can take the input in any form that's best suitable. You may assume that the input numbers are sorted in the way that best suits your program.
- Input is a set of 8 different, positive integers (no zeros). The output is one non-negative integer.
- In case there's no 1 in the input set, your program should output 0 because any number from 0 to 0 satisfies the requirement.
- In the case of invalid input (the set contains zero or negative numbers), your program can do anything.
- Standard loopholes are forbidden.
- Your program should run within a few minutes on a modern computer.
Test cases (mostly taken from the answers under the linked question on Puzzling):
[1, 2, 3, 4, 5, 6, 7, 8] => 64
[2, 3, 4, 5, 6, 7, 8, 9] => 0
[1, 3, 4, 5, 6, 7, 8, 9] => 72
[1, 2, 5, 13, 34, 89, 233, 610] => 1596
[1, 5, 16, 51, 130, 332, 471, 1082] => 2721
[1, 6, 20, 75, 175, 474, 756, 785] => 3356
This is a code-golf, so the shortest program or snippet in each language wins!
code-golf number-theory integer
|
show 5 more comments
This is a "counterpart" of another puzzle, Eight coins for the fair king on Puzzling.SE.
You can read the above puzzle for the background. The details about this puzzle are as follows.
A set of 8 kinds of coins of different values are created, the king wants you to find out the maximum N such that any number of price from 0 to N can be paid with a combination no more than 8 coins and without charges.
For example, (taken from Glorfindel's answer). If a set of coins of values 1, 2, 5, 13, 34, 89, 233, 610 are given, your program should output 1596, because every number between 0 and 1596 (inclusive) can be represented by the sum of no more than 8 numbers from the given list (numbers may repeat), while 1597 cannot be represented in that way.
In a mathematical way, if the input is a set S consisting of 8 positive integers, the desired output N satisfies that for any number n between 0 and N, there exists x1, x2, x3, ..., x8 such that
$$x_1 + x_2 + ... + x_8 = n quadtextrm{and}quad x_1, x_2, ...,x_8 in {0} cup S$$
Your goal is to write a program, a function, or a snippet that takes 8 numbers as input, and output the maximum N as described above.
Rules:
- Flexible I/O allowed, so your program can take the input in any form that's best suitable. You may assume that the input numbers are sorted in the way that best suits your program.
- Input is a set of 8 different, positive integers (no zeros). The output is one non-negative integer.
- In case there's no 1 in the input set, your program should output 0 because any number from 0 to 0 satisfies the requirement.
- In the case of invalid input (the set contains zero or negative numbers), your program can do anything.
- Standard loopholes are forbidden.
- Your program should run within a few minutes on a modern computer.
Test cases (mostly taken from the answers under the linked question on Puzzling):
[1, 2, 3, 4, 5, 6, 7, 8] => 64
[2, 3, 4, 5, 6, 7, 8, 9] => 0
[1, 3, 4, 5, 6, 7, 8, 9] => 72
[1, 2, 5, 13, 34, 89, 233, 610] => 1596
[1, 5, 16, 51, 130, 332, 471, 1082] => 2721
[1, 6, 20, 75, 175, 474, 756, 785] => 3356
This is a code-golf, so the shortest program or snippet in each language wins!
code-golf number-theory integer
Nice puzzle, but I personally think that some more test cases would be helpful in order to test our submissions.
– Mr. Xcoder
20 hours ago
Wouldn't it be better to make input size a parameter? Brute force approaches will struggle with 8
– Luis Mendo
20 hours ago
@LuisMendo Brute force approaches don't run within a reasonable amount of time. But I can't find a way to codify this as a rule.
– iBug
20 hours ago
@iBug Do you mean you want to allow such inefficient approaches, or rule them out?
– Luis Mendo
20 hours ago
1
input numbers can be sorted in any order: does that mean that we can assume that they are sorted in a certain way, or that we must support all cases?
– Arnauld
12 hours ago
|
show 5 more comments
This is a "counterpart" of another puzzle, Eight coins for the fair king on Puzzling.SE.
You can read the above puzzle for the background. The details about this puzzle are as follows.
A set of 8 kinds of coins of different values are created, the king wants you to find out the maximum N such that any number of price from 0 to N can be paid with a combination no more than 8 coins and without charges.
For example, (taken from Glorfindel's answer). If a set of coins of values 1, 2, 5, 13, 34, 89, 233, 610 are given, your program should output 1596, because every number between 0 and 1596 (inclusive) can be represented by the sum of no more than 8 numbers from the given list (numbers may repeat), while 1597 cannot be represented in that way.
In a mathematical way, if the input is a set S consisting of 8 positive integers, the desired output N satisfies that for any number n between 0 and N, there exists x1, x2, x3, ..., x8 such that
$$x_1 + x_2 + ... + x_8 = n quadtextrm{and}quad x_1, x_2, ...,x_8 in {0} cup S$$
Your goal is to write a program, a function, or a snippet that takes 8 numbers as input, and output the maximum N as described above.
Rules:
- Flexible I/O allowed, so your program can take the input in any form that's best suitable. You may assume that the input numbers are sorted in the way that best suits your program.
- Input is a set of 8 different, positive integers (no zeros). The output is one non-negative integer.
- In case there's no 1 in the input set, your program should output 0 because any number from 0 to 0 satisfies the requirement.
- In the case of invalid input (the set contains zero or negative numbers), your program can do anything.
- Standard loopholes are forbidden.
- Your program should run within a few minutes on a modern computer.
Test cases (mostly taken from the answers under the linked question on Puzzling):
[1, 2, 3, 4, 5, 6, 7, 8] => 64
[2, 3, 4, 5, 6, 7, 8, 9] => 0
[1, 3, 4, 5, 6, 7, 8, 9] => 72
[1, 2, 5, 13, 34, 89, 233, 610] => 1596
[1, 5, 16, 51, 130, 332, 471, 1082] => 2721
[1, 6, 20, 75, 175, 474, 756, 785] => 3356
This is a code-golf, so the shortest program or snippet in each language wins!
code-golf number-theory integer
This is a "counterpart" of another puzzle, Eight coins for the fair king on Puzzling.SE.
You can read the above puzzle for the background. The details about this puzzle are as follows.
A set of 8 kinds of coins of different values are created, the king wants you to find out the maximum N such that any number of price from 0 to N can be paid with a combination no more than 8 coins and without charges.
For example, (taken from Glorfindel's answer). If a set of coins of values 1, 2, 5, 13, 34, 89, 233, 610 are given, your program should output 1596, because every number between 0 and 1596 (inclusive) can be represented by the sum of no more than 8 numbers from the given list (numbers may repeat), while 1597 cannot be represented in that way.
In a mathematical way, if the input is a set S consisting of 8 positive integers, the desired output N satisfies that for any number n between 0 and N, there exists x1, x2, x3, ..., x8 such that
$$x_1 + x_2 + ... + x_8 = n quadtextrm{and}quad x_1, x_2, ...,x_8 in {0} cup S$$
Your goal is to write a program, a function, or a snippet that takes 8 numbers as input, and output the maximum N as described above.
Rules:
- Flexible I/O allowed, so your program can take the input in any form that's best suitable. You may assume that the input numbers are sorted in the way that best suits your program.
- Input is a set of 8 different, positive integers (no zeros). The output is one non-negative integer.
- In case there's no 1 in the input set, your program should output 0 because any number from 0 to 0 satisfies the requirement.
- In the case of invalid input (the set contains zero or negative numbers), your program can do anything.
- Standard loopholes are forbidden.
- Your program should run within a few minutes on a modern computer.
Test cases (mostly taken from the answers under the linked question on Puzzling):
[1, 2, 3, 4, 5, 6, 7, 8] => 64
[2, 3, 4, 5, 6, 7, 8, 9] => 0
[1, 3, 4, 5, 6, 7, 8, 9] => 72
[1, 2, 5, 13, 34, 89, 233, 610] => 1596
[1, 5, 16, 51, 130, 332, 471, 1082] => 2721
[1, 6, 20, 75, 175, 474, 756, 785] => 3356
This is a code-golf, so the shortest program or snippet in each language wins!
code-golf number-theory integer
code-golf number-theory integer
edited 2 hours ago
asked 22 hours ago
iBug
1,242729
1,242729
Nice puzzle, but I personally think that some more test cases would be helpful in order to test our submissions.
– Mr. Xcoder
20 hours ago
Wouldn't it be better to make input size a parameter? Brute force approaches will struggle with 8
– Luis Mendo
20 hours ago
@LuisMendo Brute force approaches don't run within a reasonable amount of time. But I can't find a way to codify this as a rule.
– iBug
20 hours ago
@iBug Do you mean you want to allow such inefficient approaches, or rule them out?
– Luis Mendo
20 hours ago
1
input numbers can be sorted in any order: does that mean that we can assume that they are sorted in a certain way, or that we must support all cases?
– Arnauld
12 hours ago
|
show 5 more comments
Nice puzzle, but I personally think that some more test cases would be helpful in order to test our submissions.
– Mr. Xcoder
20 hours ago
Wouldn't it be better to make input size a parameter? Brute force approaches will struggle with 8
– Luis Mendo
20 hours ago
@LuisMendo Brute force approaches don't run within a reasonable amount of time. But I can't find a way to codify this as a rule.
– iBug
20 hours ago
@iBug Do you mean you want to allow such inefficient approaches, or rule them out?
– Luis Mendo
20 hours ago
1
input numbers can be sorted in any order: does that mean that we can assume that they are sorted in a certain way, or that we must support all cases?
– Arnauld
12 hours ago
Nice puzzle, but I personally think that some more test cases would be helpful in order to test our submissions.
– Mr. Xcoder
20 hours ago
Nice puzzle, but I personally think that some more test cases would be helpful in order to test our submissions.
– Mr. Xcoder
20 hours ago
Wouldn't it be better to make input size a parameter? Brute force approaches will struggle with 8
– Luis Mendo
20 hours ago
Wouldn't it be better to make input size a parameter? Brute force approaches will struggle with 8
– Luis Mendo
20 hours ago
@LuisMendo Brute force approaches don't run within a reasonable amount of time. But I can't find a way to codify this as a rule.
– iBug
20 hours ago
@LuisMendo Brute force approaches don't run within a reasonable amount of time. But I can't find a way to codify this as a rule.
– iBug
20 hours ago
@iBug Do you mean you want to allow such inefficient approaches, or rule them out?
– Luis Mendo
20 hours ago
@iBug Do you mean you want to allow such inefficient approaches, or rule them out?
– Luis Mendo
20 hours ago
1
1
input numbers can be sorted in any order: does that mean that we can assume that they are sorted in a certain way, or that we must support all cases?
– Arnauld
12 hours ago
input numbers can be sorted in any order: does that mean that we can assume that they are sorted in a certain way, or that we must support all cases?
– Arnauld
12 hours ago
|
show 5 more comments
9 Answers
9
active
oldest
votes
Python 3, 91 bytes
def f(x):
x=[0]+x
for i in[1]*3:x={a+b for a in x for b in x}
while{i}&x:i+=1
return~-i
Try it online!
(Thanks @Erik the Outgolfer)
New contributor
1
96 bytes.
– Erik the Outgolfer
12 hours ago
x=0,*x
saves 1 byte. Better yet,x+=0,
saves 2.
– Mr. Xcoder
8 hours ago
add a comment |
Jelly, 12 bytes
œċⱮ8Ẏ§ṢQJƑƤS
Try it online!
Takes on average ~3.7 seconds to run all test cases on TIO on my phone, so surprisingly it is quite fast.
Explanation
œċⱮ8Ẏ§ṢQJƑƤS Monadic link / Full program.
Ɱ8 Promote 8 to [1 ... 8] and for each value k:
œċ Generate all combinations of k elements from the list.
Ẏ§ Tighten, then sum. Flatten to a 2D list then sum each.
ṢQ Sort the result and remove equal entries.
JƑƤ For each prefix of this list, return 1 if it is equal to its length range, 0 otherwise.
S Finally, sum the result (counts the 1's which is equivalent to what is being asked).
add a comment |
Haskell, 56 50 bytes
g c=[x|x<-[1..],all((/=x).sum)$mapM(0:)$c<$c]!!0-1
Try it online!
A brute force approach. Add 0
to the list of coins and try all combinations of 8 picks. Find the first number n
that is not equal to the sum of any of the picks and return n-1
.
Takes about 5m30s for [1, 2, 5, 13, 34, 89, 233, 610]
on my 7 year old laptop hardware.
Edit: -6 bytes thanks to @Ørjan Johansen
An even shorter version (-2 bytes, again thanks to @Ørjan Johansen) is
Haskell, 48 bytes
g c=[x|x<-[1..],all((/=x).sum)$mapM(:0:c)c]!!0-1
but it uses significantly more memory and runs into heavy paging on my machine and does not finish "within a few minutes".
1
You can usemapM(0:)$c<$c
. (In factmapM(:0:c)c
should work, but times out on TIO for the given test case.)
– Ørjan Johansen
12 hours ago
add a comment |
Python 2, 145 bytes
from itertools import*
x=set(map(sum,reduce(chain,map(combinations_with_replacement,[input()]*9,range(9)))))
print~-min(set(range(1,max(x)+2))-x)
Try it online!
add a comment |
JavaScript (Node.js), 171 147 bytes
f=a=>a.map(n=>b.map((s,i)=>i++<8&&s.forEach(m=>b[i].add(m+n))),b=[...""+1e8].map(_=>new Set([0])))&&Math.min(...[...b=b[8]].filter(m=>!b.has(m+1)))
Try it online! I was worried that brute force would be too slow so I've gone for an efficient algorithm. The i
th set in the array b
contains the values obtained from up to i
coins.
add a comment |
JavaScript (ES6), 100 bytes
This is essentially a brute-force search, but enhanced with pruning to speed it up. The average execution time is less than 2 seconds on TIO.
Assumes that the input array is sorted from highest to lowest (+17 bytes if I didn't understand the rules correctly).
a=>[...Array(a[n=0]*9)].every(_=>(g=(s,i)=>s<0?0:i&&a.some(x=>a.includes(s)|g(s-x,i-1)))(++n,8))|n-1
Try it online!
add a comment |
Jelly, 9 bytes
Żœċ8§ḟ’$Ṃ
Try it online!
How it works
Żœċ8§ḟ’$Ṃ Main link. Argument: A (array)
Ż Prepend a 0 to A.
œċ8 Take all combinations of length 8, with repetitions.
§ Take the sum of each combination.
$ Combine the two links to the left into a monadic chain.
’ Decrement all sums.
ḟ Filterfalse; keep only sums that do not appear in the decremented sums.
Ṃ Take the minimum.
Żṗ8§ḟ’$Ṃ
saves one byte, but I'm not sure if 8.5 minutes counts as a few.
– Dennis♦
4 hours ago
add a comment |
Wolfram Language (Mathematica), 57 bytes
Tr[1^TakeWhile[CoefficientList[(1+Tr[x^#])^8,x],#>0&]]-1&
Try it online!
add a comment |
Pari/GP, 57 bytes
a->n=-1;while(polcoeff((1+sum(i=1,8,x^a[i]))^8,n++),);n-1
Try it online!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
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: "200"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fcodegolf.stackexchange.com%2fquestions%2f178015%2feight-coins-for-the-fair-king%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
Python 3, 91 bytes
def f(x):
x=[0]+x
for i in[1]*3:x={a+b for a in x for b in x}
while{i}&x:i+=1
return~-i
Try it online!
(Thanks @Erik the Outgolfer)
New contributor
1
96 bytes.
– Erik the Outgolfer
12 hours ago
x=0,*x
saves 1 byte. Better yet,x+=0,
saves 2.
– Mr. Xcoder
8 hours ago
add a comment |
Python 3, 91 bytes
def f(x):
x=[0]+x
for i in[1]*3:x={a+b for a in x for b in x}
while{i}&x:i+=1
return~-i
Try it online!
(Thanks @Erik the Outgolfer)
New contributor
1
96 bytes.
– Erik the Outgolfer
12 hours ago
x=0,*x
saves 1 byte. Better yet,x+=0,
saves 2.
– Mr. Xcoder
8 hours ago
add a comment |
Python 3, 91 bytes
def f(x):
x=[0]+x
for i in[1]*3:x={a+b for a in x for b in x}
while{i}&x:i+=1
return~-i
Try it online!
(Thanks @Erik the Outgolfer)
New contributor
Python 3, 91 bytes
def f(x):
x=[0]+x
for i in[1]*3:x={a+b for a in x for b in x}
while{i}&x:i+=1
return~-i
Try it online!
(Thanks @Erik the Outgolfer)
New contributor
edited 11 hours ago
New contributor
answered 12 hours ago
Mark
1712
1712
New contributor
New contributor
1
96 bytes.
– Erik the Outgolfer
12 hours ago
x=0,*x
saves 1 byte. Better yet,x+=0,
saves 2.
– Mr. Xcoder
8 hours ago
add a comment |
1
96 bytes.
– Erik the Outgolfer
12 hours ago
x=0,*x
saves 1 byte. Better yet,x+=0,
saves 2.
– Mr. Xcoder
8 hours ago
1
1
96 bytes.
– Erik the Outgolfer
12 hours ago
96 bytes.
– Erik the Outgolfer
12 hours ago
x=0,*x
saves 1 byte. Better yet, x+=0,
saves 2.– Mr. Xcoder
8 hours ago
x=0,*x
saves 1 byte. Better yet, x+=0,
saves 2.– Mr. Xcoder
8 hours ago
add a comment |
Jelly, 12 bytes
œċⱮ8Ẏ§ṢQJƑƤS
Try it online!
Takes on average ~3.7 seconds to run all test cases on TIO on my phone, so surprisingly it is quite fast.
Explanation
œċⱮ8Ẏ§ṢQJƑƤS Monadic link / Full program.
Ɱ8 Promote 8 to [1 ... 8] and for each value k:
œċ Generate all combinations of k elements from the list.
Ẏ§ Tighten, then sum. Flatten to a 2D list then sum each.
ṢQ Sort the result and remove equal entries.
JƑƤ For each prefix of this list, return 1 if it is equal to its length range, 0 otherwise.
S Finally, sum the result (counts the 1's which is equivalent to what is being asked).
add a comment |
Jelly, 12 bytes
œċⱮ8Ẏ§ṢQJƑƤS
Try it online!
Takes on average ~3.7 seconds to run all test cases on TIO on my phone, so surprisingly it is quite fast.
Explanation
œċⱮ8Ẏ§ṢQJƑƤS Monadic link / Full program.
Ɱ8 Promote 8 to [1 ... 8] and for each value k:
œċ Generate all combinations of k elements from the list.
Ẏ§ Tighten, then sum. Flatten to a 2D list then sum each.
ṢQ Sort the result and remove equal entries.
JƑƤ For each prefix of this list, return 1 if it is equal to its length range, 0 otherwise.
S Finally, sum the result (counts the 1's which is equivalent to what is being asked).
add a comment |
Jelly, 12 bytes
œċⱮ8Ẏ§ṢQJƑƤS
Try it online!
Takes on average ~3.7 seconds to run all test cases on TIO on my phone, so surprisingly it is quite fast.
Explanation
œċⱮ8Ẏ§ṢQJƑƤS Monadic link / Full program.
Ɱ8 Promote 8 to [1 ... 8] and for each value k:
œċ Generate all combinations of k elements from the list.
Ẏ§ Tighten, then sum. Flatten to a 2D list then sum each.
ṢQ Sort the result and remove equal entries.
JƑƤ For each prefix of this list, return 1 if it is equal to its length range, 0 otherwise.
S Finally, sum the result (counts the 1's which is equivalent to what is being asked).
Jelly, 12 bytes
œċⱮ8Ẏ§ṢQJƑƤS
Try it online!
Takes on average ~3.7 seconds to run all test cases on TIO on my phone, so surprisingly it is quite fast.
Explanation
œċⱮ8Ẏ§ṢQJƑƤS Monadic link / Full program.
Ɱ8 Promote 8 to [1 ... 8] and for each value k:
œċ Generate all combinations of k elements from the list.
Ẏ§ Tighten, then sum. Flatten to a 2D list then sum each.
ṢQ Sort the result and remove equal entries.
JƑƤ For each prefix of this list, return 1 if it is equal to its length range, 0 otherwise.
S Finally, sum the result (counts the 1's which is equivalent to what is being asked).
edited 19 hours ago
answered 20 hours ago
Mr. Xcoder
31.5k759198
31.5k759198
add a comment |
add a comment |
Haskell, 56 50 bytes
g c=[x|x<-[1..],all((/=x).sum)$mapM(0:)$c<$c]!!0-1
Try it online!
A brute force approach. Add 0
to the list of coins and try all combinations of 8 picks. Find the first number n
that is not equal to the sum of any of the picks and return n-1
.
Takes about 5m30s for [1, 2, 5, 13, 34, 89, 233, 610]
on my 7 year old laptop hardware.
Edit: -6 bytes thanks to @Ørjan Johansen
An even shorter version (-2 bytes, again thanks to @Ørjan Johansen) is
Haskell, 48 bytes
g c=[x|x<-[1..],all((/=x).sum)$mapM(:0:c)c]!!0-1
but it uses significantly more memory and runs into heavy paging on my machine and does not finish "within a few minutes".
1
You can usemapM(0:)$c<$c
. (In factmapM(:0:c)c
should work, but times out on TIO for the given test case.)
– Ørjan Johansen
12 hours ago
add a comment |
Haskell, 56 50 bytes
g c=[x|x<-[1..],all((/=x).sum)$mapM(0:)$c<$c]!!0-1
Try it online!
A brute force approach. Add 0
to the list of coins and try all combinations of 8 picks. Find the first number n
that is not equal to the sum of any of the picks and return n-1
.
Takes about 5m30s for [1, 2, 5, 13, 34, 89, 233, 610]
on my 7 year old laptop hardware.
Edit: -6 bytes thanks to @Ørjan Johansen
An even shorter version (-2 bytes, again thanks to @Ørjan Johansen) is
Haskell, 48 bytes
g c=[x|x<-[1..],all((/=x).sum)$mapM(:0:c)c]!!0-1
but it uses significantly more memory and runs into heavy paging on my machine and does not finish "within a few minutes".
1
You can usemapM(0:)$c<$c
. (In factmapM(:0:c)c
should work, but times out on TIO for the given test case.)
– Ørjan Johansen
12 hours ago
add a comment |
Haskell, 56 50 bytes
g c=[x|x<-[1..],all((/=x).sum)$mapM(0:)$c<$c]!!0-1
Try it online!
A brute force approach. Add 0
to the list of coins and try all combinations of 8 picks. Find the first number n
that is not equal to the sum of any of the picks and return n-1
.
Takes about 5m30s for [1, 2, 5, 13, 34, 89, 233, 610]
on my 7 year old laptop hardware.
Edit: -6 bytes thanks to @Ørjan Johansen
An even shorter version (-2 bytes, again thanks to @Ørjan Johansen) is
Haskell, 48 bytes
g c=[x|x<-[1..],all((/=x).sum)$mapM(:0:c)c]!!0-1
but it uses significantly more memory and runs into heavy paging on my machine and does not finish "within a few minutes".
Haskell, 56 50 bytes
g c=[x|x<-[1..],all((/=x).sum)$mapM(0:)$c<$c]!!0-1
Try it online!
A brute force approach. Add 0
to the list of coins and try all combinations of 8 picks. Find the first number n
that is not equal to the sum of any of the picks and return n-1
.
Takes about 5m30s for [1, 2, 5, 13, 34, 89, 233, 610]
on my 7 year old laptop hardware.
Edit: -6 bytes thanks to @Ørjan Johansen
An even shorter version (-2 bytes, again thanks to @Ørjan Johansen) is
Haskell, 48 bytes
g c=[x|x<-[1..],all((/=x).sum)$mapM(:0:c)c]!!0-1
but it uses significantly more memory and runs into heavy paging on my machine and does not finish "within a few minutes".
edited 11 hours ago
answered 20 hours ago
nimi
31.3k32085
31.3k32085
1
You can usemapM(0:)$c<$c
. (In factmapM(:0:c)c
should work, but times out on TIO for the given test case.)
– Ørjan Johansen
12 hours ago
add a comment |
1
You can usemapM(0:)$c<$c
. (In factmapM(:0:c)c
should work, but times out on TIO for the given test case.)
– Ørjan Johansen
12 hours ago
1
1
You can use
mapM(0:)$c<$c
. (In fact mapM(:0:c)c
should work, but times out on TIO for the given test case.)– Ørjan Johansen
12 hours ago
You can use
mapM(0:)$c<$c
. (In fact mapM(:0:c)c
should work, but times out on TIO for the given test case.)– Ørjan Johansen
12 hours ago
add a comment |
Python 2, 145 bytes
from itertools import*
x=set(map(sum,reduce(chain,map(combinations_with_replacement,[input()]*9,range(9)))))
print~-min(set(range(1,max(x)+2))-x)
Try it online!
add a comment |
Python 2, 145 bytes
from itertools import*
x=set(map(sum,reduce(chain,map(combinations_with_replacement,[input()]*9,range(9)))))
print~-min(set(range(1,max(x)+2))-x)
Try it online!
add a comment |
Python 2, 145 bytes
from itertools import*
x=set(map(sum,reduce(chain,map(combinations_with_replacement,[input()]*9,range(9)))))
print~-min(set(range(1,max(x)+2))-x)
Try it online!
Python 2, 145 bytes
from itertools import*
x=set(map(sum,reduce(chain,map(combinations_with_replacement,[input()]*9,range(9)))))
print~-min(set(range(1,max(x)+2))-x)
Try it online!
answered 18 hours ago
Erik the Outgolfer
31.3k429102
31.3k429102
add a comment |
add a comment |
JavaScript (Node.js), 171 147 bytes
f=a=>a.map(n=>b.map((s,i)=>i++<8&&s.forEach(m=>b[i].add(m+n))),b=[...""+1e8].map(_=>new Set([0])))&&Math.min(...[...b=b[8]].filter(m=>!b.has(m+1)))
Try it online! I was worried that brute force would be too slow so I've gone for an efficient algorithm. The i
th set in the array b
contains the values obtained from up to i
coins.
add a comment |
JavaScript (Node.js), 171 147 bytes
f=a=>a.map(n=>b.map((s,i)=>i++<8&&s.forEach(m=>b[i].add(m+n))),b=[...""+1e8].map(_=>new Set([0])))&&Math.min(...[...b=b[8]].filter(m=>!b.has(m+1)))
Try it online! I was worried that brute force would be too slow so I've gone for an efficient algorithm. The i
th set in the array b
contains the values obtained from up to i
coins.
add a comment |
JavaScript (Node.js), 171 147 bytes
f=a=>a.map(n=>b.map((s,i)=>i++<8&&s.forEach(m=>b[i].add(m+n))),b=[...""+1e8].map(_=>new Set([0])))&&Math.min(...[...b=b[8]].filter(m=>!b.has(m+1)))
Try it online! I was worried that brute force would be too slow so I've gone for an efficient algorithm. The i
th set in the array b
contains the values obtained from up to i
coins.
JavaScript (Node.js), 171 147 bytes
f=a=>a.map(n=>b.map((s,i)=>i++<8&&s.forEach(m=>b[i].add(m+n))),b=[...""+1e8].map(_=>new Set([0])))&&Math.min(...[...b=b[8]].filter(m=>!b.has(m+1)))
Try it online! I was worried that brute force would be too slow so I've gone for an efficient algorithm. The i
th set in the array b
contains the values obtained from up to i
coins.
edited 15 hours ago
answered 17 hours ago
Neil
79.3k744177
79.3k744177
add a comment |
add a comment |
JavaScript (ES6), 100 bytes
This is essentially a brute-force search, but enhanced with pruning to speed it up. The average execution time is less than 2 seconds on TIO.
Assumes that the input array is sorted from highest to lowest (+17 bytes if I didn't understand the rules correctly).
a=>[...Array(a[n=0]*9)].every(_=>(g=(s,i)=>s<0?0:i&&a.some(x=>a.includes(s)|g(s-x,i-1)))(++n,8))|n-1
Try it online!
add a comment |
JavaScript (ES6), 100 bytes
This is essentially a brute-force search, but enhanced with pruning to speed it up. The average execution time is less than 2 seconds on TIO.
Assumes that the input array is sorted from highest to lowest (+17 bytes if I didn't understand the rules correctly).
a=>[...Array(a[n=0]*9)].every(_=>(g=(s,i)=>s<0?0:i&&a.some(x=>a.includes(s)|g(s-x,i-1)))(++n,8))|n-1
Try it online!
add a comment |
JavaScript (ES6), 100 bytes
This is essentially a brute-force search, but enhanced with pruning to speed it up. The average execution time is less than 2 seconds on TIO.
Assumes that the input array is sorted from highest to lowest (+17 bytes if I didn't understand the rules correctly).
a=>[...Array(a[n=0]*9)].every(_=>(g=(s,i)=>s<0?0:i&&a.some(x=>a.includes(s)|g(s-x,i-1)))(++n,8))|n-1
Try it online!
JavaScript (ES6), 100 bytes
This is essentially a brute-force search, but enhanced with pruning to speed it up. The average execution time is less than 2 seconds on TIO.
Assumes that the input array is sorted from highest to lowest (+17 bytes if I didn't understand the rules correctly).
a=>[...Array(a[n=0]*9)].every(_=>(g=(s,i)=>s<0?0:i&&a.some(x=>a.includes(s)|g(s-x,i-1)))(++n,8))|n-1
Try it online!
answered 11 hours ago
Arnauld
72.3k689303
72.3k689303
add a comment |
add a comment |
Jelly, 9 bytes
Żœċ8§ḟ’$Ṃ
Try it online!
How it works
Żœċ8§ḟ’$Ṃ Main link. Argument: A (array)
Ż Prepend a 0 to A.
œċ8 Take all combinations of length 8, with repetitions.
§ Take the sum of each combination.
$ Combine the two links to the left into a monadic chain.
’ Decrement all sums.
ḟ Filterfalse; keep only sums that do not appear in the decremented sums.
Ṃ Take the minimum.
Żṗ8§ḟ’$Ṃ
saves one byte, but I'm not sure if 8.5 minutes counts as a few.
– Dennis♦
4 hours ago
add a comment |
Jelly, 9 bytes
Żœċ8§ḟ’$Ṃ
Try it online!
How it works
Żœċ8§ḟ’$Ṃ Main link. Argument: A (array)
Ż Prepend a 0 to A.
œċ8 Take all combinations of length 8, with repetitions.
§ Take the sum of each combination.
$ Combine the two links to the left into a monadic chain.
’ Decrement all sums.
ḟ Filterfalse; keep only sums that do not appear in the decremented sums.
Ṃ Take the minimum.
Żṗ8§ḟ’$Ṃ
saves one byte, but I'm not sure if 8.5 minutes counts as a few.
– Dennis♦
4 hours ago
add a comment |
Jelly, 9 bytes
Żœċ8§ḟ’$Ṃ
Try it online!
How it works
Żœċ8§ḟ’$Ṃ Main link. Argument: A (array)
Ż Prepend a 0 to A.
œċ8 Take all combinations of length 8, with repetitions.
§ Take the sum of each combination.
$ Combine the two links to the left into a monadic chain.
’ Decrement all sums.
ḟ Filterfalse; keep only sums that do not appear in the decremented sums.
Ṃ Take the minimum.
Jelly, 9 bytes
Żœċ8§ḟ’$Ṃ
Try it online!
How it works
Żœċ8§ḟ’$Ṃ Main link. Argument: A (array)
Ż Prepend a 0 to A.
œċ8 Take all combinations of length 8, with repetitions.
§ Take the sum of each combination.
$ Combine the two links to the left into a monadic chain.
’ Decrement all sums.
ḟ Filterfalse; keep only sums that do not appear in the decremented sums.
Ṃ Take the minimum.
answered 5 hours ago
Dennis♦
186k32295735
186k32295735
Żṗ8§ḟ’$Ṃ
saves one byte, but I'm not sure if 8.5 minutes counts as a few.
– Dennis♦
4 hours ago
add a comment |
Żṗ8§ḟ’$Ṃ
saves one byte, but I'm not sure if 8.5 minutes counts as a few.
– Dennis♦
4 hours ago
Żṗ8§ḟ’$Ṃ
saves one byte, but I'm not sure if 8.5 minutes counts as a few.– Dennis♦
4 hours ago
Żṗ8§ḟ’$Ṃ
saves one byte, but I'm not sure if 8.5 minutes counts as a few.– Dennis♦
4 hours ago
add a comment |
Wolfram Language (Mathematica), 57 bytes
Tr[1^TakeWhile[CoefficientList[(1+Tr[x^#])^8,x],#>0&]]-1&
Try it online!
add a comment |
Wolfram Language (Mathematica), 57 bytes
Tr[1^TakeWhile[CoefficientList[(1+Tr[x^#])^8,x],#>0&]]-1&
Try it online!
add a comment |
Wolfram Language (Mathematica), 57 bytes
Tr[1^TakeWhile[CoefficientList[(1+Tr[x^#])^8,x],#>0&]]-1&
Try it online!
Wolfram Language (Mathematica), 57 bytes
Tr[1^TakeWhile[CoefficientList[(1+Tr[x^#])^8,x],#>0&]]-1&
Try it online!
answered 2 hours ago
alephalpha
21.1k32989
21.1k32989
add a comment |
add a comment |
Pari/GP, 57 bytes
a->n=-1;while(polcoeff((1+sum(i=1,8,x^a[i]))^8,n++),);n-1
Try it online!
add a comment |
Pari/GP, 57 bytes
a->n=-1;while(polcoeff((1+sum(i=1,8,x^a[i]))^8,n++),);n-1
Try it online!
add a comment |
Pari/GP, 57 bytes
a->n=-1;while(polcoeff((1+sum(i=1,8,x^a[i]))^8,n++),);n-1
Try it online!
Pari/GP, 57 bytes
a->n=-1;while(polcoeff((1+sum(i=1,8,x^a[i]))^8,n++),);n-1
Try it online!
answered 2 hours ago
alephalpha
21.1k32989
21.1k32989
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fcodegolf.stackexchange.com%2fquestions%2f178015%2feight-coins-for-the-fair-king%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
Nice puzzle, but I personally think that some more test cases would be helpful in order to test our submissions.
– Mr. Xcoder
20 hours ago
Wouldn't it be better to make input size a parameter? Brute force approaches will struggle with 8
– Luis Mendo
20 hours ago
@LuisMendo Brute force approaches don't run within a reasonable amount of time. But I can't find a way to codify this as a rule.
– iBug
20 hours ago
@iBug Do you mean you want to allow such inefficient approaches, or rule them out?
– Luis Mendo
20 hours ago
1
input numbers can be sorted in any order: does that mean that we can assume that they are sorted in a certain way, or that we must support all cases?
– Arnauld
12 hours ago