How much candy can you eat?











up vote
14
down vote

favorite
2












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 a positive integer in the range 0 < x! <= l where l 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 for x = 5 and n = 2, he will eat 4 and 5


Test cases



x = 1
n = 1
(1 > 0)

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!










share|improve this question




















  • 14




    How much candy can I eat? All of it. All of the candy.
    – AdmBorkBork
    Dec 12 at 21:10






  • 2




    New title: "How much candy need you eat?"
    – Sparr
    Dec 12 at 21:44










  • @Skidsdev Should x = 0 also be handled, since 0! = 1? (Perhaps x should also be specified as a Positive Integer?)
    – Chronocidal
    Dec 13 at 12:00










  • @Chronocidal added "positive" integer
    – Skidsdev
    Dec 13 at 14:07










  • I threw 10k pieces of candy on the ground. A little figure dug a hole into the ground and found a giant candy cavern because of me. ):
    – moonheart08
    Dec 13 at 14:54















up vote
14
down vote

favorite
2












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 a positive integer in the range 0 < x! <= l where l 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 for x = 5 and n = 2, he will eat 4 and 5


Test cases



x = 1
n = 1
(1 > 0)

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!










share|improve this question




















  • 14




    How much candy can I eat? All of it. All of the candy.
    – AdmBorkBork
    Dec 12 at 21:10






  • 2




    New title: "How much candy need you eat?"
    – Sparr
    Dec 12 at 21:44










  • @Skidsdev Should x = 0 also be handled, since 0! = 1? (Perhaps x should also be specified as a Positive Integer?)
    – Chronocidal
    Dec 13 at 12:00










  • @Chronocidal added "positive" integer
    – Skidsdev
    Dec 13 at 14:07










  • I threw 10k pieces of candy on the ground. A little figure dug a hole into the ground and found a giant candy cavern because of me. ):
    – moonheart08
    Dec 13 at 14:54













up vote
14
down vote

favorite
2









up vote
14
down vote

favorite
2






2





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 a positive integer in the range 0 < x! <= l where l 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 for x = 5 and n = 2, he will eat 4 and 5


Test cases



x = 1
n = 1
(1 > 0)

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!










share|improve this question















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 a positive integer in the range 0 < x! <= l where l 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 for x = 5 and n = 2, he will eat 4 and 5


Test cases



x = 1
n = 1
(1 > 0)

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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 13 at 14:06

























asked Dec 12 at 21:00









Skidsdev

6,2382972




6,2382972








  • 14




    How much candy can I eat? All of it. All of the candy.
    – AdmBorkBork
    Dec 12 at 21:10






  • 2




    New title: "How much candy need you eat?"
    – Sparr
    Dec 12 at 21:44










  • @Skidsdev Should x = 0 also be handled, since 0! = 1? (Perhaps x should also be specified as a Positive Integer?)
    – Chronocidal
    Dec 13 at 12:00










  • @Chronocidal added "positive" integer
    – Skidsdev
    Dec 13 at 14:07










  • I threw 10k pieces of candy on the ground. A little figure dug a hole into the ground and found a giant candy cavern because of me. ):
    – moonheart08
    Dec 13 at 14:54














  • 14




    How much candy can I eat? All of it. All of the candy.
    – AdmBorkBork
    Dec 12 at 21:10






  • 2




    New title: "How much candy need you eat?"
    – Sparr
    Dec 12 at 21:44










  • @Skidsdev Should x = 0 also be handled, since 0! = 1? (Perhaps x should also be specified as a Positive Integer?)
    – Chronocidal
    Dec 13 at 12:00










  • @Chronocidal added "positive" integer
    – Skidsdev
    Dec 13 at 14:07










  • I threw 10k pieces of candy on the ground. A little figure dug a hole into the ground and found a giant candy cavern because of me. ):
    – moonheart08
    Dec 13 at 14:54








14




14




How much candy can I eat? All of it. All of the candy.
– AdmBorkBork
Dec 12 at 21:10




How much candy can I eat? All of it. All of the candy.
– AdmBorkBork
Dec 12 at 21:10




2




2




New title: "How much candy need you eat?"
– Sparr
Dec 12 at 21:44




New title: "How much candy need you eat?"
– Sparr
Dec 12 at 21:44












@Skidsdev Should x = 0 also be handled, since 0! = 1? (Perhaps x should also be specified as a Positive Integer?)
– Chronocidal
Dec 13 at 12:00




@Skidsdev Should x = 0 also be handled, since 0! = 1? (Perhaps x should also be specified as a Positive Integer?)
– Chronocidal
Dec 13 at 12:00












@Chronocidal added "positive" integer
– Skidsdev
Dec 13 at 14:07




@Chronocidal added "positive" integer
– Skidsdev
Dec 13 at 14:07












I threw 10k pieces of candy on the ground. A little figure dug a hole into the ground and found a giant candy cavern because of me. ):
– moonheart08
Dec 13 at 14:54




I threw 10k pieces of candy on the ground. A little figure dug a hole into the ground and found a giant candy cavern because of me. ):
– moonheart08
Dec 13 at 14:54










18 Answers
18






active

oldest

votes

















up vote
8
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






share|improve this answer



















  • 1




    You can save 1 byte by replacing the top function with from math import factorial as F
    – Skidsdev
    Dec 12 at 21:31






  • 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
    Dec 12 at 21:37








  • 1




    All three of those are nice suggestions, thanks. (And added)
    – nedla2004
    Dec 12 at 21:40






  • 1




    -3 bytes with import math;F=math.factorial which I should probably go find the python tips meta to mention...
    – Sparr
    Dec 12 at 21:51








  • 2




    @Sparr: But F=lambda x:x<2or x*F(x-1) is three bytes less?
    – BMO
    Dec 12 at 21:58




















up vote
5
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!



Working range



Interestingly, the differences between the kids' products are always big enough that the loss of precision inherent to IEEE 754 encoding is not an issue.



As a result, it works for $0 le n le 170$. Beyond that, both the mantissa and the exponent overflow (yielding +Infinity) and we'd need BigInts (+1 byte).



How?



Let $p$ be the candy product of the other kid and let $q$ be our own candy product.




  1. We start with $p=n!$ (all the candy for the other kid) and $q=1$ (nothing for us).



  2. We repeat the following operations until $qge p$:




    • divide $p$ by $n$

    • multiply $q$ by $n$

    • decrement $n$




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





share|improve this answer






























    up vote
    4
    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





    share|improve this answer



















    • 1




      that's impressive but would be more educative if explained
      – Setop
      Dec 12 at 22:52










    • It will be... :)
      – Jonathan Allan
      Dec 12 at 22:53










    • @Setop - added.
      – Jonathan Allan
      Dec 12 at 23:13










    • like it ! and it must be fast compare to all solutions with tons of factorials
      – Setop
      Dec 12 at 23:17










    • Nah, still calculates all those products and factorials (more than some other solutions).
      – Jonathan Allan
      Dec 12 at 23:29


















    up vote
    3
    down vote














    APL (Dyalog Unicode), 10 bytes





    +/!≤2*⍨!∘⍳


    Try it online!



    Port of Dennis' answer. Thanks to, well, Dennis for it.



    How:



    +/!≤2*⍨!∘⍳ ⍝ Tacit function, takes 1 argument (E.g. 5)
    ⍳ ⍝ Range 1 2 3 4 5
    !∘ ⍝ Factorials. Yields 1 2 6 24 120
    2*⍨ ⍝ Squared. Yields 1 4 36 576 14400
    ! ⍝ Factorial of the argument. Yields 120.
    ≤ ⍝ Less than or equal to. Yields 0 0 0 1 1
    +/ ⍝ Sum the results, yielding 2.


    Since this answer wasn't strictly made by me, I'll keep my original answer below.






    APL (Dyalog Unicode), 14 12 bytes





    +/(!>×)∘⌽∘⍳


    Try it online!



    Prefix tacit function. Basically a Dyalog port of Jonathan's answer.



    Thanks to ngn and H.PWiz for the help in chat.



    Thanks to Dennis for pointing out that my original code was wrong. Turns out it saved me 2 bytes.



    Uses ⎕IO←0.



    How:



    +/(!>×)∘⌽∘⍳ ⍝ Tacit function, taking 1 argument (E.g. 5).
    ⍳ ⍝ Range 0 1 2 3 4
    ⌽∘ ⍝ Then reverse, yielding 4 3 2 1 0
    ( )∘ ⍝ Compose with (or: "use as argument for")
    ! ⍝ Factorial (of each element in the vector), yielding 24 6 2 1 1
    × ⍝ Multiply scan. Yields 4 12 24 24 0
    > ⍝ Is greater than. Yields 1 0 0 0 1
    +/ ⍝ Finally, sum the result, yielding 2.





    share|improve this answer























    • if +/ goes inside the parentheses, one the compositions can be omitted: (+/!>×)⌽∘⍳
      – ngn
      2 days ago


















    up vote
    3
    down vote














    R, 70 41 38 bytes



    -29 because Dennis knows all the internal functions



    -3 switching to scan() input





    sum(prod(x<-scan():1)<=cumprod(1:x)^2)


    Try it online!



    Pretty simple R implementation of nedla2004's Python3 answer.



    I feel like there's a cleaner implementation of the 1-handling, and I'd like to lose the curly-braces.



    I'm mad I didn't go back to using a which approach, madder that I used a strict less-than, but even madder still that I didn't know there's a cumprod() function. Great optimisation by Dennis.






    share|improve this answer






























      up vote
      2
      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!






      share|improve this answer






























        up vote
        2
        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.






        share|improve this answer






























          up vote
          2
          down vote














          Jelly, 7 bytes



          R!²<!ċ0


          Try it online!



          How it works



          R!²<!ċ0  Main link. Argument: n

          R Range; yield [1, ..., n].
          ! Map factorial over the range.
          ² Take the squares of the factorials.
          ! Compute the factorial of n.
          < Compare the squares with the factorial of n.
          ċ0 Count the number of zeroes.





          share|improve this answer






























            up vote
            2
            down vote














            Python 3, 183 176 149 bytes





            R=reversed
            def M(I,r=1):
            for i in I:r*=i;yield r
            def f(x):S=[*range(1,x+1)];return([n for n,a,b in zip([0]+S,R([*M(S)]),[0,*M(R(S))])if b>a]+[x])[0]


            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.



            -27 from Jo King






            share|improve this answer






























              up vote
              1
              down vote














              05AB1E, 15 11 bytes



              E!IN-!n›iNq


              Try it online!



              E!IN-!n›iNq

              E For loop with N from [1 ... input]
              ! Push factorial of input
              IN- Push input - N (x - n)
              ! Factorial
              n Square
              › Push input! > (input - N)^2 or x! > (x - n)^2
              i If, run code after if top of stack is 1 (found minimum number of candies)
              N Push N
              q Quit, and as nothing has been printed, N is implicitly printed


              Uses the same approach as my Python submission. Very new to 05AB1E so any tips on code or explaination greatly appreciated.



              -4 bytes thanks to Kevin Cruijssen






              share|improve this answer























              • Nice answer! You can golf 3 bytes like this without breaking input 1. If the if-statement is truthy, it will push index N to the stack and exit the program (outputting that index implicitly). For input 1 the if-statement will be falsey, but it will output its input 1 implicitly after that single-iteration loop.
                – Kevin Cruijssen
                Dec 13 at 9:35






              • 1




                Actually, 4 bytes can be saved instead of 3: Try it online 11 bytes. The input will be used implicitly for the first factorial !, now that the stack is empty since we no longer duplicate/triplicate the if-result.
                – Kevin Cruijssen
                Dec 13 at 9:45






              • 1




                Thanks for these ideas. Although I didn't get to this idea of printing at the end, I did think of ending the for loop early. After looking for break, end, quit and escape, I just thought I wasn't understanding the way loops work correctly. Somehow terminate never occured to me.
                – nedla2004
                Dec 13 at 23:18






              • 1




                Your answer was already pretty good. It's usually easier to golf an existing answer further, then to golf it yourself from nothing. If I would have done this challenge myself I probably would have ended up at 15 or 14 bytes as well. I used your idea of breaking and replaced it with a terminate and implicit output instead, after that I tried a few things, and in the end I saw I didn't need the duplicate anymore, which would also fix test case 1 outputting the input implicitly when the stack is empty. :)
                – Kevin Cruijssen
                2 days ago






              • 1




                FYI: I've posted a 7 bytes alternative by porting Dennis♦' Jelly answer. As always, Dennis♦ is able to perform magic in terms of Jelly code-golfing.. ;p
                – Kevin Cruijssen
                2 days ago


















              up vote
              0
              down vote














              Jelly, 14 bytes



              ạ‘rP>ạ!¥
              1ç1#«


              Try it online!



              Handles 1 correctly.






              share|improve this answer




























                up vote
                0
                down vote














                Charcoal, 20 bytes



                NθI⊕ΣEθ‹Π⊕…ιθ∨Π…¹⊕ι¹


                Try it online! Link is to verbose version of code. Explanation:



                Nθ                      Input `n`
                Σ Sum of
                θ `n`
                E Mapped over implicit range
                Π Product of
                ι Current value
                … Range to
                θ `n`
                ⊕ Incremented
                ‹ Less than
                Π Product of
                ¹ Literal 1
                … Range to
                ι Current value
                ⊕ Incremented
                ∨ Logical Or
                ¹ Literal 1
                ⊕ Incremented
                I Cast to string
                Implicitly print


                Product on an empty list in Charcoal returns None rather than 1, so I have to logically Or it.






                share|improve this answer





















                • Are you sure these characters are 8 bit each?
                  – RosLuP
                  Dec 13 at 20:37












                • @RosLuP Charcoal is one of many languages you might find here that uses a custom code page instead of, say, ASCII. This means that each eight-bit value is mapped to a custom symbol; these symbols are designed to help the programmer remember what each byte does a little easier than if they were just randomly dispersed among one of the standardized code pages. Feel free to ask for more details in the PPCG chat.
                  – Phlarx
                  Dec 13 at 21:14




















                up vote
                0
                down vote














                PHP, 107 bytes





                <?php $x=fgets(STDIN);function f($i){return $i==0?:$i*f($i-1);}$n=1;while(f($x)<f($x-$n)**2){$n++;}echo $n;


                Try it online!



                Uses the same $x^2>((x-1)!)^2$ method as others have used.



                Uses the factorial function from the PHP submission for this challenge (thanks to @donutdan4114)






                share|improve this answer




























                  up vote
                  0
                  down vote














                  Wolfram Language (Mathematica), 43 bytes



                  Min[n/.Solve[#!>(#-n)!^2, Integers]]/.n->1&


                  Try it online!






                  share|improve this answer




























                    up vote
                    0
                    down vote














                    05AB1E, 7 bytes



                    L!ns!@O


                    Port of Dennis♦' Jelly answer, so make sure to upvote him if you like this answer!



                    Try it online or verify all test cases.



                    Explanation:





                    L          # List in the range [1, (implicit) input]
                    ! # Take the factorial of each
                    n # Then square each
                    s! # Take the factorial of the input
                    @ # Check for each value in the list if they are larger than or equal to the
                    # input-faculty (1 if truthy; 0 if falsey)
                    O # Sum, so determine the amount of truthy checks (and output implicitly)





                    share|improve this answer




























                      up vote
                      0
                      down vote













                      Japt -x, 7 bytes



                      Port of Dennis' Jelly solution.



                      Only works in practice up to n=4 as we get into scientific notation above that.



                      õÊ®²¨U²


                      Try it



                      õ           :Range [1,input]
                      Ê :Factorial of each
                      ® :Map
                      ² : Square
                      ¨ : Greater than or equal to
                      U² : Input squared
                      :Implicitly reduce by addition





                      share|improve this answer




























                        up vote
                        0
                        down vote














                        C (gcc), 68 bytes





                        n;f(T x){T i=2,j=x,b=1,g=x;while(i<j)b*i>g?g*=--j:(b*=i++);n=x-j+1;}


                        Try it online!



                        Edit: shove a few obvious bytes



                        Edit 2: trading bytes against mults, no doing 2*x mults instead of x+n



                        Well I have this in C. Fails at 34 when T is long.



                        There is a possible ambiguity as to whether the good kid wants to always win or never lose... what do you think?






                        share|improve this answer










                        New contributor




                        Balzola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.

























                          up vote
                          0
                          down vote














                          C# (.NET Core), 93 bytes





                          n=>{int d(int k)=>k<2?1:k*d(k-1);int a=1,b=d(n),c=n;for(;;){a*=n;b/=n--;if(a>=b)return c-n;}}


                          Try it online!



                          Based off of @Arnauld's javascript answer.






                          share|improve this answer





















                            Your Answer





                            StackExchange.ifUsing("editor", function () {
                            return StackExchange.using("mathjaxEditing", function () {
                            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
                            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
                            });
                            });
                            }, "mathjax-editing");

                            StackExchange.ifUsing("editor", function () {
                            StackExchange.using("externalEditor", function () {
                            StackExchange.using("snippets", function () {
                            StackExchange.snippets.init();
                            });
                            });
                            }, "code-snippets");

                            StackExchange.ready(function() {
                            var channelOptions = {
                            tags: "".split(" "),
                            id: "200"
                            };
                            initTagRenderer("".split(" "), "".split(" "), channelOptions);

                            StackExchange.using("externalEditor", function() {
                            // Have to fire editor after snippets, if snippets enabled
                            if (StackExchange.settings.snippets.snippetsEnabled) {
                            StackExchange.using("snippets", function() {
                            createEditor();
                            });
                            }
                            else {
                            createEditor();
                            }
                            });

                            function createEditor() {
                            StackExchange.prepareEditor({
                            heartbeatType: 'answer',
                            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
                            });


                            }
                            });














                            draft saved

                            draft discarded


















                            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

























                            18 Answers
                            18






                            active

                            oldest

                            votes








                            18 Answers
                            18






                            active

                            oldest

                            votes









                            active

                            oldest

                            votes






                            active

                            oldest

                            votes








                            up vote
                            8
                            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






                            share|improve this answer



















                            • 1




                              You can save 1 byte by replacing the top function with from math import factorial as F
                              – Skidsdev
                              Dec 12 at 21:31






                            • 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
                              Dec 12 at 21:37








                            • 1




                              All three of those are nice suggestions, thanks. (And added)
                              – nedla2004
                              Dec 12 at 21:40






                            • 1




                              -3 bytes with import math;F=math.factorial which I should probably go find the python tips meta to mention...
                              – Sparr
                              Dec 12 at 21:51








                            • 2




                              @Sparr: But F=lambda x:x<2or x*F(x-1) is three bytes less?
                              – BMO
                              Dec 12 at 21:58

















                            up vote
                            8
                            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






                            share|improve this answer



















                            • 1




                              You can save 1 byte by replacing the top function with from math import factorial as F
                              – Skidsdev
                              Dec 12 at 21:31






                            • 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
                              Dec 12 at 21:37








                            • 1




                              All three of those are nice suggestions, thanks. (And added)
                              – nedla2004
                              Dec 12 at 21:40






                            • 1




                              -3 bytes with import math;F=math.factorial which I should probably go find the python tips meta to mention...
                              – Sparr
                              Dec 12 at 21:51








                            • 2




                              @Sparr: But F=lambda x:x<2or x*F(x-1) is three bytes less?
                              – BMO
                              Dec 12 at 21:58















                            up vote
                            8
                            down vote










                            up vote
                            8
                            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






                            share|improve this answer















                            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







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 12 at 23:03

























                            answered Dec 12 at 21:24









                            nedla2004

                            3911310




                            3911310








                            • 1




                              You can save 1 byte by replacing the top function with from math import factorial as F
                              – Skidsdev
                              Dec 12 at 21:31






                            • 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
                              Dec 12 at 21:37








                            • 1




                              All three of those are nice suggestions, thanks. (And added)
                              – nedla2004
                              Dec 12 at 21:40






                            • 1




                              -3 bytes with import math;F=math.factorial which I should probably go find the python tips meta to mention...
                              – Sparr
                              Dec 12 at 21:51








                            • 2




                              @Sparr: But F=lambda x:x<2or x*F(x-1) is three bytes less?
                              – BMO
                              Dec 12 at 21:58
















                            • 1




                              You can save 1 byte by replacing the top function with from math import factorial as F
                              – Skidsdev
                              Dec 12 at 21:31






                            • 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
                              Dec 12 at 21:37








                            • 1




                              All three of those are nice suggestions, thanks. (And added)
                              – nedla2004
                              Dec 12 at 21:40






                            • 1




                              -3 bytes with import math;F=math.factorial which I should probably go find the python tips meta to mention...
                              – Sparr
                              Dec 12 at 21:51








                            • 2




                              @Sparr: But F=lambda x:x<2or x*F(x-1) is three bytes less?
                              – BMO
                              Dec 12 at 21:58










                            1




                            1




                            You can save 1 byte by replacing the top function with from math import factorial as F
                            – Skidsdev
                            Dec 12 at 21:31




                            You can save 1 byte by replacing the top function with from math import factorial as F
                            – Skidsdev
                            Dec 12 at 21:31




                            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
                            Dec 12 at 21:37






                            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
                            Dec 12 at 21:37






                            1




                            1




                            All three of those are nice suggestions, thanks. (And added)
                            – nedla2004
                            Dec 12 at 21:40




                            All three of those are nice suggestions, thanks. (And added)
                            – nedla2004
                            Dec 12 at 21:40




                            1




                            1




                            -3 bytes with import math;F=math.factorial which I should probably go find the python tips meta to mention...
                            – Sparr
                            Dec 12 at 21:51






                            -3 bytes with import math;F=math.factorial which I should probably go find the python tips meta to mention...
                            – Sparr
                            Dec 12 at 21:51






                            2




                            2




                            @Sparr: But F=lambda x:x<2or x*F(x-1) is three bytes less?
                            – BMO
                            Dec 12 at 21:58






                            @Sparr: But F=lambda x:x<2or x*F(x-1) is three bytes less?
                            – BMO
                            Dec 12 at 21:58












                            up vote
                            5
                            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!



                            Working range



                            Interestingly, the differences between the kids' products are always big enough that the loss of precision inherent to IEEE 754 encoding is not an issue.



                            As a result, it works for $0 le n le 170$. Beyond that, both the mantissa and the exponent overflow (yielding +Infinity) and we'd need BigInts (+1 byte).



                            How?



                            Let $p$ be the candy product of the other kid and let $q$ be our own candy product.




                            1. We start with $p=n!$ (all the candy for the other kid) and $q=1$ (nothing for us).



                            2. We repeat the following operations until $qge p$:




                              • divide $p$ by $n$

                              • multiply $q$ by $n$

                              • decrement $n$




                            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





                            share|improve this answer



























                              up vote
                              5
                              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!



                              Working range



                              Interestingly, the differences between the kids' products are always big enough that the loss of precision inherent to IEEE 754 encoding is not an issue.



                              As a result, it works for $0 le n le 170$. Beyond that, both the mantissa and the exponent overflow (yielding +Infinity) and we'd need BigInts (+1 byte).



                              How?



                              Let $p$ be the candy product of the other kid and let $q$ be our own candy product.




                              1. We start with $p=n!$ (all the candy for the other kid) and $q=1$ (nothing for us).



                              2. We repeat the following operations until $qge p$:




                                • divide $p$ by $n$

                                • multiply $q$ by $n$

                                • decrement $n$




                              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





                              share|improve this answer

























                                up vote
                                5
                                down vote










                                up vote
                                5
                                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!



                                Working range



                                Interestingly, the differences between the kids' products are always big enough that the loss of precision inherent to IEEE 754 encoding is not an issue.



                                As a result, it works for $0 le n le 170$. Beyond that, both the mantissa and the exponent overflow (yielding +Infinity) and we'd need BigInts (+1 byte).



                                How?



                                Let $p$ be the candy product of the other kid and let $q$ be our own candy product.




                                1. We start with $p=n!$ (all the candy for the other kid) and $q=1$ (nothing for us).



                                2. We repeat the following operations until $qge p$:




                                  • divide $p$ by $n$

                                  • multiply $q$ by $n$

                                  • decrement $n$




                                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





                                share|improve this answer














                                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!



                                Working range



                                Interestingly, the differences between the kids' products are always big enough that the loss of precision inherent to IEEE 754 encoding is not an issue.



                                As a result, it works for $0 le n le 170$. Beyond that, both the mantissa and the exponent overflow (yielding +Infinity) and we'd need BigInts (+1 byte).



                                How?



                                Let $p$ be the candy product of the other kid and let $q$ be our own candy product.




                                1. We start with $p=n!$ (all the candy for the other kid) and $q=1$ (nothing for us).



                                2. We repeat the following operations until $qge p$:




                                  • divide $p$ by $n$

                                  • multiply $q$ by $n$

                                  • decrement $n$




                                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






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Dec 13 at 12:54

























                                answered Dec 12 at 21:55









                                Arnauld

                                71.7k688299




                                71.7k688299






















                                    up vote
                                    4
                                    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





                                    share|improve this answer



















                                    • 1




                                      that's impressive but would be more educative if explained
                                      – Setop
                                      Dec 12 at 22:52










                                    • It will be... :)
                                      – Jonathan Allan
                                      Dec 12 at 22:53










                                    • @Setop - added.
                                      – Jonathan Allan
                                      Dec 12 at 23:13










                                    • like it ! and it must be fast compare to all solutions with tons of factorials
                                      – Setop
                                      Dec 12 at 23:17










                                    • Nah, still calculates all those products and factorials (more than some other solutions).
                                      – Jonathan Allan
                                      Dec 12 at 23:29















                                    up vote
                                    4
                                    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





                                    share|improve this answer



















                                    • 1




                                      that's impressive but would be more educative if explained
                                      – Setop
                                      Dec 12 at 22:52










                                    • It will be... :)
                                      – Jonathan Allan
                                      Dec 12 at 22:53










                                    • @Setop - added.
                                      – Jonathan Allan
                                      Dec 12 at 23:13










                                    • like it ! and it must be fast compare to all solutions with tons of factorials
                                      – Setop
                                      Dec 12 at 23:17










                                    • Nah, still calculates all those products and factorials (more than some other solutions).
                                      – Jonathan Allan
                                      Dec 12 at 23:29













                                    up vote
                                    4
                                    down vote










                                    up vote
                                    4
                                    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





                                    share|improve this answer















                                    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






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Dec 12 at 23:42

























                                    answered Dec 12 at 22:50









                                    Jonathan Allan

                                    50.6k534165




                                    50.6k534165








                                    • 1




                                      that's impressive but would be more educative if explained
                                      – Setop
                                      Dec 12 at 22:52










                                    • It will be... :)
                                      – Jonathan Allan
                                      Dec 12 at 22:53










                                    • @Setop - added.
                                      – Jonathan Allan
                                      Dec 12 at 23:13










                                    • like it ! and it must be fast compare to all solutions with tons of factorials
                                      – Setop
                                      Dec 12 at 23:17










                                    • Nah, still calculates all those products and factorials (more than some other solutions).
                                      – Jonathan Allan
                                      Dec 12 at 23:29














                                    • 1




                                      that's impressive but would be more educative if explained
                                      – Setop
                                      Dec 12 at 22:52










                                    • It will be... :)
                                      – Jonathan Allan
                                      Dec 12 at 22:53










                                    • @Setop - added.
                                      – Jonathan Allan
                                      Dec 12 at 23:13










                                    • like it ! and it must be fast compare to all solutions with tons of factorials
                                      – Setop
                                      Dec 12 at 23:17










                                    • Nah, still calculates all those products and factorials (more than some other solutions).
                                      – Jonathan Allan
                                      Dec 12 at 23:29








                                    1




                                    1




                                    that's impressive but would be more educative if explained
                                    – Setop
                                    Dec 12 at 22:52




                                    that's impressive but would be more educative if explained
                                    – Setop
                                    Dec 12 at 22:52












                                    It will be... :)
                                    – Jonathan Allan
                                    Dec 12 at 22:53




                                    It will be... :)
                                    – Jonathan Allan
                                    Dec 12 at 22:53












                                    @Setop - added.
                                    – Jonathan Allan
                                    Dec 12 at 23:13




                                    @Setop - added.
                                    – Jonathan Allan
                                    Dec 12 at 23:13












                                    like it ! and it must be fast compare to all solutions with tons of factorials
                                    – Setop
                                    Dec 12 at 23:17




                                    like it ! and it must be fast compare to all solutions with tons of factorials
                                    – Setop
                                    Dec 12 at 23:17












                                    Nah, still calculates all those products and factorials (more than some other solutions).
                                    – Jonathan Allan
                                    Dec 12 at 23:29




                                    Nah, still calculates all those products and factorials (more than some other solutions).
                                    – Jonathan Allan
                                    Dec 12 at 23:29










                                    up vote
                                    3
                                    down vote














                                    APL (Dyalog Unicode), 10 bytes





                                    +/!≤2*⍨!∘⍳


                                    Try it online!



                                    Port of Dennis' answer. Thanks to, well, Dennis for it.



                                    How:



                                    +/!≤2*⍨!∘⍳ ⍝ Tacit function, takes 1 argument (E.g. 5)
                                    ⍳ ⍝ Range 1 2 3 4 5
                                    !∘ ⍝ Factorials. Yields 1 2 6 24 120
                                    2*⍨ ⍝ Squared. Yields 1 4 36 576 14400
                                    ! ⍝ Factorial of the argument. Yields 120.
                                    ≤ ⍝ Less than or equal to. Yields 0 0 0 1 1
                                    +/ ⍝ Sum the results, yielding 2.


                                    Since this answer wasn't strictly made by me, I'll keep my original answer below.






                                    APL (Dyalog Unicode), 14 12 bytes





                                    +/(!>×)∘⌽∘⍳


                                    Try it online!



                                    Prefix tacit function. Basically a Dyalog port of Jonathan's answer.



                                    Thanks to ngn and H.PWiz for the help in chat.



                                    Thanks to Dennis for pointing out that my original code was wrong. Turns out it saved me 2 bytes.



                                    Uses ⎕IO←0.



                                    How:



                                    +/(!>×)∘⌽∘⍳ ⍝ Tacit function, taking 1 argument (E.g. 5).
                                    ⍳ ⍝ Range 0 1 2 3 4
                                    ⌽∘ ⍝ Then reverse, yielding 4 3 2 1 0
                                    ( )∘ ⍝ Compose with (or: "use as argument for")
                                    ! ⍝ Factorial (of each element in the vector), yielding 24 6 2 1 1
                                    × ⍝ Multiply scan. Yields 4 12 24 24 0
                                    > ⍝ Is greater than. Yields 1 0 0 0 1
                                    +/ ⍝ Finally, sum the result, yielding 2.





                                    share|improve this answer























                                    • if +/ goes inside the parentheses, one the compositions can be omitted: (+/!>×)⌽∘⍳
                                      – ngn
                                      2 days ago















                                    up vote
                                    3
                                    down vote














                                    APL (Dyalog Unicode), 10 bytes





                                    +/!≤2*⍨!∘⍳


                                    Try it online!



                                    Port of Dennis' answer. Thanks to, well, Dennis for it.



                                    How:



                                    +/!≤2*⍨!∘⍳ ⍝ Tacit function, takes 1 argument (E.g. 5)
                                    ⍳ ⍝ Range 1 2 3 4 5
                                    !∘ ⍝ Factorials. Yields 1 2 6 24 120
                                    2*⍨ ⍝ Squared. Yields 1 4 36 576 14400
                                    ! ⍝ Factorial of the argument. Yields 120.
                                    ≤ ⍝ Less than or equal to. Yields 0 0 0 1 1
                                    +/ ⍝ Sum the results, yielding 2.


                                    Since this answer wasn't strictly made by me, I'll keep my original answer below.






                                    APL (Dyalog Unicode), 14 12 bytes





                                    +/(!>×)∘⌽∘⍳


                                    Try it online!



                                    Prefix tacit function. Basically a Dyalog port of Jonathan's answer.



                                    Thanks to ngn and H.PWiz for the help in chat.



                                    Thanks to Dennis for pointing out that my original code was wrong. Turns out it saved me 2 bytes.



                                    Uses ⎕IO←0.



                                    How:



                                    +/(!>×)∘⌽∘⍳ ⍝ Tacit function, taking 1 argument (E.g. 5).
                                    ⍳ ⍝ Range 0 1 2 3 4
                                    ⌽∘ ⍝ Then reverse, yielding 4 3 2 1 0
                                    ( )∘ ⍝ Compose with (or: "use as argument for")
                                    ! ⍝ Factorial (of each element in the vector), yielding 24 6 2 1 1
                                    × ⍝ Multiply scan. Yields 4 12 24 24 0
                                    > ⍝ Is greater than. Yields 1 0 0 0 1
                                    +/ ⍝ Finally, sum the result, yielding 2.





                                    share|improve this answer























                                    • if +/ goes inside the parentheses, one the compositions can be omitted: (+/!>×)⌽∘⍳
                                      – ngn
                                      2 days ago













                                    up vote
                                    3
                                    down vote










                                    up vote
                                    3
                                    down vote










                                    APL (Dyalog Unicode), 10 bytes





                                    +/!≤2*⍨!∘⍳


                                    Try it online!



                                    Port of Dennis' answer. Thanks to, well, Dennis for it.



                                    How:



                                    +/!≤2*⍨!∘⍳ ⍝ Tacit function, takes 1 argument (E.g. 5)
                                    ⍳ ⍝ Range 1 2 3 4 5
                                    !∘ ⍝ Factorials. Yields 1 2 6 24 120
                                    2*⍨ ⍝ Squared. Yields 1 4 36 576 14400
                                    ! ⍝ Factorial of the argument. Yields 120.
                                    ≤ ⍝ Less than or equal to. Yields 0 0 0 1 1
                                    +/ ⍝ Sum the results, yielding 2.


                                    Since this answer wasn't strictly made by me, I'll keep my original answer below.






                                    APL (Dyalog Unicode), 14 12 bytes





                                    +/(!>×)∘⌽∘⍳


                                    Try it online!



                                    Prefix tacit function. Basically a Dyalog port of Jonathan's answer.



                                    Thanks to ngn and H.PWiz for the help in chat.



                                    Thanks to Dennis for pointing out that my original code was wrong. Turns out it saved me 2 bytes.



                                    Uses ⎕IO←0.



                                    How:



                                    +/(!>×)∘⌽∘⍳ ⍝ Tacit function, taking 1 argument (E.g. 5).
                                    ⍳ ⍝ Range 0 1 2 3 4
                                    ⌽∘ ⍝ Then reverse, yielding 4 3 2 1 0
                                    ( )∘ ⍝ Compose with (or: "use as argument for")
                                    ! ⍝ Factorial (of each element in the vector), yielding 24 6 2 1 1
                                    × ⍝ Multiply scan. Yields 4 12 24 24 0
                                    > ⍝ Is greater than. Yields 1 0 0 0 1
                                    +/ ⍝ Finally, sum the result, yielding 2.





                                    share|improve this answer















                                    APL (Dyalog Unicode), 10 bytes





                                    +/!≤2*⍨!∘⍳


                                    Try it online!



                                    Port of Dennis' answer. Thanks to, well, Dennis for it.



                                    How:



                                    +/!≤2*⍨!∘⍳ ⍝ Tacit function, takes 1 argument (E.g. 5)
                                    ⍳ ⍝ Range 1 2 3 4 5
                                    !∘ ⍝ Factorials. Yields 1 2 6 24 120
                                    2*⍨ ⍝ Squared. Yields 1 4 36 576 14400
                                    ! ⍝ Factorial of the argument. Yields 120.
                                    ≤ ⍝ Less than or equal to. Yields 0 0 0 1 1
                                    +/ ⍝ Sum the results, yielding 2.


                                    Since this answer wasn't strictly made by me, I'll keep my original answer below.






                                    APL (Dyalog Unicode), 14 12 bytes





                                    +/(!>×)∘⌽∘⍳


                                    Try it online!



                                    Prefix tacit function. Basically a Dyalog port of Jonathan's answer.



                                    Thanks to ngn and H.PWiz for the help in chat.



                                    Thanks to Dennis for pointing out that my original code was wrong. Turns out it saved me 2 bytes.



                                    Uses ⎕IO←0.



                                    How:



                                    +/(!>×)∘⌽∘⍳ ⍝ Tacit function, taking 1 argument (E.g. 5).
                                    ⍳ ⍝ Range 0 1 2 3 4
                                    ⌽∘ ⍝ Then reverse, yielding 4 3 2 1 0
                                    ( )∘ ⍝ Compose with (or: "use as argument for")
                                    ! ⍝ Factorial (of each element in the vector), yielding 24 6 2 1 1
                                    × ⍝ Multiply scan. Yields 4 12 24 24 0
                                    > ⍝ Is greater than. Yields 1 0 0 0 1
                                    +/ ⍝ Finally, sum the result, yielding 2.






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Dec 13 at 15:03

























                                    answered Dec 13 at 13:54









                                    J. Sallé

                                    1,903322




                                    1,903322












                                    • if +/ goes inside the parentheses, one the compositions can be omitted: (+/!>×)⌽∘⍳
                                      – ngn
                                      2 days ago


















                                    • if +/ goes inside the parentheses, one the compositions can be omitted: (+/!>×)⌽∘⍳
                                      – ngn
                                      2 days ago
















                                    if +/ goes inside the parentheses, one the compositions can be omitted: (+/!>×)⌽∘⍳
                                    – ngn
                                    2 days ago




                                    if +/ goes inside the parentheses, one the compositions can be omitted: (+/!>×)⌽∘⍳
                                    – ngn
                                    2 days ago










                                    up vote
                                    3
                                    down vote














                                    R, 70 41 38 bytes



                                    -29 because Dennis knows all the internal functions



                                    -3 switching to scan() input





                                    sum(prod(x<-scan():1)<=cumprod(1:x)^2)


                                    Try it online!



                                    Pretty simple R implementation of nedla2004's Python3 answer.



                                    I feel like there's a cleaner implementation of the 1-handling, and I'd like to lose the curly-braces.



                                    I'm mad I didn't go back to using a which approach, madder that I used a strict less-than, but even madder still that I didn't know there's a cumprod() function. Great optimisation by Dennis.






                                    share|improve this answer



























                                      up vote
                                      3
                                      down vote














                                      R, 70 41 38 bytes



                                      -29 because Dennis knows all the internal functions



                                      -3 switching to scan() input





                                      sum(prod(x<-scan():1)<=cumprod(1:x)^2)


                                      Try it online!



                                      Pretty simple R implementation of nedla2004's Python3 answer.



                                      I feel like there's a cleaner implementation of the 1-handling, and I'd like to lose the curly-braces.



                                      I'm mad I didn't go back to using a which approach, madder that I used a strict less-than, but even madder still that I didn't know there's a cumprod() function. Great optimisation by Dennis.






                                      share|improve this answer

























                                        up vote
                                        3
                                        down vote










                                        up vote
                                        3
                                        down vote










                                        R, 70 41 38 bytes



                                        -29 because Dennis knows all the internal functions



                                        -3 switching to scan() input





                                        sum(prod(x<-scan():1)<=cumprod(1:x)^2)


                                        Try it online!



                                        Pretty simple R implementation of nedla2004's Python3 answer.



                                        I feel like there's a cleaner implementation of the 1-handling, and I'd like to lose the curly-braces.



                                        I'm mad I didn't go back to using a which approach, madder that I used a strict less-than, but even madder still that I didn't know there's a cumprod() function. Great optimisation by Dennis.






                                        share|improve this answer















                                        R, 70 41 38 bytes



                                        -29 because Dennis knows all the internal functions



                                        -3 switching to scan() input





                                        sum(prod(x<-scan():1)<=cumprod(1:x)^2)


                                        Try it online!



                                        Pretty simple R implementation of nedla2004's Python3 answer.



                                        I feel like there's a cleaner implementation of the 1-handling, and I'd like to lose the curly-braces.



                                        I'm mad I didn't go back to using a which approach, madder that I used a strict less-than, but even madder still that I didn't know there's a cumprod() function. Great optimisation by Dennis.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited 2 days ago

























                                        answered Dec 13 at 15:43









                                        CriminallyVulgar

                                        1715




                                        1715






















                                            up vote
                                            2
                                            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!






                                            share|improve this answer



























                                              up vote
                                              2
                                              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!






                                              share|improve this answer

























                                                up vote
                                                2
                                                down vote










                                                up vote
                                                2
                                                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!






                                                share|improve this answer















                                                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!







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Dec 12 at 21:42

























                                                answered Dec 12 at 21:37









                                                flawr

                                                26.6k664187




                                                26.6k664187






















                                                    up vote
                                                    2
                                                    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.






                                                    share|improve this answer



























                                                      up vote
                                                      2
                                                      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.






                                                      share|improve this answer

























                                                        up vote
                                                        2
                                                        down vote










                                                        up vote
                                                        2
                                                        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.






                                                        share|improve this answer















                                                        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.







                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited Dec 12 at 23:08

























                                                        answered Dec 12 at 22:29









                                                        Οurous

                                                        6,31811032




                                                        6,31811032






















                                                            up vote
                                                            2
                                                            down vote














                                                            Jelly, 7 bytes



                                                            R!²<!ċ0


                                                            Try it online!



                                                            How it works



                                                            R!²<!ċ0  Main link. Argument: n

                                                            R Range; yield [1, ..., n].
                                                            ! Map factorial over the range.
                                                            ² Take the squares of the factorials.
                                                            ! Compute the factorial of n.
                                                            < Compare the squares with the factorial of n.
                                                            ċ0 Count the number of zeroes.





                                                            share|improve this answer



























                                                              up vote
                                                              2
                                                              down vote














                                                              Jelly, 7 bytes



                                                              R!²<!ċ0


                                                              Try it online!



                                                              How it works



                                                              R!²<!ċ0  Main link. Argument: n

                                                              R Range; yield [1, ..., n].
                                                              ! Map factorial over the range.
                                                              ² Take the squares of the factorials.
                                                              ! Compute the factorial of n.
                                                              < Compare the squares with the factorial of n.
                                                              ċ0 Count the number of zeroes.





                                                              share|improve this answer

























                                                                up vote
                                                                2
                                                                down vote










                                                                up vote
                                                                2
                                                                down vote










                                                                Jelly, 7 bytes



                                                                R!²<!ċ0


                                                                Try it online!



                                                                How it works



                                                                R!²<!ċ0  Main link. Argument: n

                                                                R Range; yield [1, ..., n].
                                                                ! Map factorial over the range.
                                                                ² Take the squares of the factorials.
                                                                ! Compute the factorial of n.
                                                                < Compare the squares with the factorial of n.
                                                                ċ0 Count the number of zeroes.





                                                                share|improve this answer















                                                                Jelly, 7 bytes



                                                                R!²<!ċ0


                                                                Try it online!



                                                                How it works



                                                                R!²<!ċ0  Main link. Argument: n

                                                                R Range; yield [1, ..., n].
                                                                ! Map factorial over the range.
                                                                ² Take the squares of the factorials.
                                                                ! Compute the factorial of n.
                                                                < Compare the squares with the factorial of n.
                                                                ċ0 Count the number of zeroes.






                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited Dec 13 at 14:02

























                                                                answered Dec 13 at 13:50









                                                                Dennis

                                                                186k32295735




                                                                186k32295735






















                                                                    up vote
                                                                    2
                                                                    down vote














                                                                    Python 3, 183 176 149 bytes





                                                                    R=reversed
                                                                    def M(I,r=1):
                                                                    for i in I:r*=i;yield r
                                                                    def f(x):S=[*range(1,x+1)];return([n for n,a,b in zip([0]+S,R([*M(S)]),[0,*M(R(S))])if b>a]+[x])[0]


                                                                    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.



                                                                    -27 from Jo King






                                                                    share|improve this answer



























                                                                      up vote
                                                                      2
                                                                      down vote














                                                                      Python 3, 183 176 149 bytes





                                                                      R=reversed
                                                                      def M(I,r=1):
                                                                      for i in I:r*=i;yield r
                                                                      def f(x):S=[*range(1,x+1)];return([n for n,a,b in zip([0]+S,R([*M(S)]),[0,*M(R(S))])if b>a]+[x])[0]


                                                                      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.



                                                                      -27 from Jo King






                                                                      share|improve this answer

























                                                                        up vote
                                                                        2
                                                                        down vote










                                                                        up vote
                                                                        2
                                                                        down vote










                                                                        Python 3, 183 176 149 bytes





                                                                        R=reversed
                                                                        def M(I,r=1):
                                                                        for i in I:r*=i;yield r
                                                                        def f(x):S=[*range(1,x+1)];return([n for n,a,b in zip([0]+S,R([*M(S)]),[0,*M(R(S))])if b>a]+[x])[0]


                                                                        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.



                                                                        -27 from Jo King






                                                                        share|improve this answer















                                                                        Python 3, 183 176 149 bytes





                                                                        R=reversed
                                                                        def M(I,r=1):
                                                                        for i in I:r*=i;yield r
                                                                        def f(x):S=[*range(1,x+1)];return([n for n,a,b in zip([0]+S,R([*M(S)]),[0,*M(R(S))])if b>a]+[x])[0]


                                                                        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.



                                                                        -27 from Jo King







                                                                        share|improve this answer














                                                                        share|improve this answer



                                                                        share|improve this answer








                                                                        edited Dec 13 at 22:28

























                                                                        answered Dec 13 at 0:27









                                                                        Setop

                                                                        1686




                                                                        1686






















                                                                            up vote
                                                                            1
                                                                            down vote














                                                                            05AB1E, 15 11 bytes



                                                                            E!IN-!n›iNq


                                                                            Try it online!



                                                                            E!IN-!n›iNq

                                                                            E For loop with N from [1 ... input]
                                                                            ! Push factorial of input
                                                                            IN- Push input - N (x - n)
                                                                            ! Factorial
                                                                            n Square
                                                                            › Push input! > (input - N)^2 or x! > (x - n)^2
                                                                            i If, run code after if top of stack is 1 (found minimum number of candies)
                                                                            N Push N
                                                                            q Quit, and as nothing has been printed, N is implicitly printed


                                                                            Uses the same approach as my Python submission. Very new to 05AB1E so any tips on code or explaination greatly appreciated.



                                                                            -4 bytes thanks to Kevin Cruijssen






                                                                            share|improve this answer























                                                                            • Nice answer! You can golf 3 bytes like this without breaking input 1. If the if-statement is truthy, it will push index N to the stack and exit the program (outputting that index implicitly). For input 1 the if-statement will be falsey, but it will output its input 1 implicitly after that single-iteration loop.
                                                                              – Kevin Cruijssen
                                                                              Dec 13 at 9:35






                                                                            • 1




                                                                              Actually, 4 bytes can be saved instead of 3: Try it online 11 bytes. The input will be used implicitly for the first factorial !, now that the stack is empty since we no longer duplicate/triplicate the if-result.
                                                                              – Kevin Cruijssen
                                                                              Dec 13 at 9:45






                                                                            • 1




                                                                              Thanks for these ideas. Although I didn't get to this idea of printing at the end, I did think of ending the for loop early. After looking for break, end, quit and escape, I just thought I wasn't understanding the way loops work correctly. Somehow terminate never occured to me.
                                                                              – nedla2004
                                                                              Dec 13 at 23:18






                                                                            • 1




                                                                              Your answer was already pretty good. It's usually easier to golf an existing answer further, then to golf it yourself from nothing. If I would have done this challenge myself I probably would have ended up at 15 or 14 bytes as well. I used your idea of breaking and replaced it with a terminate and implicit output instead, after that I tried a few things, and in the end I saw I didn't need the duplicate anymore, which would also fix test case 1 outputting the input implicitly when the stack is empty. :)
                                                                              – Kevin Cruijssen
                                                                              2 days ago






                                                                            • 1




                                                                              FYI: I've posted a 7 bytes alternative by porting Dennis♦' Jelly answer. As always, Dennis♦ is able to perform magic in terms of Jelly code-golfing.. ;p
                                                                              – Kevin Cruijssen
                                                                              2 days ago















                                                                            up vote
                                                                            1
                                                                            down vote














                                                                            05AB1E, 15 11 bytes



                                                                            E!IN-!n›iNq


                                                                            Try it online!



                                                                            E!IN-!n›iNq

                                                                            E For loop with N from [1 ... input]
                                                                            ! Push factorial of input
                                                                            IN- Push input - N (x - n)
                                                                            ! Factorial
                                                                            n Square
                                                                            › Push input! > (input - N)^2 or x! > (x - n)^2
                                                                            i If, run code after if top of stack is 1 (found minimum number of candies)
                                                                            N Push N
                                                                            q Quit, and as nothing has been printed, N is implicitly printed


                                                                            Uses the same approach as my Python submission. Very new to 05AB1E so any tips on code or explaination greatly appreciated.



                                                                            -4 bytes thanks to Kevin Cruijssen






                                                                            share|improve this answer























                                                                            • Nice answer! You can golf 3 bytes like this without breaking input 1. If the if-statement is truthy, it will push index N to the stack and exit the program (outputting that index implicitly). For input 1 the if-statement will be falsey, but it will output its input 1 implicitly after that single-iteration loop.
                                                                              – Kevin Cruijssen
                                                                              Dec 13 at 9:35






                                                                            • 1




                                                                              Actually, 4 bytes can be saved instead of 3: Try it online 11 bytes. The input will be used implicitly for the first factorial !, now that the stack is empty since we no longer duplicate/triplicate the if-result.
                                                                              – Kevin Cruijssen
                                                                              Dec 13 at 9:45






                                                                            • 1




                                                                              Thanks for these ideas. Although I didn't get to this idea of printing at the end, I did think of ending the for loop early. After looking for break, end, quit and escape, I just thought I wasn't understanding the way loops work correctly. Somehow terminate never occured to me.
                                                                              – nedla2004
                                                                              Dec 13 at 23:18






                                                                            • 1




                                                                              Your answer was already pretty good. It's usually easier to golf an existing answer further, then to golf it yourself from nothing. If I would have done this challenge myself I probably would have ended up at 15 or 14 bytes as well. I used your idea of breaking and replaced it with a terminate and implicit output instead, after that I tried a few things, and in the end I saw I didn't need the duplicate anymore, which would also fix test case 1 outputting the input implicitly when the stack is empty. :)
                                                                              – Kevin Cruijssen
                                                                              2 days ago






                                                                            • 1




                                                                              FYI: I've posted a 7 bytes alternative by porting Dennis♦' Jelly answer. As always, Dennis♦ is able to perform magic in terms of Jelly code-golfing.. ;p
                                                                              – Kevin Cruijssen
                                                                              2 days ago













                                                                            up vote
                                                                            1
                                                                            down vote










                                                                            up vote
                                                                            1
                                                                            down vote










                                                                            05AB1E, 15 11 bytes



                                                                            E!IN-!n›iNq


                                                                            Try it online!



                                                                            E!IN-!n›iNq

                                                                            E For loop with N from [1 ... input]
                                                                            ! Push factorial of input
                                                                            IN- Push input - N (x - n)
                                                                            ! Factorial
                                                                            n Square
                                                                            › Push input! > (input - N)^2 or x! > (x - n)^2
                                                                            i If, run code after if top of stack is 1 (found minimum number of candies)
                                                                            N Push N
                                                                            q Quit, and as nothing has been printed, N is implicitly printed


                                                                            Uses the same approach as my Python submission. Very new to 05AB1E so any tips on code or explaination greatly appreciated.



                                                                            -4 bytes thanks to Kevin Cruijssen






                                                                            share|improve this answer















                                                                            05AB1E, 15 11 bytes



                                                                            E!IN-!n›iNq


                                                                            Try it online!



                                                                            E!IN-!n›iNq

                                                                            E For loop with N from [1 ... input]
                                                                            ! Push factorial of input
                                                                            IN- Push input - N (x - n)
                                                                            ! Factorial
                                                                            n Square
                                                                            › Push input! > (input - N)^2 or x! > (x - n)^2
                                                                            i If, run code after if top of stack is 1 (found minimum number of candies)
                                                                            N Push N
                                                                            q Quit, and as nothing has been printed, N is implicitly printed


                                                                            Uses the same approach as my Python submission. Very new to 05AB1E so any tips on code or explaination greatly appreciated.



                                                                            -4 bytes thanks to Kevin Cruijssen







                                                                            share|improve this answer














                                                                            share|improve this answer



                                                                            share|improve this answer








                                                                            edited Dec 13 at 23:16

























                                                                            answered Dec 12 at 21:44









                                                                            nedla2004

                                                                            3911310




                                                                            3911310












                                                                            • Nice answer! You can golf 3 bytes like this without breaking input 1. If the if-statement is truthy, it will push index N to the stack and exit the program (outputting that index implicitly). For input 1 the if-statement will be falsey, but it will output its input 1 implicitly after that single-iteration loop.
                                                                              – Kevin Cruijssen
                                                                              Dec 13 at 9:35






                                                                            • 1




                                                                              Actually, 4 bytes can be saved instead of 3: Try it online 11 bytes. The input will be used implicitly for the first factorial !, now that the stack is empty since we no longer duplicate/triplicate the if-result.
                                                                              – Kevin Cruijssen
                                                                              Dec 13 at 9:45






                                                                            • 1




                                                                              Thanks for these ideas. Although I didn't get to this idea of printing at the end, I did think of ending the for loop early. After looking for break, end, quit and escape, I just thought I wasn't understanding the way loops work correctly. Somehow terminate never occured to me.
                                                                              – nedla2004
                                                                              Dec 13 at 23:18






                                                                            • 1




                                                                              Your answer was already pretty good. It's usually easier to golf an existing answer further, then to golf it yourself from nothing. If I would have done this challenge myself I probably would have ended up at 15 or 14 bytes as well. I used your idea of breaking and replaced it with a terminate and implicit output instead, after that I tried a few things, and in the end I saw I didn't need the duplicate anymore, which would also fix test case 1 outputting the input implicitly when the stack is empty. :)
                                                                              – Kevin Cruijssen
                                                                              2 days ago






                                                                            • 1




                                                                              FYI: I've posted a 7 bytes alternative by porting Dennis♦' Jelly answer. As always, Dennis♦ is able to perform magic in terms of Jelly code-golfing.. ;p
                                                                              – Kevin Cruijssen
                                                                              2 days ago


















                                                                            • Nice answer! You can golf 3 bytes like this without breaking input 1. If the if-statement is truthy, it will push index N to the stack and exit the program (outputting that index implicitly). For input 1 the if-statement will be falsey, but it will output its input 1 implicitly after that single-iteration loop.
                                                                              – Kevin Cruijssen
                                                                              Dec 13 at 9:35






                                                                            • 1




                                                                              Actually, 4 bytes can be saved instead of 3: Try it online 11 bytes. The input will be used implicitly for the first factorial !, now that the stack is empty since we no longer duplicate/triplicate the if-result.
                                                                              – Kevin Cruijssen
                                                                              Dec 13 at 9:45






                                                                            • 1




                                                                              Thanks for these ideas. Although I didn't get to this idea of printing at the end, I did think of ending the for loop early. After looking for break, end, quit and escape, I just thought I wasn't understanding the way loops work correctly. Somehow terminate never occured to me.
                                                                              – nedla2004
                                                                              Dec 13 at 23:18






                                                                            • 1




                                                                              Your answer was already pretty good. It's usually easier to golf an existing answer further, then to golf it yourself from nothing. If I would have done this challenge myself I probably would have ended up at 15 or 14 bytes as well. I used your idea of breaking and replaced it with a terminate and implicit output instead, after that I tried a few things, and in the end I saw I didn't need the duplicate anymore, which would also fix test case 1 outputting the input implicitly when the stack is empty. :)
                                                                              – Kevin Cruijssen
                                                                              2 days ago






                                                                            • 1




                                                                              FYI: I've posted a 7 bytes alternative by porting Dennis♦' Jelly answer. As always, Dennis♦ is able to perform magic in terms of Jelly code-golfing.. ;p
                                                                              – Kevin Cruijssen
                                                                              2 days ago
















                                                                            Nice answer! You can golf 3 bytes like this without breaking input 1. If the if-statement is truthy, it will push index N to the stack and exit the program (outputting that index implicitly). For input 1 the if-statement will be falsey, but it will output its input 1 implicitly after that single-iteration loop.
                                                                            – Kevin Cruijssen
                                                                            Dec 13 at 9:35




                                                                            Nice answer! You can golf 3 bytes like this without breaking input 1. If the if-statement is truthy, it will push index N to the stack and exit the program (outputting that index implicitly). For input 1 the if-statement will be falsey, but it will output its input 1 implicitly after that single-iteration loop.
                                                                            – Kevin Cruijssen
                                                                            Dec 13 at 9:35




                                                                            1




                                                                            1




                                                                            Actually, 4 bytes can be saved instead of 3: Try it online 11 bytes. The input will be used implicitly for the first factorial !, now that the stack is empty since we no longer duplicate/triplicate the if-result.
                                                                            – Kevin Cruijssen
                                                                            Dec 13 at 9:45




                                                                            Actually, 4 bytes can be saved instead of 3: Try it online 11 bytes. The input will be used implicitly for the first factorial !, now that the stack is empty since we no longer duplicate/triplicate the if-result.
                                                                            – Kevin Cruijssen
                                                                            Dec 13 at 9:45




                                                                            1




                                                                            1




                                                                            Thanks for these ideas. Although I didn't get to this idea of printing at the end, I did think of ending the for loop early. After looking for break, end, quit and escape, I just thought I wasn't understanding the way loops work correctly. Somehow terminate never occured to me.
                                                                            – nedla2004
                                                                            Dec 13 at 23:18




                                                                            Thanks for these ideas. Although I didn't get to this idea of printing at the end, I did think of ending the for loop early. After looking for break, end, quit and escape, I just thought I wasn't understanding the way loops work correctly. Somehow terminate never occured to me.
                                                                            – nedla2004
                                                                            Dec 13 at 23:18




                                                                            1




                                                                            1




                                                                            Your answer was already pretty good. It's usually easier to golf an existing answer further, then to golf it yourself from nothing. If I would have done this challenge myself I probably would have ended up at 15 or 14 bytes as well. I used your idea of breaking and replaced it with a terminate and implicit output instead, after that I tried a few things, and in the end I saw I didn't need the duplicate anymore, which would also fix test case 1 outputting the input implicitly when the stack is empty. :)
                                                                            – Kevin Cruijssen
                                                                            2 days ago




                                                                            Your answer was already pretty good. It's usually easier to golf an existing answer further, then to golf it yourself from nothing. If I would have done this challenge myself I probably would have ended up at 15 or 14 bytes as well. I used your idea of breaking and replaced it with a terminate and implicit output instead, after that I tried a few things, and in the end I saw I didn't need the duplicate anymore, which would also fix test case 1 outputting the input implicitly when the stack is empty. :)
                                                                            – Kevin Cruijssen
                                                                            2 days ago




                                                                            1




                                                                            1




                                                                            FYI: I've posted a 7 bytes alternative by porting Dennis♦' Jelly answer. As always, Dennis♦ is able to perform magic in terms of Jelly code-golfing.. ;p
                                                                            – Kevin Cruijssen
                                                                            2 days ago




                                                                            FYI: I've posted a 7 bytes alternative by porting Dennis♦' Jelly answer. As always, Dennis♦ is able to perform magic in terms of Jelly code-golfing.. ;p
                                                                            – Kevin Cruijssen
                                                                            2 days ago










                                                                            up vote
                                                                            0
                                                                            down vote














                                                                            Jelly, 14 bytes



                                                                            ạ‘rP>ạ!¥
                                                                            1ç1#«


                                                                            Try it online!



                                                                            Handles 1 correctly.






                                                                            share|improve this answer

























                                                                              up vote
                                                                              0
                                                                              down vote














                                                                              Jelly, 14 bytes



                                                                              ạ‘rP>ạ!¥
                                                                              1ç1#«


                                                                              Try it online!



                                                                              Handles 1 correctly.






                                                                              share|improve this answer























                                                                                up vote
                                                                                0
                                                                                down vote










                                                                                up vote
                                                                                0
                                                                                down vote










                                                                                Jelly, 14 bytes



                                                                                ạ‘rP>ạ!¥
                                                                                1ç1#«


                                                                                Try it online!



                                                                                Handles 1 correctly.






                                                                                share|improve this answer













                                                                                Jelly, 14 bytes



                                                                                ạ‘rP>ạ!¥
                                                                                1ç1#«


                                                                                Try it online!



                                                                                Handles 1 correctly.







                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered Dec 12 at 22:39









                                                                                Erik the Outgolfer

                                                                                31.2k429102




                                                                                31.2k429102






















                                                                                    up vote
                                                                                    0
                                                                                    down vote














                                                                                    Charcoal, 20 bytes



                                                                                    NθI⊕ΣEθ‹Π⊕…ιθ∨Π…¹⊕ι¹


                                                                                    Try it online! Link is to verbose version of code. Explanation:



                                                                                    Nθ                      Input `n`
                                                                                    Σ Sum of
                                                                                    θ `n`
                                                                                    E Mapped over implicit range
                                                                                    Π Product of
                                                                                    ι Current value
                                                                                    … Range to
                                                                                    θ `n`
                                                                                    ⊕ Incremented
                                                                                    ‹ Less than
                                                                                    Π Product of
                                                                                    ¹ Literal 1
                                                                                    … Range to
                                                                                    ι Current value
                                                                                    ⊕ Incremented
                                                                                    ∨ Logical Or
                                                                                    ¹ Literal 1
                                                                                    ⊕ Incremented
                                                                                    I Cast to string
                                                                                    Implicitly print


                                                                                    Product on an empty list in Charcoal returns None rather than 1, so I have to logically Or it.






                                                                                    share|improve this answer





















                                                                                    • Are you sure these characters are 8 bit each?
                                                                                      – RosLuP
                                                                                      Dec 13 at 20:37












                                                                                    • @RosLuP Charcoal is one of many languages you might find here that uses a custom code page instead of, say, ASCII. This means that each eight-bit value is mapped to a custom symbol; these symbols are designed to help the programmer remember what each byte does a little easier than if they were just randomly dispersed among one of the standardized code pages. Feel free to ask for more details in the PPCG chat.
                                                                                      – Phlarx
                                                                                      Dec 13 at 21:14

















                                                                                    up vote
                                                                                    0
                                                                                    down vote














                                                                                    Charcoal, 20 bytes



                                                                                    NθI⊕ΣEθ‹Π⊕…ιθ∨Π…¹⊕ι¹


                                                                                    Try it online! Link is to verbose version of code. Explanation:



                                                                                    Nθ                      Input `n`
                                                                                    Σ Sum of
                                                                                    θ `n`
                                                                                    E Mapped over implicit range
                                                                                    Π Product of
                                                                                    ι Current value
                                                                                    … Range to
                                                                                    θ `n`
                                                                                    ⊕ Incremented
                                                                                    ‹ Less than
                                                                                    Π Product of
                                                                                    ¹ Literal 1
                                                                                    … Range to
                                                                                    ι Current value
                                                                                    ⊕ Incremented
                                                                                    ∨ Logical Or
                                                                                    ¹ Literal 1
                                                                                    ⊕ Incremented
                                                                                    I Cast to string
                                                                                    Implicitly print


                                                                                    Product on an empty list in Charcoal returns None rather than 1, so I have to logically Or it.






                                                                                    share|improve this answer





















                                                                                    • Are you sure these characters are 8 bit each?
                                                                                      – RosLuP
                                                                                      Dec 13 at 20:37












                                                                                    • @RosLuP Charcoal is one of many languages you might find here that uses a custom code page instead of, say, ASCII. This means that each eight-bit value is mapped to a custom symbol; these symbols are designed to help the programmer remember what each byte does a little easier than if they were just randomly dispersed among one of the standardized code pages. Feel free to ask for more details in the PPCG chat.
                                                                                      – Phlarx
                                                                                      Dec 13 at 21:14















                                                                                    up vote
                                                                                    0
                                                                                    down vote










                                                                                    up vote
                                                                                    0
                                                                                    down vote










                                                                                    Charcoal, 20 bytes



                                                                                    NθI⊕ΣEθ‹Π⊕…ιθ∨Π…¹⊕ι¹


                                                                                    Try it online! Link is to verbose version of code. Explanation:



                                                                                    Nθ                      Input `n`
                                                                                    Σ Sum of
                                                                                    θ `n`
                                                                                    E Mapped over implicit range
                                                                                    Π Product of
                                                                                    ι Current value
                                                                                    … Range to
                                                                                    θ `n`
                                                                                    ⊕ Incremented
                                                                                    ‹ Less than
                                                                                    Π Product of
                                                                                    ¹ Literal 1
                                                                                    … Range to
                                                                                    ι Current value
                                                                                    ⊕ Incremented
                                                                                    ∨ Logical Or
                                                                                    ¹ Literal 1
                                                                                    ⊕ Incremented
                                                                                    I Cast to string
                                                                                    Implicitly print


                                                                                    Product on an empty list in Charcoal returns None rather than 1, so I have to logically Or it.






                                                                                    share|improve this answer













                                                                                    Charcoal, 20 bytes



                                                                                    NθI⊕ΣEθ‹Π⊕…ιθ∨Π…¹⊕ι¹


                                                                                    Try it online! Link is to verbose version of code. Explanation:



                                                                                    Nθ                      Input `n`
                                                                                    Σ Sum of
                                                                                    θ `n`
                                                                                    E Mapped over implicit range
                                                                                    Π Product of
                                                                                    ι Current value
                                                                                    … Range to
                                                                                    θ `n`
                                                                                    ⊕ Incremented
                                                                                    ‹ Less than
                                                                                    Π Product of
                                                                                    ¹ Literal 1
                                                                                    … Range to
                                                                                    ι Current value
                                                                                    ⊕ Incremented
                                                                                    ∨ Logical Or
                                                                                    ¹ Literal 1
                                                                                    ⊕ Incremented
                                                                                    I Cast to string
                                                                                    Implicitly print


                                                                                    Product on an empty list in Charcoal returns None rather than 1, so I have to logically Or it.







                                                                                    share|improve this answer












                                                                                    share|improve this answer



                                                                                    share|improve this answer










                                                                                    answered Dec 13 at 20:26









                                                                                    Neil

                                                                                    78.9k744175




                                                                                    78.9k744175












                                                                                    • Are you sure these characters are 8 bit each?
                                                                                      – RosLuP
                                                                                      Dec 13 at 20:37












                                                                                    • @RosLuP Charcoal is one of many languages you might find here that uses a custom code page instead of, say, ASCII. This means that each eight-bit value is mapped to a custom symbol; these symbols are designed to help the programmer remember what each byte does a little easier than if they were just randomly dispersed among one of the standardized code pages. Feel free to ask for more details in the PPCG chat.
                                                                                      – Phlarx
                                                                                      Dec 13 at 21:14




















                                                                                    • Are you sure these characters are 8 bit each?
                                                                                      – RosLuP
                                                                                      Dec 13 at 20:37












                                                                                    • @RosLuP Charcoal is one of many languages you might find here that uses a custom code page instead of, say, ASCII. This means that each eight-bit value is mapped to a custom symbol; these symbols are designed to help the programmer remember what each byte does a little easier than if they were just randomly dispersed among one of the standardized code pages. Feel free to ask for more details in the PPCG chat.
                                                                                      – Phlarx
                                                                                      Dec 13 at 21:14


















                                                                                    Are you sure these characters are 8 bit each?
                                                                                    – RosLuP
                                                                                    Dec 13 at 20:37






                                                                                    Are you sure these characters are 8 bit each?
                                                                                    – RosLuP
                                                                                    Dec 13 at 20:37














                                                                                    @RosLuP Charcoal is one of many languages you might find here that uses a custom code page instead of, say, ASCII. This means that each eight-bit value is mapped to a custom symbol; these symbols are designed to help the programmer remember what each byte does a little easier than if they were just randomly dispersed among one of the standardized code pages. Feel free to ask for more details in the PPCG chat.
                                                                                    – Phlarx
                                                                                    Dec 13 at 21:14






                                                                                    @RosLuP Charcoal is one of many languages you might find here that uses a custom code page instead of, say, ASCII. This means that each eight-bit value is mapped to a custom symbol; these symbols are designed to help the programmer remember what each byte does a little easier than if they were just randomly dispersed among one of the standardized code pages. Feel free to ask for more details in the PPCG chat.
                                                                                    – Phlarx
                                                                                    Dec 13 at 21:14












                                                                                    up vote
                                                                                    0
                                                                                    down vote














                                                                                    PHP, 107 bytes





                                                                                    <?php $x=fgets(STDIN);function f($i){return $i==0?:$i*f($i-1);}$n=1;while(f($x)<f($x-$n)**2){$n++;}echo $n;


                                                                                    Try it online!



                                                                                    Uses the same $x^2>((x-1)!)^2$ method as others have used.



                                                                                    Uses the factorial function from the PHP submission for this challenge (thanks to @donutdan4114)






                                                                                    share|improve this answer

























                                                                                      up vote
                                                                                      0
                                                                                      down vote














                                                                                      PHP, 107 bytes





                                                                                      <?php $x=fgets(STDIN);function f($i){return $i==0?:$i*f($i-1);}$n=1;while(f($x)<f($x-$n)**2){$n++;}echo $n;


                                                                                      Try it online!



                                                                                      Uses the same $x^2>((x-1)!)^2$ method as others have used.



                                                                                      Uses the factorial function from the PHP submission for this challenge (thanks to @donutdan4114)






                                                                                      share|improve this answer























                                                                                        up vote
                                                                                        0
                                                                                        down vote










                                                                                        up vote
                                                                                        0
                                                                                        down vote










                                                                                        PHP, 107 bytes





                                                                                        <?php $x=fgets(STDIN);function f($i){return $i==0?:$i*f($i-1);}$n=1;while(f($x)<f($x-$n)**2){$n++;}echo $n;


                                                                                        Try it online!



                                                                                        Uses the same $x^2>((x-1)!)^2$ method as others have used.



                                                                                        Uses the factorial function from the PHP submission for this challenge (thanks to @donutdan4114)






                                                                                        share|improve this answer













                                                                                        PHP, 107 bytes





                                                                                        <?php $x=fgets(STDIN);function f($i){return $i==0?:$i*f($i-1);}$n=1;while(f($x)<f($x-$n)**2){$n++;}echo $n;


                                                                                        Try it online!



                                                                                        Uses the same $x^2>((x-1)!)^2$ method as others have used.



                                                                                        Uses the factorial function from the PHP submission for this challenge (thanks to @donutdan4114)







                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Dec 13 at 23:02









                                                                                        NK1406

                                                                                        479213




                                                                                        479213






















                                                                                            up vote
                                                                                            0
                                                                                            down vote














                                                                                            Wolfram Language (Mathematica), 43 bytes



                                                                                            Min[n/.Solve[#!>(#-n)!^2, Integers]]/.n->1&


                                                                                            Try it online!






                                                                                            share|improve this answer

























                                                                                              up vote
                                                                                              0
                                                                                              down vote














                                                                                              Wolfram Language (Mathematica), 43 bytes



                                                                                              Min[n/.Solve[#!>(#-n)!^2, Integers]]/.n->1&


                                                                                              Try it online!






                                                                                              share|improve this answer























                                                                                                up vote
                                                                                                0
                                                                                                down vote










                                                                                                up vote
                                                                                                0
                                                                                                down vote










                                                                                                Wolfram Language (Mathematica), 43 bytes



                                                                                                Min[n/.Solve[#!>(#-n)!^2, Integers]]/.n->1&


                                                                                                Try it online!






                                                                                                share|improve this answer













                                                                                                Wolfram Language (Mathematica), 43 bytes



                                                                                                Min[n/.Solve[#!>(#-n)!^2, Integers]]/.n->1&


                                                                                                Try it online!







                                                                                                share|improve this answer












                                                                                                share|improve this answer



                                                                                                share|improve this answer










                                                                                                answered Dec 14 at 2:57









                                                                                                Kelly Lowder

                                                                                                2,998416




                                                                                                2,998416






















                                                                                                    up vote
                                                                                                    0
                                                                                                    down vote














                                                                                                    05AB1E, 7 bytes



                                                                                                    L!ns!@O


                                                                                                    Port of Dennis♦' Jelly answer, so make sure to upvote him if you like this answer!



                                                                                                    Try it online or verify all test cases.



                                                                                                    Explanation:





                                                                                                    L          # List in the range [1, (implicit) input]
                                                                                                    ! # Take the factorial of each
                                                                                                    n # Then square each
                                                                                                    s! # Take the factorial of the input
                                                                                                    @ # Check for each value in the list if they are larger than or equal to the
                                                                                                    # input-faculty (1 if truthy; 0 if falsey)
                                                                                                    O # Sum, so determine the amount of truthy checks (and output implicitly)





                                                                                                    share|improve this answer

























                                                                                                      up vote
                                                                                                      0
                                                                                                      down vote














                                                                                                      05AB1E, 7 bytes



                                                                                                      L!ns!@O


                                                                                                      Port of Dennis♦' Jelly answer, so make sure to upvote him if you like this answer!



                                                                                                      Try it online or verify all test cases.



                                                                                                      Explanation:





                                                                                                      L          # List in the range [1, (implicit) input]
                                                                                                      ! # Take the factorial of each
                                                                                                      n # Then square each
                                                                                                      s! # Take the factorial of the input
                                                                                                      @ # Check for each value in the list if they are larger than or equal to the
                                                                                                      # input-faculty (1 if truthy; 0 if falsey)
                                                                                                      O # Sum, so determine the amount of truthy checks (and output implicitly)





                                                                                                      share|improve this answer























                                                                                                        up vote
                                                                                                        0
                                                                                                        down vote










                                                                                                        up vote
                                                                                                        0
                                                                                                        down vote










                                                                                                        05AB1E, 7 bytes



                                                                                                        L!ns!@O


                                                                                                        Port of Dennis♦' Jelly answer, so make sure to upvote him if you like this answer!



                                                                                                        Try it online or verify all test cases.



                                                                                                        Explanation:





                                                                                                        L          # List in the range [1, (implicit) input]
                                                                                                        ! # Take the factorial of each
                                                                                                        n # Then square each
                                                                                                        s! # Take the factorial of the input
                                                                                                        @ # Check for each value in the list if they are larger than or equal to the
                                                                                                        # input-faculty (1 if truthy; 0 if falsey)
                                                                                                        O # Sum, so determine the amount of truthy checks (and output implicitly)





                                                                                                        share|improve this answer













                                                                                                        05AB1E, 7 bytes



                                                                                                        L!ns!@O


                                                                                                        Port of Dennis♦' Jelly answer, so make sure to upvote him if you like this answer!



                                                                                                        Try it online or verify all test cases.



                                                                                                        Explanation:





                                                                                                        L          # List in the range [1, (implicit) input]
                                                                                                        ! # Take the factorial of each
                                                                                                        n # Then square each
                                                                                                        s! # Take the factorial of the input
                                                                                                        @ # Check for each value in the list if they are larger than or equal to the
                                                                                                        # input-faculty (1 if truthy; 0 if falsey)
                                                                                                        O # Sum, so determine the amount of truthy checks (and output implicitly)






                                                                                                        share|improve this answer












                                                                                                        share|improve this answer



                                                                                                        share|improve this answer










                                                                                                        answered 2 days ago









                                                                                                        Kevin Cruijssen

                                                                                                        35.5k554186




                                                                                                        35.5k554186






















                                                                                                            up vote
                                                                                                            0
                                                                                                            down vote













                                                                                                            Japt -x, 7 bytes



                                                                                                            Port of Dennis' Jelly solution.



                                                                                                            Only works in practice up to n=4 as we get into scientific notation above that.



                                                                                                            õÊ®²¨U²


                                                                                                            Try it



                                                                                                            õ           :Range [1,input]
                                                                                                            Ê :Factorial of each
                                                                                                            ® :Map
                                                                                                            ² : Square
                                                                                                            ¨ : Greater than or equal to
                                                                                                            U² : Input squared
                                                                                                            :Implicitly reduce by addition





                                                                                                            share|improve this answer

























                                                                                                              up vote
                                                                                                              0
                                                                                                              down vote













                                                                                                              Japt -x, 7 bytes



                                                                                                              Port of Dennis' Jelly solution.



                                                                                                              Only works in practice up to n=4 as we get into scientific notation above that.



                                                                                                              õÊ®²¨U²


                                                                                                              Try it



                                                                                                              õ           :Range [1,input]
                                                                                                              Ê :Factorial of each
                                                                                                              ® :Map
                                                                                                              ² : Square
                                                                                                              ¨ : Greater than or equal to
                                                                                                              U² : Input squared
                                                                                                              :Implicitly reduce by addition





                                                                                                              share|improve this answer























                                                                                                                up vote
                                                                                                                0
                                                                                                                down vote










                                                                                                                up vote
                                                                                                                0
                                                                                                                down vote









                                                                                                                Japt -x, 7 bytes



                                                                                                                Port of Dennis' Jelly solution.



                                                                                                                Only works in practice up to n=4 as we get into scientific notation above that.



                                                                                                                õÊ®²¨U²


                                                                                                                Try it



                                                                                                                õ           :Range [1,input]
                                                                                                                Ê :Factorial of each
                                                                                                                ® :Map
                                                                                                                ² : Square
                                                                                                                ¨ : Greater than or equal to
                                                                                                                U² : Input squared
                                                                                                                :Implicitly reduce by addition





                                                                                                                share|improve this answer












                                                                                                                Japt -x, 7 bytes



                                                                                                                Port of Dennis' Jelly solution.



                                                                                                                Only works in practice up to n=4 as we get into scientific notation above that.



                                                                                                                õÊ®²¨U²


                                                                                                                Try it



                                                                                                                õ           :Range [1,input]
                                                                                                                Ê :Factorial of each
                                                                                                                ® :Map
                                                                                                                ² : Square
                                                                                                                ¨ : Greater than or equal to
                                                                                                                U² : Input squared
                                                                                                                :Implicitly reduce by addition






                                                                                                                share|improve this answer












                                                                                                                share|improve this answer



                                                                                                                share|improve this answer










                                                                                                                answered 2 days ago









                                                                                                                Shaggy

                                                                                                                18.7k21663




                                                                                                                18.7k21663






















                                                                                                                    up vote
                                                                                                                    0
                                                                                                                    down vote














                                                                                                                    C (gcc), 68 bytes





                                                                                                                    n;f(T x){T i=2,j=x,b=1,g=x;while(i<j)b*i>g?g*=--j:(b*=i++);n=x-j+1;}


                                                                                                                    Try it online!



                                                                                                                    Edit: shove a few obvious bytes



                                                                                                                    Edit 2: trading bytes against mults, no doing 2*x mults instead of x+n



                                                                                                                    Well I have this in C. Fails at 34 when T is long.



                                                                                                                    There is a possible ambiguity as to whether the good kid wants to always win or never lose... what do you think?






                                                                                                                    share|improve this answer










                                                                                                                    New contributor




                                                                                                                    Balzola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                    Check out our Code of Conduct.






















                                                                                                                      up vote
                                                                                                                      0
                                                                                                                      down vote














                                                                                                                      C (gcc), 68 bytes





                                                                                                                      n;f(T x){T i=2,j=x,b=1,g=x;while(i<j)b*i>g?g*=--j:(b*=i++);n=x-j+1;}


                                                                                                                      Try it online!



                                                                                                                      Edit: shove a few obvious bytes



                                                                                                                      Edit 2: trading bytes against mults, no doing 2*x mults instead of x+n



                                                                                                                      Well I have this in C. Fails at 34 when T is long.



                                                                                                                      There is a possible ambiguity as to whether the good kid wants to always win or never lose... what do you think?






                                                                                                                      share|improve this answer










                                                                                                                      New contributor




                                                                                                                      Balzola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                      Check out our Code of Conduct.




















                                                                                                                        up vote
                                                                                                                        0
                                                                                                                        down vote










                                                                                                                        up vote
                                                                                                                        0
                                                                                                                        down vote










                                                                                                                        C (gcc), 68 bytes





                                                                                                                        n;f(T x){T i=2,j=x,b=1,g=x;while(i<j)b*i>g?g*=--j:(b*=i++);n=x-j+1;}


                                                                                                                        Try it online!



                                                                                                                        Edit: shove a few obvious bytes



                                                                                                                        Edit 2: trading bytes against mults, no doing 2*x mults instead of x+n



                                                                                                                        Well I have this in C. Fails at 34 when T is long.



                                                                                                                        There is a possible ambiguity as to whether the good kid wants to always win or never lose... what do you think?






                                                                                                                        share|improve this answer










                                                                                                                        New contributor




                                                                                                                        Balzola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                        Check out our Code of Conduct.










                                                                                                                        C (gcc), 68 bytes





                                                                                                                        n;f(T x){T i=2,j=x,b=1,g=x;while(i<j)b*i>g?g*=--j:(b*=i++);n=x-j+1;}


                                                                                                                        Try it online!



                                                                                                                        Edit: shove a few obvious bytes



                                                                                                                        Edit 2: trading bytes against mults, no doing 2*x mults instead of x+n



                                                                                                                        Well I have this in C. Fails at 34 when T is long.



                                                                                                                        There is a possible ambiguity as to whether the good kid wants to always win or never lose... what do you think?







                                                                                                                        share|improve this answer










                                                                                                                        New contributor




                                                                                                                        Balzola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                        Check out our Code of Conduct.









                                                                                                                        share|improve this answer



                                                                                                                        share|improve this answer








                                                                                                                        edited 2 days ago





















                                                                                                                        New contributor




                                                                                                                        Balzola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                        Check out our Code of Conduct.









                                                                                                                        answered Dec 13 at 22:56









                                                                                                                        Balzola

                                                                                                                        1011




                                                                                                                        1011




                                                                                                                        New contributor




                                                                                                                        Balzola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                        Check out our Code of Conduct.





                                                                                                                        New contributor





                                                                                                                        Balzola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                        Check out our Code of Conduct.






                                                                                                                        Balzola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                        Check out our Code of Conduct.






















                                                                                                                            up vote
                                                                                                                            0
                                                                                                                            down vote














                                                                                                                            C# (.NET Core), 93 bytes





                                                                                                                            n=>{int d(int k)=>k<2?1:k*d(k-1);int a=1,b=d(n),c=n;for(;;){a*=n;b/=n--;if(a>=b)return c-n;}}


                                                                                                                            Try it online!



                                                                                                                            Based off of @Arnauld's javascript answer.






                                                                                                                            share|improve this answer

























                                                                                                                              up vote
                                                                                                                              0
                                                                                                                              down vote














                                                                                                                              C# (.NET Core), 93 bytes





                                                                                                                              n=>{int d(int k)=>k<2?1:k*d(k-1);int a=1,b=d(n),c=n;for(;;){a*=n;b/=n--;if(a>=b)return c-n;}}


                                                                                                                              Try it online!



                                                                                                                              Based off of @Arnauld's javascript answer.






                                                                                                                              share|improve this answer























                                                                                                                                up vote
                                                                                                                                0
                                                                                                                                down vote










                                                                                                                                up vote
                                                                                                                                0
                                                                                                                                down vote










                                                                                                                                C# (.NET Core), 93 bytes





                                                                                                                                n=>{int d(int k)=>k<2?1:k*d(k-1);int a=1,b=d(n),c=n;for(;;){a*=n;b/=n--;if(a>=b)return c-n;}}


                                                                                                                                Try it online!



                                                                                                                                Based off of @Arnauld's javascript answer.






                                                                                                                                share|improve this answer













                                                                                                                                C# (.NET Core), 93 bytes





                                                                                                                                n=>{int d(int k)=>k<2?1:k*d(k-1);int a=1,b=d(n),c=n;for(;;){a*=n;b/=n--;if(a>=b)return c-n;}}


                                                                                                                                Try it online!



                                                                                                                                Based off of @Arnauld's javascript answer.







                                                                                                                                share|improve this answer












                                                                                                                                share|improve this answer



                                                                                                                                share|improve this answer










                                                                                                                                answered 2 days ago









                                                                                                                                Embodiment of Ignorance

                                                                                                                                2088




                                                                                                                                2088






























                                                                                                                                    draft saved

                                                                                                                                    draft discarded




















































                                                                                                                                    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.




                                                                                                                                    draft saved


                                                                                                                                    draft discarded














                                                                                                                                    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





















































                                                                                                                                    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







                                                                                                                                    Popular posts from this blog

                                                                                                                                    If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                                                                                                                                    Alcedinidae

                                                                                                                                    Can an atomic nucleus contain both particles and antiparticles? [duplicate]