How much candy can you eat?
up vote
6
down vote
favorite
Credit to Geobits in TNB for the idea
A post without sufficient detail recently posited an interesting game:
2 children sit in front of an array of candy. Each piece of candy is numbered 1 to x
, with x
being the total amount of candy present. There is exactly 1 occurrence of each number.
The goal of the game is for the children to eat candy and multiply the values of the candy they have eaten to arrive at a final score, with the higher score winning.
However the original post missed key information, such as how candy is selected, so the kids in our story decided that the older kid gets to go first, and can eat up to half the candy, however once he announces the end of his turn, he can't change his mind.
One of the kids in this game doesn't like candy, so he wants to eat as little as possible, and he once watched his dad write some code once, and figures he can use the skills gained from that to work out how much candy he needs to eat to ensure victory, whilst still eating as little as possible.
The Challenge
Given the total number of candy x
, your program or function should output the smallest amount of candy he has to eat to ensure victory, n
, even if his opponent eats all the remaining candy.
Naturally bigger numbers make bigger numbers, so whatever amount you'll give him, he'll eat the n
largest numbers.
The Rules
x
will always be in the range0 < x! <= l
wherel
is the upper limit of your language's number handling capabilities- It is guaranteed that the kid will always eat the
n
largest numbers, for example forx = 5
andn = 2
, he will eat4
and5
Test cases
x = 2
n = 1
(2 > 1)
x = 4
n = 2
(3 * 4 == 12 > 1 * 2 == 2)
x = 5
n = 2
(4 * 5 == 20 > 1 * 2 * 3 == 6)
x = 100
n = 42
(product([59..100]) > product([1..58]))
x = 500
n = 220
(product([281..500]) > product([1..280]))
Scoring
Unfortunately, our brave contestant has nothing to write his code with, so he has to arrange the pieces of candy into the characters of the code, as a result, your code needs to be as small as possible, smallest code in bytes wins!
code-golf arithmetic
add a comment |
up vote
6
down vote
favorite
Credit to Geobits in TNB for the idea
A post without sufficient detail recently posited an interesting game:
2 children sit in front of an array of candy. Each piece of candy is numbered 1 to x
, with x
being the total amount of candy present. There is exactly 1 occurrence of each number.
The goal of the game is for the children to eat candy and multiply the values of the candy they have eaten to arrive at a final score, with the higher score winning.
However the original post missed key information, such as how candy is selected, so the kids in our story decided that the older kid gets to go first, and can eat up to half the candy, however once he announces the end of his turn, he can't change his mind.
One of the kids in this game doesn't like candy, so he wants to eat as little as possible, and he once watched his dad write some code once, and figures he can use the skills gained from that to work out how much candy he needs to eat to ensure victory, whilst still eating as little as possible.
The Challenge
Given the total number of candy x
, your program or function should output the smallest amount of candy he has to eat to ensure victory, n
, even if his opponent eats all the remaining candy.
Naturally bigger numbers make bigger numbers, so whatever amount you'll give him, he'll eat the n
largest numbers.
The Rules
x
will always be in the range0 < x! <= l
wherel
is the upper limit of your language's number handling capabilities- It is guaranteed that the kid will always eat the
n
largest numbers, for example forx = 5
andn = 2
, he will eat4
and5
Test cases
x = 2
n = 1
(2 > 1)
x = 4
n = 2
(3 * 4 == 12 > 1 * 2 == 2)
x = 5
n = 2
(4 * 5 == 20 > 1 * 2 * 3 == 6)
x = 100
n = 42
(product([59..100]) > product([1..58]))
x = 500
n = 220
(product([281..500]) > product([1..280]))
Scoring
Unfortunately, our brave contestant has nothing to write his code with, so he has to arrange the pieces of candy into the characters of the code, as a result, your code needs to be as small as possible, smallest code in bytes wins!
code-golf arithmetic
2
How much candy can I eat? All of it. All of the candy.
– AdmBorkBork
6 hours ago
New title: "How much candy need you eat?"
– Sparr
5 hours ago
1
It looks likex=1
must be handled, could you confirm (since it's an edge case which should result in1
rather than0
or infinite loop as a couple of answers do)
– Jonathan Allan
4 hours ago
@JonathanAllan yes,x=1
must be handled, as it is within the bounds of0 < x! <= l
(because 1! is 1, which is greater than 0)
– Skidsdev
4 hours ago
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Credit to Geobits in TNB for the idea
A post without sufficient detail recently posited an interesting game:
2 children sit in front of an array of candy. Each piece of candy is numbered 1 to x
, with x
being the total amount of candy present. There is exactly 1 occurrence of each number.
The goal of the game is for the children to eat candy and multiply the values of the candy they have eaten to arrive at a final score, with the higher score winning.
However the original post missed key information, such as how candy is selected, so the kids in our story decided that the older kid gets to go first, and can eat up to half the candy, however once he announces the end of his turn, he can't change his mind.
One of the kids in this game doesn't like candy, so he wants to eat as little as possible, and he once watched his dad write some code once, and figures he can use the skills gained from that to work out how much candy he needs to eat to ensure victory, whilst still eating as little as possible.
The Challenge
Given the total number of candy x
, your program or function should output the smallest amount of candy he has to eat to ensure victory, n
, even if his opponent eats all the remaining candy.
Naturally bigger numbers make bigger numbers, so whatever amount you'll give him, he'll eat the n
largest numbers.
The Rules
x
will always be in the range0 < x! <= l
wherel
is the upper limit of your language's number handling capabilities- It is guaranteed that the kid will always eat the
n
largest numbers, for example forx = 5
andn = 2
, he will eat4
and5
Test cases
x = 2
n = 1
(2 > 1)
x = 4
n = 2
(3 * 4 == 12 > 1 * 2 == 2)
x = 5
n = 2
(4 * 5 == 20 > 1 * 2 * 3 == 6)
x = 100
n = 42
(product([59..100]) > product([1..58]))
x = 500
n = 220
(product([281..500]) > product([1..280]))
Scoring
Unfortunately, our brave contestant has nothing to write his code with, so he has to arrange the pieces of candy into the characters of the code, as a result, your code needs to be as small as possible, smallest code in bytes wins!
code-golf arithmetic
Credit to Geobits in TNB for the idea
A post without sufficient detail recently posited an interesting game:
2 children sit in front of an array of candy. Each piece of candy is numbered 1 to x
, with x
being the total amount of candy present. There is exactly 1 occurrence of each number.
The goal of the game is for the children to eat candy and multiply the values of the candy they have eaten to arrive at a final score, with the higher score winning.
However the original post missed key information, such as how candy is selected, so the kids in our story decided that the older kid gets to go first, and can eat up to half the candy, however once he announces the end of his turn, he can't change his mind.
One of the kids in this game doesn't like candy, so he wants to eat as little as possible, and he once watched his dad write some code once, and figures he can use the skills gained from that to work out how much candy he needs to eat to ensure victory, whilst still eating as little as possible.
The Challenge
Given the total number of candy x
, your program or function should output the smallest amount of candy he has to eat to ensure victory, n
, even if his opponent eats all the remaining candy.
Naturally bigger numbers make bigger numbers, so whatever amount you'll give him, he'll eat the n
largest numbers.
The Rules
x
will always be in the range0 < x! <= l
wherel
is the upper limit of your language's number handling capabilities- It is guaranteed that the kid will always eat the
n
largest numbers, for example forx = 5
andn = 2
, he will eat4
and5
Test cases
x = 2
n = 1
(2 > 1)
x = 4
n = 2
(3 * 4 == 12 > 1 * 2 == 2)
x = 5
n = 2
(4 * 5 == 20 > 1 * 2 * 3 == 6)
x = 100
n = 42
(product([59..100]) > product([1..58]))
x = 500
n = 220
(product([281..500]) > product([1..280]))
Scoring
Unfortunately, our brave contestant has nothing to write his code with, so he has to arrange the pieces of candy into the characters of the code, as a result, your code needs to be as small as possible, smallest code in bytes wins!
code-golf arithmetic
code-golf arithmetic
edited 5 hours ago
asked 6 hours ago
Skidsdev
6,1982870
6,1982870
2
How much candy can I eat? All of it. All of the candy.
– AdmBorkBork
6 hours ago
New title: "How much candy need you eat?"
– Sparr
5 hours ago
1
It looks likex=1
must be handled, could you confirm (since it's an edge case which should result in1
rather than0
or infinite loop as a couple of answers do)
– Jonathan Allan
4 hours ago
@JonathanAllan yes,x=1
must be handled, as it is within the bounds of0 < x! <= l
(because 1! is 1, which is greater than 0)
– Skidsdev
4 hours ago
add a comment |
2
How much candy can I eat? All of it. All of the candy.
– AdmBorkBork
6 hours ago
New title: "How much candy need you eat?"
– Sparr
5 hours ago
1
It looks likex=1
must be handled, could you confirm (since it's an edge case which should result in1
rather than0
or infinite loop as a couple of answers do)
– Jonathan Allan
4 hours ago
@JonathanAllan yes,x=1
must be handled, as it is within the bounds of0 < x! <= l
(because 1! is 1, which is greater than 0)
– Skidsdev
4 hours ago
2
2
How much candy can I eat? All of it. All of the candy.
– AdmBorkBork
6 hours ago
How much candy can I eat? All of it. All of the candy.
– AdmBorkBork
6 hours ago
New title: "How much candy need you eat?"
– Sparr
5 hours ago
New title: "How much candy need you eat?"
– Sparr
5 hours ago
1
1
It looks like
x=1
must be handled, could you confirm (since it's an edge case which should result in 1
rather than 0
or infinite loop as a couple of answers do)– Jonathan Allan
4 hours ago
It looks like
x=1
must be handled, could you confirm (since it's an edge case which should result in 1
rather than 0
or infinite loop as a couple of answers do)– Jonathan Allan
4 hours ago
@JonathanAllan yes,
x=1
must be handled, as it is within the bounds of 0 < x! <= l
(because 1! is 1, which is greater than 0)– Skidsdev
4 hours ago
@JonathanAllan yes,
x=1
must be handled, as it is within the bounds of 0 < x! <= l
(because 1! is 1, which is greater than 0)– Skidsdev
4 hours ago
add a comment |
8 Answers
8
active
oldest
votes
up vote
3
down vote
Python 3, 76 bytes
F=lambda x:x<2or x*F(x-1)
f=lambda x,n=1:x<2or n*(F(x)>F(x-n)**2)or f(x,n+1)
Try it online!
Relies on the fact that for eating $n$ candies to still win and the total number of candies being $x$, $frac{x!}{(x-n)!}>(x-n)!$ must be true, which means $x!>((x-n)!)^2$.
-1 from Skidsdev
-3 -6 from BMO
-3 from Sparr
+6 to fix x = 1
1
You can save 1 byte by replacing the top function withfrom math import factorial as F
– Skidsdev
5 hours ago
1
You can rewrite these recursion using short-circuiting behaviour, eg. for the second one:n*(F(x)>F(x-n)**2)or f(x,n+1)
. Similarlyx<2or x*F(x-1)
for the first one which is shorter than the import.
– BMO
5 hours ago
1
All three of those are nice suggestions, thanks. (And added)
– nedla2004
5 hours ago
1
-3 bytes withimport math;F=math.factorial
which I should probably go find the python tips meta to mention...
– Sparr
5 hours ago
2
@Sparr: ButF=lambda x:x<2or x*F(x-1)
is three bytes less?
– BMO
5 hours ago
|
show 3 more comments
up vote
1
down vote
Haskell, 52 51 bytes
Using the straightforward approach: We check whether the product of the last $n$ numbers, which is $frac{x!}{(x-n)!}$ is less than the product of the first $n$ numbers, namely $(x-n)!$ and takes the least $n$ for which this is true.
g b=product[1..b]
f x=[n|n<-[1..],g(x-n)^2<=g x]!!0
Try it online!
add a comment |
up vote
1
down vote
Clean, 57 bytes
import StdEnv
$x=while(e=prod[1..x-e]^2>prod[1..x])inc 1
Try it online!
A straight-forward solution.
add a comment |
up vote
1
down vote
Jelly, 9 bytes
ḊPÐƤ<!€TL
Try it online! Or see the test-suite.
How?
ḊPÐƤ<!€TL - Link: integer, x e.g. 7
Ḋ - dequeue (implicit range of x) [ 2, 3, 4, 5, 6, 7]
ÐƤ - for postfixes [all, allButFirst, ...]:
P - product [5040,2520, 840, 210, 42, 7]
€ - for each (in implicit range of x):
! - factorial [ 1, 2, 6, 24, 120, 720, 5040]
< - (left) less than (right)? [ 0, 0, 0, 0, 1, 1, 5040]
- -- note right always 1 longer than left giving trailing x! like the 5040 ^
T - truthy indices [ 5, 6, 7 ]
L - length 3
that's impressive but would be more educative if explained
– Setop
4 hours ago
It will be... :)
– Jonathan Allan
4 hours ago
@Setop - added.
– Jonathan Allan
4 hours ago
like it ! and it must be fast compare to all solutions with tons of factorials
– Setop
4 hours ago
Nah, still calculates all those products and factorials (more than some other solutions).
– Jonathan Allan
3 hours ago
add a comment |
up vote
1
down vote
Python 3, 183 176 bytes
R=reversed
L=list
def M(I):
r=1
for i in I:
r*=i
yield r
def f(x):
S=L(range(1,x+1))
for(n,a,b)in zip([0]+S,R(L(M(S))),[0]+L(M(R(S)))):
if(b>a):
return n
return x
Try it online!
It's is a lot faster than some other solutions - 0(N) multiplications instead of O(N²) - but I can't manage to reduce code size.
add a comment |
up vote
0
down vote
Jelly, 14 bytes
ạ‘rP>ạ!¥
1ç1#«
Try it online!
Handles 1 correctly.
add a comment |
up vote
0
down vote
JavaScript (ES6), 53 bytes
n=>(g=p=>x<n?g(p*++x):q<p&&1+g(p/n,q*=n--))(q=x=1)||n
Try it online!
Works up to $a(170)$. Beyond that, we'd need BigInts (+1 byte).
How?
We start with $p=n!$ and $q=1$.
You can think of $q$ as the product of our candies and of $p$ as the product of the candies of the other kid. (So at the beginning of the process, the other kid has all the candies and we have none.)
Then we repeat the following operations:
- divide $p$ by $n$
- multiply $q$ by $n$
- decrement $n$
until $qge p$.
The result is the number of required iterations. (At each iteration, we 'take the next highest candy from the other kid'.)
Commented
This is implemented as a single recursive function which first compute $n!$ and then enters the loop described above.
n => ( // main function taking n
g = p => // g = recursive function taking p
x < n ? // if x is less than n:
g( // this is the first part of the recursion:
p * ++x // we're computing p = n! by multiplying p
) // by x = 1 .. n
: // else (second part):
q < p && // while q is less than p:
1 + g( // add 1 to the final result
p / n, // divide p by n
q *= n-- // multiply q by n; decrement n
) //
)(q = x = 1) // initial call to g with p = q = x = 1
|| n // edge cases: return n for n < 2
add a comment |
up vote
0
down vote
05AB1E, 15 bytes
EI!IN-!n›Ði–#]1
Try it online!
EI!IN-!n›Ði–#]1
E For loop with N from [1 ... input]
I! Push factorial of input
IN- Push input - N (x - n)
! Factorial
n Square
› Push input! > (input - N)^2 or x! > (x - n)^2
Ð Triplicate, used for three upcoming operations (if, print, end)
i If, run code after if top of stack is 1 (found minimum number of candies)
– Print N if top of stack is 1
#] Ends for loop and program is top of stack is 1
1 Pushes 1 so that if the input is 1 and we leave the for loop without
printing anything 1 is printed
Uses the same approach as my Python submission. Very new to 05AB1E so any tips on code or explaination greatly appreciated.
add a comment |
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Python 3, 76 bytes
F=lambda x:x<2or x*F(x-1)
f=lambda x,n=1:x<2or n*(F(x)>F(x-n)**2)or f(x,n+1)
Try it online!
Relies on the fact that for eating $n$ candies to still win and the total number of candies being $x$, $frac{x!}{(x-n)!}>(x-n)!$ must be true, which means $x!>((x-n)!)^2$.
-1 from Skidsdev
-3 -6 from BMO
-3 from Sparr
+6 to fix x = 1
1
You can save 1 byte by replacing the top function withfrom math import factorial as F
– Skidsdev
5 hours ago
1
You can rewrite these recursion using short-circuiting behaviour, eg. for the second one:n*(F(x)>F(x-n)**2)or f(x,n+1)
. Similarlyx<2or x*F(x-1)
for the first one which is shorter than the import.
– BMO
5 hours ago
1
All three of those are nice suggestions, thanks. (And added)
– nedla2004
5 hours ago
1
-3 bytes withimport math;F=math.factorial
which I should probably go find the python tips meta to mention...
– Sparr
5 hours ago
2
@Sparr: ButF=lambda x:x<2or x*F(x-1)
is three bytes less?
– BMO
5 hours ago
|
show 3 more comments
up vote
3
down vote
Python 3, 76 bytes
F=lambda x:x<2or x*F(x-1)
f=lambda x,n=1:x<2or n*(F(x)>F(x-n)**2)or f(x,n+1)
Try it online!
Relies on the fact that for eating $n$ candies to still win and the total number of candies being $x$, $frac{x!}{(x-n)!}>(x-n)!$ must be true, which means $x!>((x-n)!)^2$.
-1 from Skidsdev
-3 -6 from BMO
-3 from Sparr
+6 to fix x = 1
1
You can save 1 byte by replacing the top function withfrom math import factorial as F
– Skidsdev
5 hours ago
1
You can rewrite these recursion using short-circuiting behaviour, eg. for the second one:n*(F(x)>F(x-n)**2)or f(x,n+1)
. Similarlyx<2or x*F(x-1)
for the first one which is shorter than the import.
– BMO
5 hours ago
1
All three of those are nice suggestions, thanks. (And added)
– nedla2004
5 hours ago
1
-3 bytes withimport math;F=math.factorial
which I should probably go find the python tips meta to mention...
– Sparr
5 hours ago
2
@Sparr: ButF=lambda x:x<2or x*F(x-1)
is three bytes less?
– BMO
5 hours ago
|
show 3 more comments
up vote
3
down vote
up vote
3
down vote
Python 3, 76 bytes
F=lambda x:x<2or x*F(x-1)
f=lambda x,n=1:x<2or n*(F(x)>F(x-n)**2)or f(x,n+1)
Try it online!
Relies on the fact that for eating $n$ candies to still win and the total number of candies being $x$, $frac{x!}{(x-n)!}>(x-n)!$ must be true, which means $x!>((x-n)!)^2$.
-1 from Skidsdev
-3 -6 from BMO
-3 from Sparr
+6 to fix x = 1
Python 3, 76 bytes
F=lambda x:x<2or x*F(x-1)
f=lambda x,n=1:x<2or n*(F(x)>F(x-n)**2)or f(x,n+1)
Try it online!
Relies on the fact that for eating $n$ candies to still win and the total number of candies being $x$, $frac{x!}{(x-n)!}>(x-n)!$ must be true, which means $x!>((x-n)!)^2$.
-1 from Skidsdev
-3 -6 from BMO
-3 from Sparr
+6 to fix x = 1
edited 4 hours ago
answered 6 hours ago
nedla2004
3211310
3211310
1
You can save 1 byte by replacing the top function withfrom math import factorial as F
– Skidsdev
5 hours ago
1
You can rewrite these recursion using short-circuiting behaviour, eg. for the second one:n*(F(x)>F(x-n)**2)or f(x,n+1)
. Similarlyx<2or x*F(x-1)
for the first one which is shorter than the import.
– BMO
5 hours ago
1
All three of those are nice suggestions, thanks. (And added)
– nedla2004
5 hours ago
1
-3 bytes withimport math;F=math.factorial
which I should probably go find the python tips meta to mention...
– Sparr
5 hours ago
2
@Sparr: ButF=lambda x:x<2or x*F(x-1)
is three bytes less?
– BMO
5 hours ago
|
show 3 more comments
1
You can save 1 byte by replacing the top function withfrom math import factorial as F
– Skidsdev
5 hours ago
1
You can rewrite these recursion using short-circuiting behaviour, eg. for the second one:n*(F(x)>F(x-n)**2)or f(x,n+1)
. Similarlyx<2or x*F(x-1)
for the first one which is shorter than the import.
– BMO
5 hours ago
1
All three of those are nice suggestions, thanks. (And added)
– nedla2004
5 hours ago
1
-3 bytes withimport math;F=math.factorial
which I should probably go find the python tips meta to mention...
– Sparr
5 hours ago
2
@Sparr: ButF=lambda x:x<2or x*F(x-1)
is three bytes less?
– BMO
5 hours ago
1
1
You can save 1 byte by replacing the top function with
from math import factorial as F
– Skidsdev
5 hours ago
You can save 1 byte by replacing the top function with
from math import factorial as F
– Skidsdev
5 hours ago
1
1
You can rewrite these recursion using short-circuiting behaviour, eg. for the second one:
n*(F(x)>F(x-n)**2)or f(x,n+1)
. Similarly x<2or x*F(x-1)
for the first one which is shorter than the import.– BMO
5 hours ago
You can rewrite these recursion using short-circuiting behaviour, eg. for the second one:
n*(F(x)>F(x-n)**2)or f(x,n+1)
. Similarly x<2or x*F(x-1)
for the first one which is shorter than the import.– BMO
5 hours ago
1
1
All three of those are nice suggestions, thanks. (And added)
– nedla2004
5 hours ago
All three of those are nice suggestions, thanks. (And added)
– nedla2004
5 hours ago
1
1
-3 bytes with
import math;F=math.factorial
which I should probably go find the python tips meta to mention...– Sparr
5 hours ago
-3 bytes with
import math;F=math.factorial
which I should probably go find the python tips meta to mention...– Sparr
5 hours ago
2
2
@Sparr: But
F=lambda x:x<2or x*F(x-1)
is three bytes less?– BMO
5 hours ago
@Sparr: But
F=lambda x:x<2or x*F(x-1)
is three bytes less?– BMO
5 hours ago
|
show 3 more comments
up vote
1
down vote
Haskell, 52 51 bytes
Using the straightforward approach: We check whether the product of the last $n$ numbers, which is $frac{x!}{(x-n)!}$ is less than the product of the first $n$ numbers, namely $(x-n)!$ and takes the least $n$ for which this is true.
g b=product[1..b]
f x=[n|n<-[1..],g(x-n)^2<=g x]!!0
Try it online!
add a comment |
up vote
1
down vote
Haskell, 52 51 bytes
Using the straightforward approach: We check whether the product of the last $n$ numbers, which is $frac{x!}{(x-n)!}$ is less than the product of the first $n$ numbers, namely $(x-n)!$ and takes the least $n$ for which this is true.
g b=product[1..b]
f x=[n|n<-[1..],g(x-n)^2<=g x]!!0
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
Haskell, 52 51 bytes
Using the straightforward approach: We check whether the product of the last $n$ numbers, which is $frac{x!}{(x-n)!}$ is less than the product of the first $n$ numbers, namely $(x-n)!$ and takes the least $n$ for which this is true.
g b=product[1..b]
f x=[n|n<-[1..],g(x-n)^2<=g x]!!0
Try it online!
Haskell, 52 51 bytes
Using the straightforward approach: We check whether the product of the last $n$ numbers, which is $frac{x!}{(x-n)!}$ is less than the product of the first $n$ numbers, namely $(x-n)!$ and takes the least $n$ for which this is true.
g b=product[1..b]
f x=[n|n<-[1..],g(x-n)^2<=g x]!!0
Try it online!
edited 5 hours ago
answered 5 hours ago
flawr
26.5k664186
26.5k664186
add a comment |
add a comment |
up vote
1
down vote
Clean, 57 bytes
import StdEnv
$x=while(e=prod[1..x-e]^2>prod[1..x])inc 1
Try it online!
A straight-forward solution.
add a comment |
up vote
1
down vote
Clean, 57 bytes
import StdEnv
$x=while(e=prod[1..x-e]^2>prod[1..x])inc 1
Try it online!
A straight-forward solution.
add a comment |
up vote
1
down vote
up vote
1
down vote
Clean, 57 bytes
import StdEnv
$x=while(e=prod[1..x-e]^2>prod[1..x])inc 1
Try it online!
A straight-forward solution.
Clean, 57 bytes
import StdEnv
$x=while(e=prod[1..x-e]^2>prod[1..x])inc 1
Try it online!
A straight-forward solution.
edited 4 hours ago
answered 4 hours ago
Οurous
6,18311032
6,18311032
add a comment |
add a comment |
up vote
1
down vote
Jelly, 9 bytes
ḊPÐƤ<!€TL
Try it online! Or see the test-suite.
How?
ḊPÐƤ<!€TL - Link: integer, x e.g. 7
Ḋ - dequeue (implicit range of x) [ 2, 3, 4, 5, 6, 7]
ÐƤ - for postfixes [all, allButFirst, ...]:
P - product [5040,2520, 840, 210, 42, 7]
€ - for each (in implicit range of x):
! - factorial [ 1, 2, 6, 24, 120, 720, 5040]
< - (left) less than (right)? [ 0, 0, 0, 0, 1, 1, 5040]
- -- note right always 1 longer than left giving trailing x! like the 5040 ^
T - truthy indices [ 5, 6, 7 ]
L - length 3
that's impressive but would be more educative if explained
– Setop
4 hours ago
It will be... :)
– Jonathan Allan
4 hours ago
@Setop - added.
– Jonathan Allan
4 hours ago
like it ! and it must be fast compare to all solutions with tons of factorials
– Setop
4 hours ago
Nah, still calculates all those products and factorials (more than some other solutions).
– Jonathan Allan
3 hours ago
add a comment |
up vote
1
down vote
Jelly, 9 bytes
ḊPÐƤ<!€TL
Try it online! Or see the test-suite.
How?
ḊPÐƤ<!€TL - Link: integer, x e.g. 7
Ḋ - dequeue (implicit range of x) [ 2, 3, 4, 5, 6, 7]
ÐƤ - for postfixes [all, allButFirst, ...]:
P - product [5040,2520, 840, 210, 42, 7]
€ - for each (in implicit range of x):
! - factorial [ 1, 2, 6, 24, 120, 720, 5040]
< - (left) less than (right)? [ 0, 0, 0, 0, 1, 1, 5040]
- -- note right always 1 longer than left giving trailing x! like the 5040 ^
T - truthy indices [ 5, 6, 7 ]
L - length 3
that's impressive but would be more educative if explained
– Setop
4 hours ago
It will be... :)
– Jonathan Allan
4 hours ago
@Setop - added.
– Jonathan Allan
4 hours ago
like it ! and it must be fast compare to all solutions with tons of factorials
– Setop
4 hours ago
Nah, still calculates all those products and factorials (more than some other solutions).
– Jonathan Allan
3 hours ago
add a comment |
up vote
1
down vote
up vote
1
down vote
Jelly, 9 bytes
ḊPÐƤ<!€TL
Try it online! Or see the test-suite.
How?
ḊPÐƤ<!€TL - Link: integer, x e.g. 7
Ḋ - dequeue (implicit range of x) [ 2, 3, 4, 5, 6, 7]
ÐƤ - for postfixes [all, allButFirst, ...]:
P - product [5040,2520, 840, 210, 42, 7]
€ - for each (in implicit range of x):
! - factorial [ 1, 2, 6, 24, 120, 720, 5040]
< - (left) less than (right)? [ 0, 0, 0, 0, 1, 1, 5040]
- -- note right always 1 longer than left giving trailing x! like the 5040 ^
T - truthy indices [ 5, 6, 7 ]
L - length 3
Jelly, 9 bytes
ḊPÐƤ<!€TL
Try it online! Or see the test-suite.
How?
ḊPÐƤ<!€TL - Link: integer, x e.g. 7
Ḋ - dequeue (implicit range of x) [ 2, 3, 4, 5, 6, 7]
ÐƤ - for postfixes [all, allButFirst, ...]:
P - product [5040,2520, 840, 210, 42, 7]
€ - for each (in implicit range of x):
! - factorial [ 1, 2, 6, 24, 120, 720, 5040]
< - (left) less than (right)? [ 0, 0, 0, 0, 1, 1, 5040]
- -- note right always 1 longer than left giving trailing x! like the 5040 ^
T - truthy indices [ 5, 6, 7 ]
L - length 3
edited 3 hours ago
answered 4 hours ago
Jonathan Allan
50.5k534165
50.5k534165
that's impressive but would be more educative if explained
– Setop
4 hours ago
It will be... :)
– Jonathan Allan
4 hours ago
@Setop - added.
– Jonathan Allan
4 hours ago
like it ! and it must be fast compare to all solutions with tons of factorials
– Setop
4 hours ago
Nah, still calculates all those products and factorials (more than some other solutions).
– Jonathan Allan
3 hours ago
add a comment |
that's impressive but would be more educative if explained
– Setop
4 hours ago
It will be... :)
– Jonathan Allan
4 hours ago
@Setop - added.
– Jonathan Allan
4 hours ago
like it ! and it must be fast compare to all solutions with tons of factorials
– Setop
4 hours ago
Nah, still calculates all those products and factorials (more than some other solutions).
– Jonathan Allan
3 hours ago
that's impressive but would be more educative if explained
– Setop
4 hours ago
that's impressive but would be more educative if explained
– Setop
4 hours ago
It will be... :)
– Jonathan Allan
4 hours ago
It will be... :)
– Jonathan Allan
4 hours ago
@Setop - added.
– Jonathan Allan
4 hours ago
@Setop - added.
– Jonathan Allan
4 hours ago
like it ! and it must be fast compare to all solutions with tons of factorials
– Setop
4 hours ago
like it ! and it must be fast compare to all solutions with tons of factorials
– Setop
4 hours ago
Nah, still calculates all those products and factorials (more than some other solutions).
– Jonathan Allan
3 hours ago
Nah, still calculates all those products and factorials (more than some other solutions).
– Jonathan Allan
3 hours ago
add a comment |
up vote
1
down vote
Python 3, 183 176 bytes
R=reversed
L=list
def M(I):
r=1
for i in I:
r*=i
yield r
def f(x):
S=L(range(1,x+1))
for(n,a,b)in zip([0]+S,R(L(M(S))),[0]+L(M(R(S)))):
if(b>a):
return n
return x
Try it online!
It's is a lot faster than some other solutions - 0(N) multiplications instead of O(N²) - but I can't manage to reduce code size.
add a comment |
up vote
1
down vote
Python 3, 183 176 bytes
R=reversed
L=list
def M(I):
r=1
for i in I:
r*=i
yield r
def f(x):
S=L(range(1,x+1))
for(n,a,b)in zip([0]+S,R(L(M(S))),[0]+L(M(R(S)))):
if(b>a):
return n
return x
Try it online!
It's is a lot faster than some other solutions - 0(N) multiplications instead of O(N²) - but I can't manage to reduce code size.
add a comment |
up vote
1
down vote
up vote
1
down vote
Python 3, 183 176 bytes
R=reversed
L=list
def M(I):
r=1
for i in I:
r*=i
yield r
def f(x):
S=L(range(1,x+1))
for(n,a,b)in zip([0]+S,R(L(M(S))),[0]+L(M(R(S)))):
if(b>a):
return n
return x
Try it online!
It's is a lot faster than some other solutions - 0(N) multiplications instead of O(N²) - but I can't manage to reduce code size.
Python 3, 183 176 bytes
R=reversed
L=list
def M(I):
r=1
for i in I:
r*=i
yield r
def f(x):
S=L(range(1,x+1))
for(n,a,b)in zip([0]+S,R(L(M(S))),[0]+L(M(R(S)))):
if(b>a):
return n
return x
Try it online!
It's is a lot faster than some other solutions - 0(N) multiplications instead of O(N²) - but I can't manage to reduce code size.
edited 2 hours ago
answered 2 hours ago
Setop
1586
1586
add a comment |
add a comment |
up vote
0
down vote
Jelly, 14 bytes
ạ‘rP>ạ!¥
1ç1#«
Try it online!
Handles 1 correctly.
add a comment |
up vote
0
down vote
Jelly, 14 bytes
ạ‘rP>ạ!¥
1ç1#«
Try it online!
Handles 1 correctly.
add a comment |
up vote
0
down vote
up vote
0
down vote
Jelly, 14 bytes
ạ‘rP>ạ!¥
1ç1#«
Try it online!
Handles 1 correctly.
Jelly, 14 bytes
ạ‘rP>ạ!¥
1ç1#«
Try it online!
Handles 1 correctly.
answered 4 hours ago
Erik the Outgolfer
31k429102
31k429102
add a comment |
add a comment |
up vote
0
down vote
JavaScript (ES6), 53 bytes
n=>(g=p=>x<n?g(p*++x):q<p&&1+g(p/n,q*=n--))(q=x=1)||n
Try it online!
Works up to $a(170)$. Beyond that, we'd need BigInts (+1 byte).
How?
We start with $p=n!$ and $q=1$.
You can think of $q$ as the product of our candies and of $p$ as the product of the candies of the other kid. (So at the beginning of the process, the other kid has all the candies and we have none.)
Then we repeat the following operations:
- divide $p$ by $n$
- multiply $q$ by $n$
- decrement $n$
until $qge p$.
The result is the number of required iterations. (At each iteration, we 'take the next highest candy from the other kid'.)
Commented
This is implemented as a single recursive function which first compute $n!$ and then enters the loop described above.
n => ( // main function taking n
g = p => // g = recursive function taking p
x < n ? // if x is less than n:
g( // this is the first part of the recursion:
p * ++x // we're computing p = n! by multiplying p
) // by x = 1 .. n
: // else (second part):
q < p && // while q is less than p:
1 + g( // add 1 to the final result
p / n, // divide p by n
q *= n-- // multiply q by n; decrement n
) //
)(q = x = 1) // initial call to g with p = q = x = 1
|| n // edge cases: return n for n < 2
add a comment |
up vote
0
down vote
JavaScript (ES6), 53 bytes
n=>(g=p=>x<n?g(p*++x):q<p&&1+g(p/n,q*=n--))(q=x=1)||n
Try it online!
Works up to $a(170)$. Beyond that, we'd need BigInts (+1 byte).
How?
We start with $p=n!$ and $q=1$.
You can think of $q$ as the product of our candies and of $p$ as the product of the candies of the other kid. (So at the beginning of the process, the other kid has all the candies and we have none.)
Then we repeat the following operations:
- divide $p$ by $n$
- multiply $q$ by $n$
- decrement $n$
until $qge p$.
The result is the number of required iterations. (At each iteration, we 'take the next highest candy from the other kid'.)
Commented
This is implemented as a single recursive function which first compute $n!$ and then enters the loop described above.
n => ( // main function taking n
g = p => // g = recursive function taking p
x < n ? // if x is less than n:
g( // this is the first part of the recursion:
p * ++x // we're computing p = n! by multiplying p
) // by x = 1 .. n
: // else (second part):
q < p && // while q is less than p:
1 + g( // add 1 to the final result
p / n, // divide p by n
q *= n-- // multiply q by n; decrement n
) //
)(q = x = 1) // initial call to g with p = q = x = 1
|| n // edge cases: return n for n < 2
add a comment |
up vote
0
down vote
up vote
0
down vote
JavaScript (ES6), 53 bytes
n=>(g=p=>x<n?g(p*++x):q<p&&1+g(p/n,q*=n--))(q=x=1)||n
Try it online!
Works up to $a(170)$. Beyond that, we'd need BigInts (+1 byte).
How?
We start with $p=n!$ and $q=1$.
You can think of $q$ as the product of our candies and of $p$ as the product of the candies of the other kid. (So at the beginning of the process, the other kid has all the candies and we have none.)
Then we repeat the following operations:
- divide $p$ by $n$
- multiply $q$ by $n$
- decrement $n$
until $qge p$.
The result is the number of required iterations. (At each iteration, we 'take the next highest candy from the other kid'.)
Commented
This is implemented as a single recursive function which first compute $n!$ and then enters the loop described above.
n => ( // main function taking n
g = p => // g = recursive function taking p
x < n ? // if x is less than n:
g( // this is the first part of the recursion:
p * ++x // we're computing p = n! by multiplying p
) // by x = 1 .. n
: // else (second part):
q < p && // while q is less than p:
1 + g( // add 1 to the final result
p / n, // divide p by n
q *= n-- // multiply q by n; decrement n
) //
)(q = x = 1) // initial call to g with p = q = x = 1
|| n // edge cases: return n for n < 2
JavaScript (ES6), 53 bytes
n=>(g=p=>x<n?g(p*++x):q<p&&1+g(p/n,q*=n--))(q=x=1)||n
Try it online!
Works up to $a(170)$. Beyond that, we'd need BigInts (+1 byte).
How?
We start with $p=n!$ and $q=1$.
You can think of $q$ as the product of our candies and of $p$ as the product of the candies of the other kid. (So at the beginning of the process, the other kid has all the candies and we have none.)
Then we repeat the following operations:
- divide $p$ by $n$
- multiply $q$ by $n$
- decrement $n$
until $qge p$.
The result is the number of required iterations. (At each iteration, we 'take the next highest candy from the other kid'.)
Commented
This is implemented as a single recursive function which first compute $n!$ and then enters the loop described above.
n => ( // main function taking n
g = p => // g = recursive function taking p
x < n ? // if x is less than n:
g( // this is the first part of the recursion:
p * ++x // we're computing p = n! by multiplying p
) // by x = 1 .. n
: // else (second part):
q < p && // while q is less than p:
1 + g( // add 1 to the final result
p / n, // divide p by n
q *= n-- // multiply q by n; decrement n
) //
)(q = x = 1) // initial call to g with p = q = x = 1
|| n // edge cases: return n for n < 2
edited 4 hours ago
answered 5 hours ago
Arnauld
71.3k688298
71.3k688298
add a comment |
add a comment |
up vote
0
down vote
05AB1E, 15 bytes
EI!IN-!n›Ði–#]1
Try it online!
EI!IN-!n›Ði–#]1
E For loop with N from [1 ... input]
I! Push factorial of input
IN- Push input - N (x - n)
! Factorial
n Square
› Push input! > (input - N)^2 or x! > (x - n)^2
Ð Triplicate, used for three upcoming operations (if, print, end)
i If, run code after if top of stack is 1 (found minimum number of candies)
– Print N if top of stack is 1
#] Ends for loop and program is top of stack is 1
1 Pushes 1 so that if the input is 1 and we leave the for loop without
printing anything 1 is printed
Uses the same approach as my Python submission. Very new to 05AB1E so any tips on code or explaination greatly appreciated.
add a comment |
up vote
0
down vote
05AB1E, 15 bytes
EI!IN-!n›Ði–#]1
Try it online!
EI!IN-!n›Ði–#]1
E For loop with N from [1 ... input]
I! Push factorial of input
IN- Push input - N (x - n)
! Factorial
n Square
› Push input! > (input - N)^2 or x! > (x - n)^2
Ð Triplicate, used for three upcoming operations (if, print, end)
i If, run code after if top of stack is 1 (found minimum number of candies)
– Print N if top of stack is 1
#] Ends for loop and program is top of stack is 1
1 Pushes 1 so that if the input is 1 and we leave the for loop without
printing anything 1 is printed
Uses the same approach as my Python submission. Very new to 05AB1E so any tips on code or explaination greatly appreciated.
add a comment |
up vote
0
down vote
up vote
0
down vote
05AB1E, 15 bytes
EI!IN-!n›Ði–#]1
Try it online!
EI!IN-!n›Ði–#]1
E For loop with N from [1 ... input]
I! Push factorial of input
IN- Push input - N (x - n)
! Factorial
n Square
› Push input! > (input - N)^2 or x! > (x - n)^2
Ð Triplicate, used for three upcoming operations (if, print, end)
i If, run code after if top of stack is 1 (found minimum number of candies)
– Print N if top of stack is 1
#] Ends for loop and program is top of stack is 1
1 Pushes 1 so that if the input is 1 and we leave the for loop without
printing anything 1 is printed
Uses the same approach as my Python submission. Very new to 05AB1E so any tips on code or explaination greatly appreciated.
05AB1E, 15 bytes
EI!IN-!n›Ði–#]1
Try it online!
EI!IN-!n›Ði–#]1
E For loop with N from [1 ... input]
I! Push factorial of input
IN- Push input - N (x - n)
! Factorial
n Square
› Push input! > (input - N)^2 or x! > (x - n)^2
Ð Triplicate, used for three upcoming operations (if, print, end)
i If, run code after if top of stack is 1 (found minimum number of candies)
– Print N if top of stack is 1
#] Ends for loop and program is top of stack is 1
1 Pushes 1 so that if the input is 1 and we leave the for loop without
printing anything 1 is printed
Uses the same approach as my Python submission. Very new to 05AB1E so any tips on code or explaination greatly appreciated.
edited 4 hours ago
answered 5 hours ago
nedla2004
3211310
3211310
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%2f177495%2fhow-much-candy-can-you-eat%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
2
How much candy can I eat? All of it. All of the candy.
– AdmBorkBork
6 hours ago
New title: "How much candy need you eat?"
– Sparr
5 hours ago
1
It looks like
x=1
must be handled, could you confirm (since it's an edge case which should result in1
rather than0
or infinite loop as a couple of answers do)– Jonathan Allan
4 hours ago
@JonathanAllan yes,
x=1
must be handled, as it is within the bounds of0 < x! <= l
(because 1! is 1, which is greater than 0)– Skidsdev
4 hours ago