Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Unless description was changed:
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 :)
Nice kata. I was afraid my solution was unnecessarily complicated but when it ran in less than 1s I was happy :)
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!)
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 :)
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:
As you can see, the value
916102737627536640
that appears in your result IS NOT a binomial coefficient of the formm choose 2
for some integerm
.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.
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?
This comment is hidden because it contains spoiler information about the solution
Its 5 per test.
How many assertions per test? In my 100 random tests I've got 255 assertions passed but still timing out.
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