Ad
  • Custom User Avatar

    Unless description was changed:

    ... between 'left' and 'right' (including both)

  • Custom User Avatar

    Hey @neerajgopal - thanks for solving and for the kind words and feedback, glad you liked it; and indeed that's a really nice solution!

    Btw, since you're interested in combinatorics and timing your solutions, I allow myself to include this link for you:

    https://github.com/python/cpython/blob/ffcc7cd57f6a52c6074ecc9f0a9f0177fb1dbfee/Modules/mathmodule.c#L1905

    this is how factorial is implemented in CPython - it's actually much faster than using the "schoolboy" approach; so if you ever need performance in maths related katas you might need to use the inbuilt one.

    In Python that's from math import factorial.

    For example on my machine (which is a 10+ year old laptop mind you) the naive factorial takes 7.9 seconds to calculate 100_000! while the library version takes 4.3 seconds.

    See you around on other maths katas :)

  • Custom User Avatar

    Nice kata. I was afraid my solution was unnecessarily complicated but when it ran in less than 1s I was happy :)

  • Custom User Avatar

    Hey! No worries - rereading my comments, I hope I wasn't too "concise" as I was typing quickly before dinner time :)

    Glad you figured out the long integer division stuff, and good luck with advancing on the kata (it's a difficult one in my opinion compared to other 3 kyu, so don't feel bad if you have to come back to it later after solving other number theory katas on Codewars!)

  • Custom User Avatar

    Thanks @benjaminzwhite.
    Indeed I should have checked it more thoroughly. I'll be more critical before raising an issue next time. Or just not mark any comment as issue!
    In this case, I doubt I would have realized what was going wrong if not for the operations you used in your comment.
    On the bright side, today I learnt about long integer division error in Python :)

  • Custom User Avatar

    Hi @neerajgopal - no the problem is with your code (the f(n) part specifically, if you want a hint).

    "However each of the numbers is a valid binomial coefficient." <- this claim is incorrect.

    Proof - I ran your code and picked the first failed test that I encountered:

    916102738276855971 = [916102737627536640, 599982120, 49337211] <- this is your returned result 3 numbers.

    Let's look at the first of the 3 values in your returned solution list:

    res = 916102737627536640
    
    x = 1353589846 # trying various integers to see
    y = 1353589847 # if we can write the above number
    z = 1353589848 # as a binomial coefficient
    
    resx = x*(x+1)//2
    resy = y*(y+1)//2
    resz = z*(z+1)//2
    
    print(resx) # = 916102736273946781 < 916102737627536640
    print(resy) # = 916102737627536628 < 916102737627536640
    print(resz) # = 916102738981126476 > 916102737627536640
    

    As you can see, the value 916102737627536640 that appears in your result IS NOT a binomial coefficient of the form m choose 2 for some integer m.

    It seems you are new to Codewars (welcome!), so please note: in future, please try to make sure there is a real problem with the kata rather than with your solution before raising Issue flag - here as you can see it only takes 2 minutes of checking to see that your claim isn't correct.

  • Custom User Avatar

    My solution breaks for numbers > 10^18 with the error "not a valid representation using only binomial coefficients

    Example:
    961046579609841869 = [961046576024667392, 3580145271, 5029206]

    However each of the numbers is a valid binomial coefficient. Ran the same check that 8 * n + 1 is a perfect square.
    Since the tester uses the bitwise operation x<<3, is it possible the leftmost bits are getting dropped at large numbers?

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Its 5 per test.

  • Custom User Avatar

    How many assertions per test? In my 100 random tests I've got 255 assertions passed but still timing out.

  • Custom User Avatar

    Heads up. Both left and right are included in the count. I wasted nearly an hour wondering why my result was off by a tiny amount