Cut the gold chainSave my secrets!Markov Chain Beatbox GeneratorNumbers on a ChainBuilding a long chain of wordsBuild a poisoned wine testing schedulerSprocket Science: Animating a Chain Drive SystemPlay the word chain“Cut and Paste Sorting”Markov Chain QuineLongest domino chainWhich wire to cut
Do I have to roll to maintain concentration if a target other than me who is affected by my concentration spell takes damage?
Cross over of arrows in a complex diagram
Bash echo $-1 prints hb1. Why?
Disabling automatic add after resolving git conflict
How can I create ribbons like these in Microsoft word 2010?
Why is Madam Hooch not a professor?
Confusion about multiple information Sets
The difference between Rad1 and Rfd1
Why isn’t the tax system continuous rather than bracketed?
Are there any vegetarian astronauts?
What happens when your group is victim of a surprise attack but you can't be surprised?
How exactly is a normal force exerted, at the molecular level?
Do 3D printers really reach 50 micron (0.050mm) accuracy?
A player is constantly pestering me about rules, what do I do as a DM?
Is there any set of 2-6 notes that doesn't have a chord name?
Row to remove the dotted white border around focused button text
What are good ways to spray paint a QR code on a footpath?
Why is the divergence of this series apparently not predicted by the Monotonic Sequence Theorem?
Why does the numerical solution of an ODE move away from an unstable equilibrium?
“Transitive verb” + interrupter+ “object”?
Should I hide continue button until tasks are completed?
How come I was asked by a CBP officer why I was in the US when leaving?
Signing using digital signatures?
SPI Waveform on Raspberry Pi Not clean and I'm wondering why
Cut the gold chain
Save my secrets!Markov Chain Beatbox GeneratorNumbers on a ChainBuilding a long chain of wordsBuild a poisoned wine testing schedulerSprocket Science: Animating a Chain Drive SystemPlay the word chain“Cut and Paste Sorting”Markov Chain QuineLongest domino chainWhich wire to cut
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
A traveler needs to stay for n days in a hotel outside town. He is out of cash and his credit card is expired. But he has a gold chain with n links.
The rule in this hotel is that residents should pay their rent every morning. The traveler comes to an agreement with the manager to pay one link of the golden chain for each day. But the manager also demands that the traveler should make the least possible damage to the chain while paying every day. In other words, he has to come up with a solution to cut as few links as possible.
Cutting a link creates three subchains: one containing only the cut link, and one on each side. For example, cutting the third link of a chain of length 8 creates subchains of length [2, 1, 5]. The manager is happy to make change, so the traveller can pay the first day with the chain of length 1, then the second day with the chain of length 2, getting the first chain back.
Your code should input the length n, and output a list of links to cut of minimum length.
Rules:
n is an integer > 0.- You can use either 0-based or 1-based indexing for the links.
- For some numbers, the solution is not unique. For example, if
n = 15
both[3, 8]
and[4, 8]
are valid outputs. - You can either return the list, or print it with any reasonable separator.
- This is code-golf, so the shortest code in bytes wins.
Test cases:
Input Output (1-indexed)
1 []
3 [1]
7 [3]
15 [3, 8]
149 [6, 17, 38, 79]
Detailed example
For n = 15, cutting the links 3 and 8 results in subchains of length [2, 1, 4, 1, 7]
. This is a valid solution because:
1 = 1
2 = 2
3 = 1+2
4 = 4
5 = 1+4
6 = 2+4
7 = 7
8 = 1+7
9 = 2+7
10 = 1+2+7
11 = 4+7
12 = 1+4+7
13 = 2+4+7
14 = 1+2+4+7
15 = 1+1+2+4+7
No solution with only one cut exists, so this is an optimal solution.
Addendum
Note that this problem is related to integer partitioning. We're looking for a partition P of n such that all integers from 1 to n have at least one patition that is a subset of P.
Here's a YouTube video about one possible algorithm for this problem.
code-golf code-challenge puzzle-solver
$endgroup$
add a comment |
$begingroup$
A traveler needs to stay for n days in a hotel outside town. He is out of cash and his credit card is expired. But he has a gold chain with n links.
The rule in this hotel is that residents should pay their rent every morning. The traveler comes to an agreement with the manager to pay one link of the golden chain for each day. But the manager also demands that the traveler should make the least possible damage to the chain while paying every day. In other words, he has to come up with a solution to cut as few links as possible.
Cutting a link creates three subchains: one containing only the cut link, and one on each side. For example, cutting the third link of a chain of length 8 creates subchains of length [2, 1, 5]. The manager is happy to make change, so the traveller can pay the first day with the chain of length 1, then the second day with the chain of length 2, getting the first chain back.
Your code should input the length n, and output a list of links to cut of minimum length.
Rules:
n is an integer > 0.- You can use either 0-based or 1-based indexing for the links.
- For some numbers, the solution is not unique. For example, if
n = 15
both[3, 8]
and[4, 8]
are valid outputs. - You can either return the list, or print it with any reasonable separator.
- This is code-golf, so the shortest code in bytes wins.
Test cases:
Input Output (1-indexed)
1 []
3 [1]
7 [3]
15 [3, 8]
149 [6, 17, 38, 79]
Detailed example
For n = 15, cutting the links 3 and 8 results in subchains of length [2, 1, 4, 1, 7]
. This is a valid solution because:
1 = 1
2 = 2
3 = 1+2
4 = 4
5 = 1+4
6 = 2+4
7 = 7
8 = 1+7
9 = 2+7
10 = 1+2+7
11 = 4+7
12 = 1+4+7
13 = 2+4+7
14 = 1+2+4+7
15 = 1+1+2+4+7
No solution with only one cut exists, so this is an optimal solution.
Addendum
Note that this problem is related to integer partitioning. We're looking for a partition P of n such that all integers from 1 to n have at least one patition that is a subset of P.
Here's a YouTube video about one possible algorithm for this problem.
code-golf code-challenge puzzle-solver
$endgroup$
$begingroup$
I don't understand your "making change" reference. In your posted example, on the second day you pay with the 2-link chain (and get the 1-link-chain (which you paid with the day before) back as change, as per your explanation). But on the third day, you pay with1+2
. Where did the second 2-link-chain come from?
$endgroup$
– Flater
Jun 18 at 11:04
4
$begingroup$
@Flater The manager already has it. We just pay the additional one. In fact, the RHS are the links that manager owns each day
$endgroup$
– polfosol ఠ_ఠ
Jun 18 at 11:17
add a comment |
$begingroup$
A traveler needs to stay for n days in a hotel outside town. He is out of cash and his credit card is expired. But he has a gold chain with n links.
The rule in this hotel is that residents should pay their rent every morning. The traveler comes to an agreement with the manager to pay one link of the golden chain for each day. But the manager also demands that the traveler should make the least possible damage to the chain while paying every day. In other words, he has to come up with a solution to cut as few links as possible.
Cutting a link creates three subchains: one containing only the cut link, and one on each side. For example, cutting the third link of a chain of length 8 creates subchains of length [2, 1, 5]. The manager is happy to make change, so the traveller can pay the first day with the chain of length 1, then the second day with the chain of length 2, getting the first chain back.
Your code should input the length n, and output a list of links to cut of minimum length.
Rules:
n is an integer > 0.- You can use either 0-based or 1-based indexing for the links.
- For some numbers, the solution is not unique. For example, if
n = 15
both[3, 8]
and[4, 8]
are valid outputs. - You can either return the list, or print it with any reasonable separator.
- This is code-golf, so the shortest code in bytes wins.
Test cases:
Input Output (1-indexed)
1 []
3 [1]
7 [3]
15 [3, 8]
149 [6, 17, 38, 79]
Detailed example
For n = 15, cutting the links 3 and 8 results in subchains of length [2, 1, 4, 1, 7]
. This is a valid solution because:
1 = 1
2 = 2
3 = 1+2
4 = 4
5 = 1+4
6 = 2+4
7 = 7
8 = 1+7
9 = 2+7
10 = 1+2+7
11 = 4+7
12 = 1+4+7
13 = 2+4+7
14 = 1+2+4+7
15 = 1+1+2+4+7
No solution with only one cut exists, so this is an optimal solution.
Addendum
Note that this problem is related to integer partitioning. We're looking for a partition P of n such that all integers from 1 to n have at least one patition that is a subset of P.
Here's a YouTube video about one possible algorithm for this problem.
code-golf code-challenge puzzle-solver
$endgroup$
A traveler needs to stay for n days in a hotel outside town. He is out of cash and his credit card is expired. But he has a gold chain with n links.
The rule in this hotel is that residents should pay their rent every morning. The traveler comes to an agreement with the manager to pay one link of the golden chain for each day. But the manager also demands that the traveler should make the least possible damage to the chain while paying every day. In other words, he has to come up with a solution to cut as few links as possible.
Cutting a link creates three subchains: one containing only the cut link, and one on each side. For example, cutting the third link of a chain of length 8 creates subchains of length [2, 1, 5]. The manager is happy to make change, so the traveller can pay the first day with the chain of length 1, then the second day with the chain of length 2, getting the first chain back.
Your code should input the length n, and output a list of links to cut of minimum length.
Rules:
n is an integer > 0.- You can use either 0-based or 1-based indexing for the links.
- For some numbers, the solution is not unique. For example, if
n = 15
both[3, 8]
and[4, 8]
are valid outputs. - You can either return the list, or print it with any reasonable separator.
- This is code-golf, so the shortest code in bytes wins.
Test cases:
Input Output (1-indexed)
1 []
3 [1]
7 [3]
15 [3, 8]
149 [6, 17, 38, 79]
Detailed example
For n = 15, cutting the links 3 and 8 results in subchains of length [2, 1, 4, 1, 7]
. This is a valid solution because:
1 = 1
2 = 2
3 = 1+2
4 = 4
5 = 1+4
6 = 2+4
7 = 7
8 = 1+7
9 = 2+7
10 = 1+2+7
11 = 4+7
12 = 1+4+7
13 = 2+4+7
14 = 1+2+4+7
15 = 1+1+2+4+7
No solution with only one cut exists, so this is an optimal solution.
Addendum
Note that this problem is related to integer partitioning. We're looking for a partition P of n such that all integers from 1 to n have at least one patition that is a subset of P.
Here's a YouTube video about one possible algorithm for this problem.
code-golf code-challenge puzzle-solver
code-golf code-challenge puzzle-solver
edited Jun 17 at 13:54
Grimy
4,55112 silver badges26 bronze badges
4,55112 silver badges26 bronze badges
asked Jun 17 at 10:24
polfosol ఠ_ఠpolfosol ఠ_ఠ
3292 silver badges8 bronze badges
3292 silver badges8 bronze badges
$begingroup$
I don't understand your "making change" reference. In your posted example, on the second day you pay with the 2-link chain (and get the 1-link-chain (which you paid with the day before) back as change, as per your explanation). But on the third day, you pay with1+2
. Where did the second 2-link-chain come from?
$endgroup$
– Flater
Jun 18 at 11:04
4
$begingroup$
@Flater The manager already has it. We just pay the additional one. In fact, the RHS are the links that manager owns each day
$endgroup$
– polfosol ఠ_ఠ
Jun 18 at 11:17
add a comment |
$begingroup$
I don't understand your "making change" reference. In your posted example, on the second day you pay with the 2-link chain (and get the 1-link-chain (which you paid with the day before) back as change, as per your explanation). But on the third day, you pay with1+2
. Where did the second 2-link-chain come from?
$endgroup$
– Flater
Jun 18 at 11:04
4
$begingroup$
@Flater The manager already has it. We just pay the additional one. In fact, the RHS are the links that manager owns each day
$endgroup$
– polfosol ఠ_ఠ
Jun 18 at 11:17
$begingroup$
I don't understand your "making change" reference. In your posted example, on the second day you pay with the 2-link chain (and get the 1-link-chain (which you paid with the day before) back as change, as per your explanation). But on the third day, you pay with
1+2
. Where did the second 2-link-chain come from?$endgroup$
– Flater
Jun 18 at 11:04
$begingroup$
I don't understand your "making change" reference. In your posted example, on the second day you pay with the 2-link chain (and get the 1-link-chain (which you paid with the day before) back as change, as per your explanation). But on the third day, you pay with
1+2
. Where did the second 2-link-chain come from?$endgroup$
– Flater
Jun 18 at 11:04
4
4
$begingroup$
@Flater The manager already has it. We just pay the additional one. In fact, the RHS are the links that manager owns each day
$endgroup$
– polfosol ఠ_ఠ
Jun 18 at 11:17
$begingroup$
@Flater The manager already has it. We just pay the additional one. In fact, the RHS are the links that manager owns each day
$endgroup$
– polfosol ఠ_ఠ
Jun 18 at 11:17
add a comment |
8 Answers
8
active
oldest
votes
$begingroup$
05AB1E, 23 11 8 bytes
ΔÍN-;иg=
Try it online!
Uses 0-based indexing.
Explanation:
# start from the implicit input
Δ # loop forever
Í # subtract 2
N- # subtract the current iteration number
; # divide by 2
и # create a list of length x
g # get the length of the list
= # print
иg
looks like a noop, but it actually does two useful things: it truncates to an integer (;
returns a float), and it crashes the interpreter if x is negative (this is the only exit condition).
The 23 byte solution used a very different approach, so here it is for posterity: ÅœʒR2äθP}ʒæOê¥P}θ2äθη€O
(TIO, explanation).
$endgroup$
2
$begingroup$
I've deleted my answer. Mine being 42 and yours being 11 is too big of a difference for me to not feel embarrassed, haha. ;) Nice answer though, and lol at theØ.Ø
. Did you just try some random things in order to both floor and map all negatives to-1
? Regardless, very nice answer and much shorter than I anticipated. I was thinking around 20 bytes after i posted my bad 42-byter.
$endgroup$
– Kevin Cruijssen
Jun 17 at 16:37
2
$begingroup$
@KevinCruijssen Nnope,Ø.Ø
was actually my first idea. Your comment inspired me to try random things: I found®Ÿà
,ï®M
, and more importantly,иg
, which yields this nice 8-byter. I always found it annoying that osabie prefers doing nothing over crashing in many cases (div by 0, wrong type, etc), so this crash will come in handy.
$endgroup$
– Grimy
Jun 17 at 17:19
2
$begingroup$
Hehe, 05AB1E is supposed to never crash, but you're right that it sometimes is a bit annoying it never does.. In the legacy I wouldn't even know how to crash, and in the past we even had an explicit division by zero error we could call manually. xD In the new version it still crashes with an error pretty often when giving incorrect arguments to certain builtins. And nice -3 again.
$endgroup$
– Kevin Cruijssen
Jun 18 at 7:48
2
$begingroup$
"crashes the interpreter if x is negative (this is the only exit condition)." - I love that
$endgroup$
– John Dvorak
Jun 18 at 10:41
add a comment |
$begingroup$
Python 2, 75 bytes
f=lambda n,i=0:n>=i<<i and f(n,i+1)or[min(n,2**j*i-i+j)for j in range(1,i)]
Try it online!
Explanation:
Builds a sequence of 'binary' chunks, with a base number matching the number of cuts.
Eg:
63
can be done in 3 cuts, which means a partition in base-4 (as we have 3 single rings):
Cuts: 5, 14, 31
, which gives chains of 4 1 8 1 16 1 32
(sorted: 1 1 1 4 8 16 32
).
All numbers can be made:
1 1
2 1 1
3 1 1 1
4 4
...
42 1 1 8 32
...
62 1 1 4 8 16 32
63 1 1 1 4 8 16 32
Other examples:
18: 4,11 -> 3 1 6 1 7
27: 5,14,27 -> 4 1 8 1 13 1
36: 5,14,31 -> 4 1 8 1 16 1 5
86: 6,17,38,79 -> 5 1 10 1 20 1 40 1 7
$endgroup$
1
$begingroup$
Shouldn't you addf=
to the start? Since you use a call tof
in the lambda function, and I can only assume that you refer to the same lambda you're defining.
$endgroup$
– randomdude999
Jun 17 at 12:03
$begingroup$
@randomdude999, Yeah, I forgot...
$endgroup$
– TFeld
Jun 17 at 12:06
$begingroup$
@randomdude999 does that rule apply to all languages, or just python? Cause I see a javascript answer that is a pure lambda in this challenge...
$endgroup$
– Shadow
Jun 18 at 1:54
3
$begingroup$
@Shadow It applies to all languages, but only for recursive lambdas.
$endgroup$
– TFeld
Jun 18 at 5:10
1
$begingroup$
@Shadow A more generic rule is that you can't reference something that is neither defined in your code nor globally defined in your language, unless it is explicitly allowed by the challenge. The most common case is a recursive function, but this applies to other situations. This answer is another example:f
is not recursive, but it's however referenced in the code and therefore has to be named.
$endgroup$
– Arnauld
Jun 18 at 6:41
add a comment |
$begingroup$
R, 77 69 bytes
-8 bytes thanks to Aaron Hayman
pmin(n<-scan(),0:(k=sum((a=2:n)*2^a<=n))+cumsum((k+2)*2^(0:k))+1)[-n]
Try it online!
Let $k$ be the number of cuts needed; $k$ is the smallest integer such that $(k+1)cdot2^kgeq n$. Indeed, a possible solution is then to have subchains of lengths $1,1,ldots,1$ ($k$ times) and $(k+1), 2(k+1), 4(k+1), 8(k+1), ldots, (k+1)cdot 2^k-1$. It is easy to check that this is sufficient and optimal.
(The last subchain might need to be made shorter if we exceed the total length of the chain.)
Ungolfed (based on previous, similar version):
n = scan() # read input
if(n - 1) # If n==1, return NULL
k = match(F, (a = 2:n) * 2 ^ a > n) # compute k
b = (k + 1) * 2 ^ (1:k - 1) # lengths of subchains
c = 1:k + cumsum(b) # positions of cuts
pmin(c, n ) # if last value is >n, coerce it to n
(Proof that the value of $k$ is as I state: suppose we have $k$ cuts. We then have $k$ unit subchains, so we need the first subchain to be of length $k+1$. We can now handle all lengths up to $2k+1$, so we need the next one to be of length $2k+2$, then $4k+4$... Thus the maximum we can get out of $k$ cuts is obtained by summing all those lengths, which gives $(k+1)cdot 2^k-1$.)
If $a(k)$ is the smallest integer $n$ requiring $k$ cuts, then $a(k)$ is OEIS A134401.
$endgroup$
$begingroup$
I doubt it would help with the special case forn=1
, but an alternative way to generate the cutoffs is the recurrence1, 4, 4a(n-1)-4a(n-2)
.
$endgroup$
– Peter Taylor
Jun 17 at 22:28
$begingroup$
@PeterTaylor I had a similar recurrence for computingk
; this corresponds to OEIS A134401: oeis.org/A134401 But my implementation of the recurrence relation takes up more bytes than the current code.
$endgroup$
– Robin Ryder
Jun 18 at 5:33
$begingroup$
A bit of rearrangement I got it down to 73. Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:06
$begingroup$
@AaronHayman Thanks! Smart move usingsum
instead ofmatch
.
$endgroup$
– Robin Ryder
Jun 18 at 9:41
$begingroup$
69 bytes and got rid of that if statement that was upsetting you: Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:56
|
show 1 more comment
$begingroup$
JavaScript (ES6), 66 bytes
This is a port of TFeld's answer.
n=>(g=a=>n<++i<<i?a.map(j=>(j+=(i<<j)-i)>n?n:j):g([...a,i]))(i=[])
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 12 bytes
’_‘ɼ:2»-µƬṖḊ
Try it online!
Translation of Grimy's 05AB1E answer so be sure to upvote that one too! Slightly disappointed this comes in a byte longer, but does at least have something a bit like an emoticon as the first three bytes!
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 61 bytes
.+
11,$&$*
+`b(1+),(1(1*)1?3)$
1$2¶1$1,$3
1+,
1
1A`
1+
$.&
Try it online! 1-indexed port of @Grimy's answer. Explanation:
.+
11,$&$*
Start with N=2
and the input converted to unary.
+`b(1+),(1(1*)1?3)$
Repeatedly try to subtract N
from the input and then divide by 2.
1$2¶1$1,$3
If successful, remember 1 more than the input on the previous line, increment N
on the current line, and update the input to the new value.
1+,
1
Remove N
and increment the last value so that it's also 1-indexed.
1A`
Remove the incremented original input.
1+
$.&
Convert the results to decimal.
$endgroup$
add a comment |
$begingroup$
C++, 109,107 bytes
-2 bytes thanks to Kevin
#include<iostream>
main()int n,k=0;for(std::cin>>n;++k<<k<n;);for(n-=k;n>0;k*=2,n-=k+1)std::cout<<n<<',';
The algorithm is similar to the Robin Ryder's answer. The code is written in a compilable, whole form. Try it!
Details:
std::cin>>n; // get the value of n as input
while(++k<<k<n); // determine k
for(n-=k;n>0;k*=2,n-=k+1) // we don't need n, so the lengths...
std::cout<<n<<' '; // of links are subtracted repeatedly
This has a C variation with the same byte length (doesn't seem to need a separate answer):
#include<stdio.h>
main()int n,k=0;for(scanf("%d",&n);++k<<k<n;);for(n-=k;n>0;k*=2,n-=k+1)printf("%d,",n);
$endgroup$
$begingroup$
Two minor things to golf:=0
afterk
can be removed, since its0
by default.std::cin>>n;while(++k<<k<n);
can befor(std::cin>>n;++k<<k<n;);
. I also have the feelingfor(n-=k;n>0;k*=2,n-=k+1)
can be simplified somehow by combining stuff, but not sure how. PS: Changing the comma-delimiter to a space looks slightly better since you don't see the trailing one imo, but this is purely cosmetic :)
$endgroup$
– Kevin Cruijssen
Jun 19 at 10:58
1
$begingroup$
@KevinCruijssen Thanks, but some compilers don't assign a default value to non-static variables. So I thought=0
was necessary for portability ;) I also realized that the space after#include
is not necessary.
$endgroup$
– polfosol ఠ_ఠ
Jun 19 at 11:02
$begingroup$
Ah ok. I don't know C++ too well, so I've used that online compiler you linked in your answer to test some things. :) You forgot the second change I proposed in my comment: the while-loop to a for-loop and putting thestd::cin>>n
inside it.
$endgroup$
– Kevin Cruijssen
Jun 19 at 11:08
add a comment |
$begingroup$
Ruby, 62 bytes
->n(1...c=(0..n).find).map[n,b-c+(c<<b)].min
Try it online!
Mostly stolen from TFeld's Python answer.
$endgroup$
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: "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%2f186983%2fcut-the-gold-chain%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
05AB1E, 23 11 8 bytes
ΔÍN-;иg=
Try it online!
Uses 0-based indexing.
Explanation:
# start from the implicit input
Δ # loop forever
Í # subtract 2
N- # subtract the current iteration number
; # divide by 2
и # create a list of length x
g # get the length of the list
= # print
иg
looks like a noop, but it actually does two useful things: it truncates to an integer (;
returns a float), and it crashes the interpreter if x is negative (this is the only exit condition).
The 23 byte solution used a very different approach, so here it is for posterity: ÅœʒR2äθP}ʒæOê¥P}θ2äθη€O
(TIO, explanation).
$endgroup$
2
$begingroup$
I've deleted my answer. Mine being 42 and yours being 11 is too big of a difference for me to not feel embarrassed, haha. ;) Nice answer though, and lol at theØ.Ø
. Did you just try some random things in order to both floor and map all negatives to-1
? Regardless, very nice answer and much shorter than I anticipated. I was thinking around 20 bytes after i posted my bad 42-byter.
$endgroup$
– Kevin Cruijssen
Jun 17 at 16:37
2
$begingroup$
@KevinCruijssen Nnope,Ø.Ø
was actually my first idea. Your comment inspired me to try random things: I found®Ÿà
,ï®M
, and more importantly,иg
, which yields this nice 8-byter. I always found it annoying that osabie prefers doing nothing over crashing in many cases (div by 0, wrong type, etc), so this crash will come in handy.
$endgroup$
– Grimy
Jun 17 at 17:19
2
$begingroup$
Hehe, 05AB1E is supposed to never crash, but you're right that it sometimes is a bit annoying it never does.. In the legacy I wouldn't even know how to crash, and in the past we even had an explicit division by zero error we could call manually. xD In the new version it still crashes with an error pretty often when giving incorrect arguments to certain builtins. And nice -3 again.
$endgroup$
– Kevin Cruijssen
Jun 18 at 7:48
2
$begingroup$
"crashes the interpreter if x is negative (this is the only exit condition)." - I love that
$endgroup$
– John Dvorak
Jun 18 at 10:41
add a comment |
$begingroup$
05AB1E, 23 11 8 bytes
ΔÍN-;иg=
Try it online!
Uses 0-based indexing.
Explanation:
# start from the implicit input
Δ # loop forever
Í # subtract 2
N- # subtract the current iteration number
; # divide by 2
и # create a list of length x
g # get the length of the list
= # print
иg
looks like a noop, but it actually does two useful things: it truncates to an integer (;
returns a float), and it crashes the interpreter if x is negative (this is the only exit condition).
The 23 byte solution used a very different approach, so here it is for posterity: ÅœʒR2äθP}ʒæOê¥P}θ2äθη€O
(TIO, explanation).
$endgroup$
2
$begingroup$
I've deleted my answer. Mine being 42 and yours being 11 is too big of a difference for me to not feel embarrassed, haha. ;) Nice answer though, and lol at theØ.Ø
. Did you just try some random things in order to both floor and map all negatives to-1
? Regardless, very nice answer and much shorter than I anticipated. I was thinking around 20 bytes after i posted my bad 42-byter.
$endgroup$
– Kevin Cruijssen
Jun 17 at 16:37
2
$begingroup$
@KevinCruijssen Nnope,Ø.Ø
was actually my first idea. Your comment inspired me to try random things: I found®Ÿà
,ï®M
, and more importantly,иg
, which yields this nice 8-byter. I always found it annoying that osabie prefers doing nothing over crashing in many cases (div by 0, wrong type, etc), so this crash will come in handy.
$endgroup$
– Grimy
Jun 17 at 17:19
2
$begingroup$
Hehe, 05AB1E is supposed to never crash, but you're right that it sometimes is a bit annoying it never does.. In the legacy I wouldn't even know how to crash, and in the past we even had an explicit division by zero error we could call manually. xD In the new version it still crashes with an error pretty often when giving incorrect arguments to certain builtins. And nice -3 again.
$endgroup$
– Kevin Cruijssen
Jun 18 at 7:48
2
$begingroup$
"crashes the interpreter if x is negative (this is the only exit condition)." - I love that
$endgroup$
– John Dvorak
Jun 18 at 10:41
add a comment |
$begingroup$
05AB1E, 23 11 8 bytes
ΔÍN-;иg=
Try it online!
Uses 0-based indexing.
Explanation:
# start from the implicit input
Δ # loop forever
Í # subtract 2
N- # subtract the current iteration number
; # divide by 2
и # create a list of length x
g # get the length of the list
= # print
иg
looks like a noop, but it actually does two useful things: it truncates to an integer (;
returns a float), and it crashes the interpreter if x is negative (this is the only exit condition).
The 23 byte solution used a very different approach, so here it is for posterity: ÅœʒR2äθP}ʒæOê¥P}θ2äθη€O
(TIO, explanation).
$endgroup$
05AB1E, 23 11 8 bytes
ΔÍN-;иg=
Try it online!
Uses 0-based indexing.
Explanation:
# start from the implicit input
Δ # loop forever
Í # subtract 2
N- # subtract the current iteration number
; # divide by 2
и # create a list of length x
g # get the length of the list
= # print
иg
looks like a noop, but it actually does two useful things: it truncates to an integer (;
returns a float), and it crashes the interpreter if x is negative (this is the only exit condition).
The 23 byte solution used a very different approach, so here it is for posterity: ÅœʒR2äθP}ʒæOê¥P}θ2äθη€O
(TIO, explanation).
edited Jun 17 at 17:16
answered Jun 17 at 14:35
GrimyGrimy
4,55112 silver badges26 bronze badges
4,55112 silver badges26 bronze badges
2
$begingroup$
I've deleted my answer. Mine being 42 and yours being 11 is too big of a difference for me to not feel embarrassed, haha. ;) Nice answer though, and lol at theØ.Ø
. Did you just try some random things in order to both floor and map all negatives to-1
? Regardless, very nice answer and much shorter than I anticipated. I was thinking around 20 bytes after i posted my bad 42-byter.
$endgroup$
– Kevin Cruijssen
Jun 17 at 16:37
2
$begingroup$
@KevinCruijssen Nnope,Ø.Ø
was actually my first idea. Your comment inspired me to try random things: I found®Ÿà
,ï®M
, and more importantly,иg
, which yields this nice 8-byter. I always found it annoying that osabie prefers doing nothing over crashing in many cases (div by 0, wrong type, etc), so this crash will come in handy.
$endgroup$
– Grimy
Jun 17 at 17:19
2
$begingroup$
Hehe, 05AB1E is supposed to never crash, but you're right that it sometimes is a bit annoying it never does.. In the legacy I wouldn't even know how to crash, and in the past we even had an explicit division by zero error we could call manually. xD In the new version it still crashes with an error pretty often when giving incorrect arguments to certain builtins. And nice -3 again.
$endgroup$
– Kevin Cruijssen
Jun 18 at 7:48
2
$begingroup$
"crashes the interpreter if x is negative (this is the only exit condition)." - I love that
$endgroup$
– John Dvorak
Jun 18 at 10:41
add a comment |
2
$begingroup$
I've deleted my answer. Mine being 42 and yours being 11 is too big of a difference for me to not feel embarrassed, haha. ;) Nice answer though, and lol at theØ.Ø
. Did you just try some random things in order to both floor and map all negatives to-1
? Regardless, very nice answer and much shorter than I anticipated. I was thinking around 20 bytes after i posted my bad 42-byter.
$endgroup$
– Kevin Cruijssen
Jun 17 at 16:37
2
$begingroup$
@KevinCruijssen Nnope,Ø.Ø
was actually my first idea. Your comment inspired me to try random things: I found®Ÿà
,ï®M
, and more importantly,иg
, which yields this nice 8-byter. I always found it annoying that osabie prefers doing nothing over crashing in many cases (div by 0, wrong type, etc), so this crash will come in handy.
$endgroup$
– Grimy
Jun 17 at 17:19
2
$begingroup$
Hehe, 05AB1E is supposed to never crash, but you're right that it sometimes is a bit annoying it never does.. In the legacy I wouldn't even know how to crash, and in the past we even had an explicit division by zero error we could call manually. xD In the new version it still crashes with an error pretty often when giving incorrect arguments to certain builtins. And nice -3 again.
$endgroup$
– Kevin Cruijssen
Jun 18 at 7:48
2
$begingroup$
"crashes the interpreter if x is negative (this is the only exit condition)." - I love that
$endgroup$
– John Dvorak
Jun 18 at 10:41
2
2
$begingroup$
I've deleted my answer. Mine being 42 and yours being 11 is too big of a difference for me to not feel embarrassed, haha. ;) Nice answer though, and lol at the
Ø.Ø
. Did you just try some random things in order to both floor and map all negatives to -1
? Regardless, very nice answer and much shorter than I anticipated. I was thinking around 20 bytes after i posted my bad 42-byter.$endgroup$
– Kevin Cruijssen
Jun 17 at 16:37
$begingroup$
I've deleted my answer. Mine being 42 and yours being 11 is too big of a difference for me to not feel embarrassed, haha. ;) Nice answer though, and lol at the
Ø.Ø
. Did you just try some random things in order to both floor and map all negatives to -1
? Regardless, very nice answer and much shorter than I anticipated. I was thinking around 20 bytes after i posted my bad 42-byter.$endgroup$
– Kevin Cruijssen
Jun 17 at 16:37
2
2
$begingroup$
@KevinCruijssen Nnope,
Ø.Ø
was actually my first idea. Your comment inspired me to try random things: I found ®Ÿà
, ï®M
, and more importantly, иg
, which yields this nice 8-byter. I always found it annoying that osabie prefers doing nothing over crashing in many cases (div by 0, wrong type, etc), so this crash will come in handy.$endgroup$
– Grimy
Jun 17 at 17:19
$begingroup$
@KevinCruijssen Nnope,
Ø.Ø
was actually my first idea. Your comment inspired me to try random things: I found ®Ÿà
, ï®M
, and more importantly, иg
, which yields this nice 8-byter. I always found it annoying that osabie prefers doing nothing over crashing in many cases (div by 0, wrong type, etc), so this crash will come in handy.$endgroup$
– Grimy
Jun 17 at 17:19
2
2
$begingroup$
Hehe, 05AB1E is supposed to never crash, but you're right that it sometimes is a bit annoying it never does.. In the legacy I wouldn't even know how to crash, and in the past we even had an explicit division by zero error we could call manually. xD In the new version it still crashes with an error pretty often when giving incorrect arguments to certain builtins. And nice -3 again.
$endgroup$
– Kevin Cruijssen
Jun 18 at 7:48
$begingroup$
Hehe, 05AB1E is supposed to never crash, but you're right that it sometimes is a bit annoying it never does.. In the legacy I wouldn't even know how to crash, and in the past we even had an explicit division by zero error we could call manually. xD In the new version it still crashes with an error pretty often when giving incorrect arguments to certain builtins. And nice -3 again.
$endgroup$
– Kevin Cruijssen
Jun 18 at 7:48
2
2
$begingroup$
"crashes the interpreter if x is negative (this is the only exit condition)." - I love that
$endgroup$
– John Dvorak
Jun 18 at 10:41
$begingroup$
"crashes the interpreter if x is negative (this is the only exit condition)." - I love that
$endgroup$
– John Dvorak
Jun 18 at 10:41
add a comment |
$begingroup$
Python 2, 75 bytes
f=lambda n,i=0:n>=i<<i and f(n,i+1)or[min(n,2**j*i-i+j)for j in range(1,i)]
Try it online!
Explanation:
Builds a sequence of 'binary' chunks, with a base number matching the number of cuts.
Eg:
63
can be done in 3 cuts, which means a partition in base-4 (as we have 3 single rings):
Cuts: 5, 14, 31
, which gives chains of 4 1 8 1 16 1 32
(sorted: 1 1 1 4 8 16 32
).
All numbers can be made:
1 1
2 1 1
3 1 1 1
4 4
...
42 1 1 8 32
...
62 1 1 4 8 16 32
63 1 1 1 4 8 16 32
Other examples:
18: 4,11 -> 3 1 6 1 7
27: 5,14,27 -> 4 1 8 1 13 1
36: 5,14,31 -> 4 1 8 1 16 1 5
86: 6,17,38,79 -> 5 1 10 1 20 1 40 1 7
$endgroup$
1
$begingroup$
Shouldn't you addf=
to the start? Since you use a call tof
in the lambda function, and I can only assume that you refer to the same lambda you're defining.
$endgroup$
– randomdude999
Jun 17 at 12:03
$begingroup$
@randomdude999, Yeah, I forgot...
$endgroup$
– TFeld
Jun 17 at 12:06
$begingroup$
@randomdude999 does that rule apply to all languages, or just python? Cause I see a javascript answer that is a pure lambda in this challenge...
$endgroup$
– Shadow
Jun 18 at 1:54
3
$begingroup$
@Shadow It applies to all languages, but only for recursive lambdas.
$endgroup$
– TFeld
Jun 18 at 5:10
1
$begingroup$
@Shadow A more generic rule is that you can't reference something that is neither defined in your code nor globally defined in your language, unless it is explicitly allowed by the challenge. The most common case is a recursive function, but this applies to other situations. This answer is another example:f
is not recursive, but it's however referenced in the code and therefore has to be named.
$endgroup$
– Arnauld
Jun 18 at 6:41
add a comment |
$begingroup$
Python 2, 75 bytes
f=lambda n,i=0:n>=i<<i and f(n,i+1)or[min(n,2**j*i-i+j)for j in range(1,i)]
Try it online!
Explanation:
Builds a sequence of 'binary' chunks, with a base number matching the number of cuts.
Eg:
63
can be done in 3 cuts, which means a partition in base-4 (as we have 3 single rings):
Cuts: 5, 14, 31
, which gives chains of 4 1 8 1 16 1 32
(sorted: 1 1 1 4 8 16 32
).
All numbers can be made:
1 1
2 1 1
3 1 1 1
4 4
...
42 1 1 8 32
...
62 1 1 4 8 16 32
63 1 1 1 4 8 16 32
Other examples:
18: 4,11 -> 3 1 6 1 7
27: 5,14,27 -> 4 1 8 1 13 1
36: 5,14,31 -> 4 1 8 1 16 1 5
86: 6,17,38,79 -> 5 1 10 1 20 1 40 1 7
$endgroup$
1
$begingroup$
Shouldn't you addf=
to the start? Since you use a call tof
in the lambda function, and I can only assume that you refer to the same lambda you're defining.
$endgroup$
– randomdude999
Jun 17 at 12:03
$begingroup$
@randomdude999, Yeah, I forgot...
$endgroup$
– TFeld
Jun 17 at 12:06
$begingroup$
@randomdude999 does that rule apply to all languages, or just python? Cause I see a javascript answer that is a pure lambda in this challenge...
$endgroup$
– Shadow
Jun 18 at 1:54
3
$begingroup$
@Shadow It applies to all languages, but only for recursive lambdas.
$endgroup$
– TFeld
Jun 18 at 5:10
1
$begingroup$
@Shadow A more generic rule is that you can't reference something that is neither defined in your code nor globally defined in your language, unless it is explicitly allowed by the challenge. The most common case is a recursive function, but this applies to other situations. This answer is another example:f
is not recursive, but it's however referenced in the code and therefore has to be named.
$endgroup$
– Arnauld
Jun 18 at 6:41
add a comment |
$begingroup$
Python 2, 75 bytes
f=lambda n,i=0:n>=i<<i and f(n,i+1)or[min(n,2**j*i-i+j)for j in range(1,i)]
Try it online!
Explanation:
Builds a sequence of 'binary' chunks, with a base number matching the number of cuts.
Eg:
63
can be done in 3 cuts, which means a partition in base-4 (as we have 3 single rings):
Cuts: 5, 14, 31
, which gives chains of 4 1 8 1 16 1 32
(sorted: 1 1 1 4 8 16 32
).
All numbers can be made:
1 1
2 1 1
3 1 1 1
4 4
...
42 1 1 8 32
...
62 1 1 4 8 16 32
63 1 1 1 4 8 16 32
Other examples:
18: 4,11 -> 3 1 6 1 7
27: 5,14,27 -> 4 1 8 1 13 1
36: 5,14,31 -> 4 1 8 1 16 1 5
86: 6,17,38,79 -> 5 1 10 1 20 1 40 1 7
$endgroup$
Python 2, 75 bytes
f=lambda n,i=0:n>=i<<i and f(n,i+1)or[min(n,2**j*i-i+j)for j in range(1,i)]
Try it online!
Explanation:
Builds a sequence of 'binary' chunks, with a base number matching the number of cuts.
Eg:
63
can be done in 3 cuts, which means a partition in base-4 (as we have 3 single rings):
Cuts: 5, 14, 31
, which gives chains of 4 1 8 1 16 1 32
(sorted: 1 1 1 4 8 16 32
).
All numbers can be made:
1 1
2 1 1
3 1 1 1
4 4
...
42 1 1 8 32
...
62 1 1 4 8 16 32
63 1 1 1 4 8 16 32
Other examples:
18: 4,11 -> 3 1 6 1 7
27: 5,14,27 -> 4 1 8 1 13 1
36: 5,14,31 -> 4 1 8 1 16 1 5
86: 6,17,38,79 -> 5 1 10 1 20 1 40 1 7
edited Jun 17 at 12:52
answered Jun 17 at 11:47
TFeldTFeld
17.3k3 gold badges14 silver badges53 bronze badges
17.3k3 gold badges14 silver badges53 bronze badges
1
$begingroup$
Shouldn't you addf=
to the start? Since you use a call tof
in the lambda function, and I can only assume that you refer to the same lambda you're defining.
$endgroup$
– randomdude999
Jun 17 at 12:03
$begingroup$
@randomdude999, Yeah, I forgot...
$endgroup$
– TFeld
Jun 17 at 12:06
$begingroup$
@randomdude999 does that rule apply to all languages, or just python? Cause I see a javascript answer that is a pure lambda in this challenge...
$endgroup$
– Shadow
Jun 18 at 1:54
3
$begingroup$
@Shadow It applies to all languages, but only for recursive lambdas.
$endgroup$
– TFeld
Jun 18 at 5:10
1
$begingroup$
@Shadow A more generic rule is that you can't reference something that is neither defined in your code nor globally defined in your language, unless it is explicitly allowed by the challenge. The most common case is a recursive function, but this applies to other situations. This answer is another example:f
is not recursive, but it's however referenced in the code and therefore has to be named.
$endgroup$
– Arnauld
Jun 18 at 6:41
add a comment |
1
$begingroup$
Shouldn't you addf=
to the start? Since you use a call tof
in the lambda function, and I can only assume that you refer to the same lambda you're defining.
$endgroup$
– randomdude999
Jun 17 at 12:03
$begingroup$
@randomdude999, Yeah, I forgot...
$endgroup$
– TFeld
Jun 17 at 12:06
$begingroup$
@randomdude999 does that rule apply to all languages, or just python? Cause I see a javascript answer that is a pure lambda in this challenge...
$endgroup$
– Shadow
Jun 18 at 1:54
3
$begingroup$
@Shadow It applies to all languages, but only for recursive lambdas.
$endgroup$
– TFeld
Jun 18 at 5:10
1
$begingroup$
@Shadow A more generic rule is that you can't reference something that is neither defined in your code nor globally defined in your language, unless it is explicitly allowed by the challenge. The most common case is a recursive function, but this applies to other situations. This answer is another example:f
is not recursive, but it's however referenced in the code and therefore has to be named.
$endgroup$
– Arnauld
Jun 18 at 6:41
1
1
$begingroup$
Shouldn't you add
f=
to the start? Since you use a call to f
in the lambda function, and I can only assume that you refer to the same lambda you're defining.$endgroup$
– randomdude999
Jun 17 at 12:03
$begingroup$
Shouldn't you add
f=
to the start? Since you use a call to f
in the lambda function, and I can only assume that you refer to the same lambda you're defining.$endgroup$
– randomdude999
Jun 17 at 12:03
$begingroup$
@randomdude999, Yeah, I forgot...
$endgroup$
– TFeld
Jun 17 at 12:06
$begingroup$
@randomdude999, Yeah, I forgot...
$endgroup$
– TFeld
Jun 17 at 12:06
$begingroup$
@randomdude999 does that rule apply to all languages, or just python? Cause I see a javascript answer that is a pure lambda in this challenge...
$endgroup$
– Shadow
Jun 18 at 1:54
$begingroup$
@randomdude999 does that rule apply to all languages, or just python? Cause I see a javascript answer that is a pure lambda in this challenge...
$endgroup$
– Shadow
Jun 18 at 1:54
3
3
$begingroup$
@Shadow It applies to all languages, but only for recursive lambdas.
$endgroup$
– TFeld
Jun 18 at 5:10
$begingroup$
@Shadow It applies to all languages, but only for recursive lambdas.
$endgroup$
– TFeld
Jun 18 at 5:10
1
1
$begingroup$
@Shadow A more generic rule is that you can't reference something that is neither defined in your code nor globally defined in your language, unless it is explicitly allowed by the challenge. The most common case is a recursive function, but this applies to other situations. This answer is another example:
f
is not recursive, but it's however referenced in the code and therefore has to be named.$endgroup$
– Arnauld
Jun 18 at 6:41
$begingroup$
@Shadow A more generic rule is that you can't reference something that is neither defined in your code nor globally defined in your language, unless it is explicitly allowed by the challenge. The most common case is a recursive function, but this applies to other situations. This answer is another example:
f
is not recursive, but it's however referenced in the code and therefore has to be named.$endgroup$
– Arnauld
Jun 18 at 6:41
add a comment |
$begingroup$
R, 77 69 bytes
-8 bytes thanks to Aaron Hayman
pmin(n<-scan(),0:(k=sum((a=2:n)*2^a<=n))+cumsum((k+2)*2^(0:k))+1)[-n]
Try it online!
Let $k$ be the number of cuts needed; $k$ is the smallest integer such that $(k+1)cdot2^kgeq n$. Indeed, a possible solution is then to have subchains of lengths $1,1,ldots,1$ ($k$ times) and $(k+1), 2(k+1), 4(k+1), 8(k+1), ldots, (k+1)cdot 2^k-1$. It is easy to check that this is sufficient and optimal.
(The last subchain might need to be made shorter if we exceed the total length of the chain.)
Ungolfed (based on previous, similar version):
n = scan() # read input
if(n - 1) # If n==1, return NULL
k = match(F, (a = 2:n) * 2 ^ a > n) # compute k
b = (k + 1) * 2 ^ (1:k - 1) # lengths of subchains
c = 1:k + cumsum(b) # positions of cuts
pmin(c, n ) # if last value is >n, coerce it to n
(Proof that the value of $k$ is as I state: suppose we have $k$ cuts. We then have $k$ unit subchains, so we need the first subchain to be of length $k+1$. We can now handle all lengths up to $2k+1$, so we need the next one to be of length $2k+2$, then $4k+4$... Thus the maximum we can get out of $k$ cuts is obtained by summing all those lengths, which gives $(k+1)cdot 2^k-1$.)
If $a(k)$ is the smallest integer $n$ requiring $k$ cuts, then $a(k)$ is OEIS A134401.
$endgroup$
$begingroup$
I doubt it would help with the special case forn=1
, but an alternative way to generate the cutoffs is the recurrence1, 4, 4a(n-1)-4a(n-2)
.
$endgroup$
– Peter Taylor
Jun 17 at 22:28
$begingroup$
@PeterTaylor I had a similar recurrence for computingk
; this corresponds to OEIS A134401: oeis.org/A134401 But my implementation of the recurrence relation takes up more bytes than the current code.
$endgroup$
– Robin Ryder
Jun 18 at 5:33
$begingroup$
A bit of rearrangement I got it down to 73. Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:06
$begingroup$
@AaronHayman Thanks! Smart move usingsum
instead ofmatch
.
$endgroup$
– Robin Ryder
Jun 18 at 9:41
$begingroup$
69 bytes and got rid of that if statement that was upsetting you: Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:56
|
show 1 more comment
$begingroup$
R, 77 69 bytes
-8 bytes thanks to Aaron Hayman
pmin(n<-scan(),0:(k=sum((a=2:n)*2^a<=n))+cumsum((k+2)*2^(0:k))+1)[-n]
Try it online!
Let $k$ be the number of cuts needed; $k$ is the smallest integer such that $(k+1)cdot2^kgeq n$. Indeed, a possible solution is then to have subchains of lengths $1,1,ldots,1$ ($k$ times) and $(k+1), 2(k+1), 4(k+1), 8(k+1), ldots, (k+1)cdot 2^k-1$. It is easy to check that this is sufficient and optimal.
(The last subchain might need to be made shorter if we exceed the total length of the chain.)
Ungolfed (based on previous, similar version):
n = scan() # read input
if(n - 1) # If n==1, return NULL
k = match(F, (a = 2:n) * 2 ^ a > n) # compute k
b = (k + 1) * 2 ^ (1:k - 1) # lengths of subchains
c = 1:k + cumsum(b) # positions of cuts
pmin(c, n ) # if last value is >n, coerce it to n
(Proof that the value of $k$ is as I state: suppose we have $k$ cuts. We then have $k$ unit subchains, so we need the first subchain to be of length $k+1$. We can now handle all lengths up to $2k+1$, so we need the next one to be of length $2k+2$, then $4k+4$... Thus the maximum we can get out of $k$ cuts is obtained by summing all those lengths, which gives $(k+1)cdot 2^k-1$.)
If $a(k)$ is the smallest integer $n$ requiring $k$ cuts, then $a(k)$ is OEIS A134401.
$endgroup$
$begingroup$
I doubt it would help with the special case forn=1
, but an alternative way to generate the cutoffs is the recurrence1, 4, 4a(n-1)-4a(n-2)
.
$endgroup$
– Peter Taylor
Jun 17 at 22:28
$begingroup$
@PeterTaylor I had a similar recurrence for computingk
; this corresponds to OEIS A134401: oeis.org/A134401 But my implementation of the recurrence relation takes up more bytes than the current code.
$endgroup$
– Robin Ryder
Jun 18 at 5:33
$begingroup$
A bit of rearrangement I got it down to 73. Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:06
$begingroup$
@AaronHayman Thanks! Smart move usingsum
instead ofmatch
.
$endgroup$
– Robin Ryder
Jun 18 at 9:41
$begingroup$
69 bytes and got rid of that if statement that was upsetting you: Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:56
|
show 1 more comment
$begingroup$
R, 77 69 bytes
-8 bytes thanks to Aaron Hayman
pmin(n<-scan(),0:(k=sum((a=2:n)*2^a<=n))+cumsum((k+2)*2^(0:k))+1)[-n]
Try it online!
Let $k$ be the number of cuts needed; $k$ is the smallest integer such that $(k+1)cdot2^kgeq n$. Indeed, a possible solution is then to have subchains of lengths $1,1,ldots,1$ ($k$ times) and $(k+1), 2(k+1), 4(k+1), 8(k+1), ldots, (k+1)cdot 2^k-1$. It is easy to check that this is sufficient and optimal.
(The last subchain might need to be made shorter if we exceed the total length of the chain.)
Ungolfed (based on previous, similar version):
n = scan() # read input
if(n - 1) # If n==1, return NULL
k = match(F, (a = 2:n) * 2 ^ a > n) # compute k
b = (k + 1) * 2 ^ (1:k - 1) # lengths of subchains
c = 1:k + cumsum(b) # positions of cuts
pmin(c, n ) # if last value is >n, coerce it to n
(Proof that the value of $k$ is as I state: suppose we have $k$ cuts. We then have $k$ unit subchains, so we need the first subchain to be of length $k+1$. We can now handle all lengths up to $2k+1$, so we need the next one to be of length $2k+2$, then $4k+4$... Thus the maximum we can get out of $k$ cuts is obtained by summing all those lengths, which gives $(k+1)cdot 2^k-1$.)
If $a(k)$ is the smallest integer $n$ requiring $k$ cuts, then $a(k)$ is OEIS A134401.
$endgroup$
R, 77 69 bytes
-8 bytes thanks to Aaron Hayman
pmin(n<-scan(),0:(k=sum((a=2:n)*2^a<=n))+cumsum((k+2)*2^(0:k))+1)[-n]
Try it online!
Let $k$ be the number of cuts needed; $k$ is the smallest integer such that $(k+1)cdot2^kgeq n$. Indeed, a possible solution is then to have subchains of lengths $1,1,ldots,1$ ($k$ times) and $(k+1), 2(k+1), 4(k+1), 8(k+1), ldots, (k+1)cdot 2^k-1$. It is easy to check that this is sufficient and optimal.
(The last subchain might need to be made shorter if we exceed the total length of the chain.)
Ungolfed (based on previous, similar version):
n = scan() # read input
if(n - 1) # If n==1, return NULL
k = match(F, (a = 2:n) * 2 ^ a > n) # compute k
b = (k + 1) * 2 ^ (1:k - 1) # lengths of subchains
c = 1:k + cumsum(b) # positions of cuts
pmin(c, n ) # if last value is >n, coerce it to n
(Proof that the value of $k$ is as I state: suppose we have $k$ cuts. We then have $k$ unit subchains, so we need the first subchain to be of length $k+1$. We can now handle all lengths up to $2k+1$, so we need the next one to be of length $2k+2$, then $4k+4$... Thus the maximum we can get out of $k$ cuts is obtained by summing all those lengths, which gives $(k+1)cdot 2^k-1$.)
If $a(k)$ is the smallest integer $n$ requiring $k$ cuts, then $a(k)$ is OEIS A134401.
edited Jun 18 at 10:38
answered Jun 17 at 16:21
Robin RyderRobin Ryder
1,9182 silver badges18 bronze badges
1,9182 silver badges18 bronze badges
$begingroup$
I doubt it would help with the special case forn=1
, but an alternative way to generate the cutoffs is the recurrence1, 4, 4a(n-1)-4a(n-2)
.
$endgroup$
– Peter Taylor
Jun 17 at 22:28
$begingroup$
@PeterTaylor I had a similar recurrence for computingk
; this corresponds to OEIS A134401: oeis.org/A134401 But my implementation of the recurrence relation takes up more bytes than the current code.
$endgroup$
– Robin Ryder
Jun 18 at 5:33
$begingroup$
A bit of rearrangement I got it down to 73. Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:06
$begingroup$
@AaronHayman Thanks! Smart move usingsum
instead ofmatch
.
$endgroup$
– Robin Ryder
Jun 18 at 9:41
$begingroup$
69 bytes and got rid of that if statement that was upsetting you: Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:56
|
show 1 more comment
$begingroup$
I doubt it would help with the special case forn=1
, but an alternative way to generate the cutoffs is the recurrence1, 4, 4a(n-1)-4a(n-2)
.
$endgroup$
– Peter Taylor
Jun 17 at 22:28
$begingroup$
@PeterTaylor I had a similar recurrence for computingk
; this corresponds to OEIS A134401: oeis.org/A134401 But my implementation of the recurrence relation takes up more bytes than the current code.
$endgroup$
– Robin Ryder
Jun 18 at 5:33
$begingroup$
A bit of rearrangement I got it down to 73. Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:06
$begingroup$
@AaronHayman Thanks! Smart move usingsum
instead ofmatch
.
$endgroup$
– Robin Ryder
Jun 18 at 9:41
$begingroup$
69 bytes and got rid of that if statement that was upsetting you: Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:56
$begingroup$
I doubt it would help with the special case for
n=1
, but an alternative way to generate the cutoffs is the recurrence 1, 4, 4a(n-1)-4a(n-2)
.$endgroup$
– Peter Taylor
Jun 17 at 22:28
$begingroup$
I doubt it would help with the special case for
n=1
, but an alternative way to generate the cutoffs is the recurrence 1, 4, 4a(n-1)-4a(n-2)
.$endgroup$
– Peter Taylor
Jun 17 at 22:28
$begingroup$
@PeterTaylor I had a similar recurrence for computing
k
; this corresponds to OEIS A134401: oeis.org/A134401 But my implementation of the recurrence relation takes up more bytes than the current code.$endgroup$
– Robin Ryder
Jun 18 at 5:33
$begingroup$
@PeterTaylor I had a similar recurrence for computing
k
; this corresponds to OEIS A134401: oeis.org/A134401 But my implementation of the recurrence relation takes up more bytes than the current code.$endgroup$
– Robin Ryder
Jun 18 at 5:33
$begingroup$
A bit of rearrangement I got it down to 73. Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:06
$begingroup$
A bit of rearrangement I got it down to 73. Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:06
$begingroup$
@AaronHayman Thanks! Smart move using
sum
instead of match
.$endgroup$
– Robin Ryder
Jun 18 at 9:41
$begingroup$
@AaronHayman Thanks! Smart move using
sum
instead of match
.$endgroup$
– Robin Ryder
Jun 18 at 9:41
$begingroup$
69 bytes and got rid of that if statement that was upsetting you: Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:56
$begingroup$
69 bytes and got rid of that if statement that was upsetting you: Try it online!
$endgroup$
– Aaron Hayman
Jun 18 at 9:56
|
show 1 more comment
$begingroup$
JavaScript (ES6), 66 bytes
This is a port of TFeld's answer.
n=>(g=a=>n<++i<<i?a.map(j=>(j+=(i<<j)-i)>n?n:j):g([...a,i]))(i=[])
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 66 bytes
This is a port of TFeld's answer.
n=>(g=a=>n<++i<<i?a.map(j=>(j+=(i<<j)-i)>n?n:j):g([...a,i]))(i=[])
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 66 bytes
This is a port of TFeld's answer.
n=>(g=a=>n<++i<<i?a.map(j=>(j+=(i<<j)-i)>n?n:j):g([...a,i]))(i=[])
Try it online!
$endgroup$
JavaScript (ES6), 66 bytes
This is a port of TFeld's answer.
n=>(g=a=>n<++i<<i?a.map(j=>(j+=(i<<j)-i)>n?n:j):g([...a,i]))(i=[])
Try it online!
answered Jun 17 at 12:23
ArnauldArnauld
86.6k7 gold badges102 silver badges353 bronze badges
86.6k7 gold badges102 silver badges353 bronze badges
add a comment |
add a comment |
$begingroup$
Jelly, 12 bytes
’_‘ɼ:2»-µƬṖḊ
Try it online!
Translation of Grimy's 05AB1E answer so be sure to upvote that one too! Slightly disappointed this comes in a byte longer, but does at least have something a bit like an emoticon as the first three bytes!
$endgroup$
add a comment |
$begingroup$
Jelly, 12 bytes
’_‘ɼ:2»-µƬṖḊ
Try it online!
Translation of Grimy's 05AB1E answer so be sure to upvote that one too! Slightly disappointed this comes in a byte longer, but does at least have something a bit like an emoticon as the first three bytes!
$endgroup$
add a comment |
$begingroup$
Jelly, 12 bytes
’_‘ɼ:2»-µƬṖḊ
Try it online!
Translation of Grimy's 05AB1E answer so be sure to upvote that one too! Slightly disappointed this comes in a byte longer, but does at least have something a bit like an emoticon as the first three bytes!
$endgroup$
Jelly, 12 bytes
’_‘ɼ:2»-µƬṖḊ
Try it online!
Translation of Grimy's 05AB1E answer so be sure to upvote that one too! Slightly disappointed this comes in a byte longer, but does at least have something a bit like an emoticon as the first three bytes!
answered Jun 17 at 17:11
Nick KennedyNick Kennedy
3,5546 silver badges10 bronze badges
3,5546 silver badges10 bronze badges
add a comment |
add a comment |
$begingroup$
Retina 0.8.2, 61 bytes
.+
11,$&$*
+`b(1+),(1(1*)1?3)$
1$2¶1$1,$3
1+,
1
1A`
1+
$.&
Try it online! 1-indexed port of @Grimy's answer. Explanation:
.+
11,$&$*
Start with N=2
and the input converted to unary.
+`b(1+),(1(1*)1?3)$
Repeatedly try to subtract N
from the input and then divide by 2.
1$2¶1$1,$3
If successful, remember 1 more than the input on the previous line, increment N
on the current line, and update the input to the new value.
1+,
1
Remove N
and increment the last value so that it's also 1-indexed.
1A`
Remove the incremented original input.
1+
$.&
Convert the results to decimal.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 61 bytes
.+
11,$&$*
+`b(1+),(1(1*)1?3)$
1$2¶1$1,$3
1+,
1
1A`
1+
$.&
Try it online! 1-indexed port of @Grimy's answer. Explanation:
.+
11,$&$*
Start with N=2
and the input converted to unary.
+`b(1+),(1(1*)1?3)$
Repeatedly try to subtract N
from the input and then divide by 2.
1$2¶1$1,$3
If successful, remember 1 more than the input on the previous line, increment N
on the current line, and update the input to the new value.
1+,
1
Remove N
and increment the last value so that it's also 1-indexed.
1A`
Remove the incremented original input.
1+
$.&
Convert the results to decimal.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 61 bytes
.+
11,$&$*
+`b(1+),(1(1*)1?3)$
1$2¶1$1,$3
1+,
1
1A`
1+
$.&
Try it online! 1-indexed port of @Grimy's answer. Explanation:
.+
11,$&$*
Start with N=2
and the input converted to unary.
+`b(1+),(1(1*)1?3)$
Repeatedly try to subtract N
from the input and then divide by 2.
1$2¶1$1,$3
If successful, remember 1 more than the input on the previous line, increment N
on the current line, and update the input to the new value.
1+,
1
Remove N
and increment the last value so that it's also 1-indexed.
1A`
Remove the incremented original input.
1+
$.&
Convert the results to decimal.
$endgroup$
Retina 0.8.2, 61 bytes
.+
11,$&$*
+`b(1+),(1(1*)1?3)$
1$2¶1$1,$3
1+,
1
1A`
1+
$.&
Try it online! 1-indexed port of @Grimy's answer. Explanation:
.+
11,$&$*
Start with N=2
and the input converted to unary.
+`b(1+),(1(1*)1?3)$
Repeatedly try to subtract N
from the input and then divide by 2.
1$2¶1$1,$3
If successful, remember 1 more than the input on the previous line, increment N
on the current line, and update the input to the new value.
1+,
1
Remove N
and increment the last value so that it's also 1-indexed.
1A`
Remove the incremented original input.
1+
$.&
Convert the results to decimal.
answered Jun 18 at 23:35
NeilNeil
85.5k8 gold badges46 silver badges183 bronze badges
85.5k8 gold badges46 silver badges183 bronze badges
add a comment |
add a comment |
$begingroup$
C++, 109,107 bytes
-2 bytes thanks to Kevin
#include<iostream>
main()int n,k=0;for(std::cin>>n;++k<<k<n;);for(n-=k;n>0;k*=2,n-=k+1)std::cout<<n<<',';
The algorithm is similar to the Robin Ryder's answer. The code is written in a compilable, whole form. Try it!
Details:
std::cin>>n; // get the value of n as input
while(++k<<k<n); // determine k
for(n-=k;n>0;k*=2,n-=k+1) // we don't need n, so the lengths...
std::cout<<n<<' '; // of links are subtracted repeatedly
This has a C variation with the same byte length (doesn't seem to need a separate answer):
#include<stdio.h>
main()int n,k=0;for(scanf("%d",&n);++k<<k<n;);for(n-=k;n>0;k*=2,n-=k+1)printf("%d,",n);
$endgroup$
$begingroup$
Two minor things to golf:=0
afterk
can be removed, since its0
by default.std::cin>>n;while(++k<<k<n);
can befor(std::cin>>n;++k<<k<n;);
. I also have the feelingfor(n-=k;n>0;k*=2,n-=k+1)
can be simplified somehow by combining stuff, but not sure how. PS: Changing the comma-delimiter to a space looks slightly better since you don't see the trailing one imo, but this is purely cosmetic :)
$endgroup$
– Kevin Cruijssen
Jun 19 at 10:58
1
$begingroup$
@KevinCruijssen Thanks, but some compilers don't assign a default value to non-static variables. So I thought=0
was necessary for portability ;) I also realized that the space after#include
is not necessary.
$endgroup$
– polfosol ఠ_ఠ
Jun 19 at 11:02
$begingroup$
Ah ok. I don't know C++ too well, so I've used that online compiler you linked in your answer to test some things. :) You forgot the second change I proposed in my comment: the while-loop to a for-loop and putting thestd::cin>>n
inside it.
$endgroup$
– Kevin Cruijssen
Jun 19 at 11:08
add a comment |
$begingroup$
C++, 109,107 bytes
-2 bytes thanks to Kevin
#include<iostream>
main()int n,k=0;for(std::cin>>n;++k<<k<n;);for(n-=k;n>0;k*=2,n-=k+1)std::cout<<n<<',';
The algorithm is similar to the Robin Ryder's answer. The code is written in a compilable, whole form. Try it!
Details:
std::cin>>n; // get the value of n as input
while(++k<<k<n); // determine k
for(n-=k;n>0;k*=2,n-=k+1) // we don't need n, so the lengths...
std::cout<<n<<' '; // of links are subtracted repeatedly
This has a C variation with the same byte length (doesn't seem to need a separate answer):
#include<stdio.h>
main()int n,k=0;for(scanf("%d",&n);++k<<k<n;);for(n-=k;n>0;k*=2,n-=k+1)printf("%d,",n);
$endgroup$
$begingroup$
Two minor things to golf:=0
afterk
can be removed, since its0
by default.std::cin>>n;while(++k<<k<n);
can befor(std::cin>>n;++k<<k<n;);
. I also have the feelingfor(n-=k;n>0;k*=2,n-=k+1)
can be simplified somehow by combining stuff, but not sure how. PS: Changing the comma-delimiter to a space looks slightly better since you don't see the trailing one imo, but this is purely cosmetic :)
$endgroup$
– Kevin Cruijssen
Jun 19 at 10:58
1
$begingroup$
@KevinCruijssen Thanks, but some compilers don't assign a default value to non-static variables. So I thought=0
was necessary for portability ;) I also realized that the space after#include
is not necessary.
$endgroup$
– polfosol ఠ_ఠ
Jun 19 at 11:02
$begingroup$
Ah ok. I don't know C++ too well, so I've used that online compiler you linked in your answer to test some things. :) You forgot the second change I proposed in my comment: the while-loop to a for-loop and putting thestd::cin>>n
inside it.
$endgroup$
– Kevin Cruijssen
Jun 19 at 11:08
add a comment |
$begingroup$
C++, 109,107 bytes
-2 bytes thanks to Kevin
#include<iostream>
main()int n,k=0;for(std::cin>>n;++k<<k<n;);for(n-=k;n>0;k*=2,n-=k+1)std::cout<<n<<',';
The algorithm is similar to the Robin Ryder's answer. The code is written in a compilable, whole form. Try it!
Details:
std::cin>>n; // get the value of n as input
while(++k<<k<n); // determine k
for(n-=k;n>0;k*=2,n-=k+1) // we don't need n, so the lengths...
std::cout<<n<<' '; // of links are subtracted repeatedly
This has a C variation with the same byte length (doesn't seem to need a separate answer):
#include<stdio.h>
main()int n,k=0;for(scanf("%d",&n);++k<<k<n;);for(n-=k;n>0;k*=2,n-=k+1)printf("%d,",n);
$endgroup$
C++, 109,107 bytes
-2 bytes thanks to Kevin
#include<iostream>
main()int n,k=0;for(std::cin>>n;++k<<k<n;);for(n-=k;n>0;k*=2,n-=k+1)std::cout<<n<<',';
The algorithm is similar to the Robin Ryder's answer. The code is written in a compilable, whole form. Try it!
Details:
std::cin>>n; // get the value of n as input
while(++k<<k<n); // determine k
for(n-=k;n>0;k*=2,n-=k+1) // we don't need n, so the lengths...
std::cout<<n<<' '; // of links are subtracted repeatedly
This has a C variation with the same byte length (doesn't seem to need a separate answer):
#include<stdio.h>
main()int n,k=0;for(scanf("%d",&n);++k<<k<n;);for(n-=k;n>0;k*=2,n-=k+1)printf("%d,",n);
edited Jun 19 at 11:11
answered Jun 19 at 10:31
polfosol ఠ_ఠpolfosol ఠ_ఠ
3292 silver badges8 bronze badges
3292 silver badges8 bronze badges
$begingroup$
Two minor things to golf:=0
afterk
can be removed, since its0
by default.std::cin>>n;while(++k<<k<n);
can befor(std::cin>>n;++k<<k<n;);
. I also have the feelingfor(n-=k;n>0;k*=2,n-=k+1)
can be simplified somehow by combining stuff, but not sure how. PS: Changing the comma-delimiter to a space looks slightly better since you don't see the trailing one imo, but this is purely cosmetic :)
$endgroup$
– Kevin Cruijssen
Jun 19 at 10:58
1
$begingroup$
@KevinCruijssen Thanks, but some compilers don't assign a default value to non-static variables. So I thought=0
was necessary for portability ;) I also realized that the space after#include
is not necessary.
$endgroup$
– polfosol ఠ_ఠ
Jun 19 at 11:02
$begingroup$
Ah ok. I don't know C++ too well, so I've used that online compiler you linked in your answer to test some things. :) You forgot the second change I proposed in my comment: the while-loop to a for-loop and putting thestd::cin>>n
inside it.
$endgroup$
– Kevin Cruijssen
Jun 19 at 11:08
add a comment |
$begingroup$
Two minor things to golf:=0
afterk
can be removed, since its0
by default.std::cin>>n;while(++k<<k<n);
can befor(std::cin>>n;++k<<k<n;);
. I also have the feelingfor(n-=k;n>0;k*=2,n-=k+1)
can be simplified somehow by combining stuff, but not sure how. PS: Changing the comma-delimiter to a space looks slightly better since you don't see the trailing one imo, but this is purely cosmetic :)
$endgroup$
– Kevin Cruijssen
Jun 19 at 10:58
1
$begingroup$
@KevinCruijssen Thanks, but some compilers don't assign a default value to non-static variables. So I thought=0
was necessary for portability ;) I also realized that the space after#include
is not necessary.
$endgroup$
– polfosol ఠ_ఠ
Jun 19 at 11:02
$begingroup$
Ah ok. I don't know C++ too well, so I've used that online compiler you linked in your answer to test some things. :) You forgot the second change I proposed in my comment: the while-loop to a for-loop and putting thestd::cin>>n
inside it.
$endgroup$
– Kevin Cruijssen
Jun 19 at 11:08
$begingroup$
Two minor things to golf:
=0
after k
can be removed, since its 0
by default. std::cin>>n;while(++k<<k<n);
can be for(std::cin>>n;++k<<k<n;);
. I also have the feeling for(n-=k;n>0;k*=2,n-=k+1)
can be simplified somehow by combining stuff, but not sure how. PS: Changing the comma-delimiter to a space looks slightly better since you don't see the trailing one imo, but this is purely cosmetic :)$endgroup$
– Kevin Cruijssen
Jun 19 at 10:58
$begingroup$
Two minor things to golf:
=0
after k
can be removed, since its 0
by default. std::cin>>n;while(++k<<k<n);
can be for(std::cin>>n;++k<<k<n;);
. I also have the feeling for(n-=k;n>0;k*=2,n-=k+1)
can be simplified somehow by combining stuff, but not sure how. PS: Changing the comma-delimiter to a space looks slightly better since you don't see the trailing one imo, but this is purely cosmetic :)$endgroup$
– Kevin Cruijssen
Jun 19 at 10:58
1
1
$begingroup$
@KevinCruijssen Thanks, but some compilers don't assign a default value to non-static variables. So I thought
=0
was necessary for portability ;) I also realized that the space after #include
is not necessary.$endgroup$
– polfosol ఠ_ఠ
Jun 19 at 11:02
$begingroup$
@KevinCruijssen Thanks, but some compilers don't assign a default value to non-static variables. So I thought
=0
was necessary for portability ;) I also realized that the space after #include
is not necessary.$endgroup$
– polfosol ఠ_ఠ
Jun 19 at 11:02
$begingroup$
Ah ok. I don't know C++ too well, so I've used that online compiler you linked in your answer to test some things. :) You forgot the second change I proposed in my comment: the while-loop to a for-loop and putting the
std::cin>>n
inside it.$endgroup$
– Kevin Cruijssen
Jun 19 at 11:08
$begingroup$
Ah ok. I don't know C++ too well, so I've used that online compiler you linked in your answer to test some things. :) You forgot the second change I proposed in my comment: the while-loop to a for-loop and putting the
std::cin>>n
inside it.$endgroup$
– Kevin Cruijssen
Jun 19 at 11:08
add a comment |
$begingroup$
Ruby, 62 bytes
->n(1...c=(0..n).find).map[n,b-c+(c<<b)].min
Try it online!
Mostly stolen from TFeld's Python answer.
$endgroup$
add a comment |
$begingroup$
Ruby, 62 bytes
->n(1...c=(0..n).find).map[n,b-c+(c<<b)].min
Try it online!
Mostly stolen from TFeld's Python answer.
$endgroup$
add a comment |
$begingroup$
Ruby, 62 bytes
->n(1...c=(0..n).find).map[n,b-c+(c<<b)].min
Try it online!
Mostly stolen from TFeld's Python answer.
$endgroup$
Ruby, 62 bytes
->n(1...c=(0..n).find).map[n,b-c+(c<<b)].min
Try it online!
Mostly stolen from TFeld's Python answer.
answered Jun 19 at 12:36
G BG B
8,34614 silver badges30 bronze badges
8,34614 silver badges30 bronze badges
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).
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%2f186983%2fcut-the-gold-chain%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
$begingroup$
I don't understand your "making change" reference. In your posted example, on the second day you pay with the 2-link chain (and get the 1-link-chain (which you paid with the day before) back as change, as per your explanation). But on the third day, you pay with
1+2
. Where did the second 2-link-chain come from?$endgroup$
– Flater
Jun 18 at 11:04
4
$begingroup$
@Flater The manager already has it. We just pay the additional one. In fact, the RHS are the links that manager owns each day
$endgroup$
– polfosol ఠ_ఠ
Jun 18 at 11:17