Ad
  • Custom User Avatar

    True. Thanks for noticing!

  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    Hi,
    the idea is to first find a formula for counting the 1s in a binary number (Hamming weight), then to calculate the partial sums.

    we define:
    fun(n) = sum(i=0..n, a(n)), where a(n) is hamming weight of n

    The hamming weight of n is easy to calculate in logarithmic time, once you realise that removing the last digit divides the number by two:

    a(2n+1) = a(n) + 1 // last digit is 1
    a(2n) = a(n) // last digit is 0

    Now we want to use the above formulas to calculate fun(2n) in terms of fun(n). One way to do it is to split the sum in two different sums over the even and the odd indexes. So we get:

    fun(2n+1)
    == sum(i=0..2
    n+1, a(i)) // definition of fun(n)
    == sum(j=0..n, a(2j)) + sum(j=0..n, a(2j+1)) // splitting odd and even
    == sum(j=0..n, a(j)) + sum(j=0..n, a(j)+1) // Hamming weight formula
    == fun(n) + fun(n) + n + 1 // definition of fun(n)
    == 2*fun(n) + n + 1

    fun(2n)
    == sum(i=0..2
    n, a(i))
    == sum(j=0..n, a(2j)) + sum(j=0..n-1, a(2j+1))
    == sum(j=0..n, a(j)) + sum(j=0..n-1, a(j) + 1)
    == fun(n) + fun(n-1) + n

  • Custom User Avatar
  • Default User Avatar

    Hi Joohan,

    thanks for your feedback.
    I did not forget the random tests: I translated the complete kata including the tests.
    As I understood from the codewars guidelines the translations are supposed to be faithful to the original rather than an attempt to provide an improved version of the original.
    In this case the original kata could have been improved by adding randomized tests (as you suggested), adding more test cases for covering the corner cases and also by expanding the description and the examples provided.
    Considering that this is a very simple evaluate-arithmetic-expression kata and many improved versions already existed in codewars, I am not sure the effort would be worth the time.
    Regarding the downvote, feel free to downvote as you please.
    If you think that translations should provide extra test cases with respect to the original, I think this should be an aspect that should be clarified in the translation guidelines. Perhaps you can contact the codewars website administrator about this?
    thanks!
    A.

  • Default User Avatar

    Hi, is it possible to approve the translation?

  • Default User Avatar

    thanks! they are there now

  • Default User Avatar

    Haskell translation added (status: waiting approval)