5 kyu

Happy Numbers (performance edition)

Description
Loading description...
Performance
Algorithms
  • Please sign in or sign up to leave a comment.
  • ahmet_popaj Avatar

    Really interesting mathematical kata to apply some dynamic programming techniques to.

  • goldenratio161 Avatar

    nice kata, maybe a low 4kyu?

    edit: oh wait nvm, https://www.codewars.com/kata/5ecef4a6640dbb0032bc176d

  • sj95126 Avatar

    My Python solution is timing out and I'm trying to figure out how much time is calculating the happy numbers and how much is finding "elements less than n" but when I return a fixed result to make it faster, I get this exception:

    File "tests.py", line 34, in it_1 test.assert_equals(act, exp, "Wrong result for {}".format(n)) NameError: name 'act' is not defined

    Possibly the answer is "then don't do that" but exceptions aren't very user-friendly.

  • RealKenshiro Avatar

    Exceptional Kata!

    Not that difficult but you really have to be efficient.

    Thanks to the author!

  • vizeit Avatar

    After optimization, my code takes less than 4.5 seconds for the random high value inputs (59090,114742,104721,194554,15314,293211,198561,118033,243374,138837,73094,33641,105638,281895,52600,193311,214239,66672,188339,221677,87299,127372,74204). I still see the error 'execution timeout 12000ms'. Is there something wrong with the tests?

  • tchai2 Avatar

    2 days, no more ideas.
    Am I about to resign ?
    Reading the discription I pretentiously thought I had a clear path to the solution. Then ...
    I've tried to call the less possible sum_of_digit_sq with memories, sieves, and permutations - because 137 has the same happinest than713 -
    many things but no result ...
    5000 times 300_000 numbers to test = 1.5 billions of operations if only one operation per number.

    %time for i in range(1_500_000_000) : i + 1

    1min 54s

    Clearly that's not the good way, have to make a U-turn to get out of the tunnel.

  • dagro Avatar

    This comment has been hidden.

  • mateTothCsak Avatar

    Damn I have spent so much time on finishing this kata just to realize that my code runs over the time limit. The code is good, it can give back the happy numbers for any length, but it can't handle 30 000 tests in 6 seconds. ~ Just came here to cry because I suck. Will give it a try tomorrow ;)

  • anter69 Avatar

    I generate the happy numbers up to the test limits; I check the first 143 items against the list found on wikipedia (all correct); I also get the correct number of happy numbers under 10^5, 10^6 (checked against wikipedia); and I also checked my list against a list created withe the "naive method" (seems correct). But when I hit the random tests, I get some very uninformative results for all the tests: "Value is not what was expected".

    I understand from the earlier discussions that you do some kind of comparison without assert_equals to speed things up, but once you hit an error, the test suite should stop and a proper error should be displayed. Also, gradually building up the test range would make debugging easier (e.g. first few tests up to n = 1000, then up to n = 10000, etc.)

  • Blind4Basics Avatar

    republished with:

    • update of the description (len of the code)
    • update of the anticheat tests
    • down sized the limit of chars of the code to 1700 chars (should be way enough)
  • Unnamed Avatar

    There's nothing about maximum code length in the description.

  • Blind4Basics Avatar

    new issue, sorry:

    digging with a different approach, I faced the following:

    This fork execute 4000 tests in 2,8s only while using the assertion code you use, it needs 11s average. That means that the assertion calls are actually the slowest part of your code. Take a look at the way I implemented the tests, to gain performances.

  • Blind4Basics Avatar

    needs fixed tests too in the test cases

  • Blind4Basics Avatar

    You're slow. You can push the things harder. ;)

  • Blind4Basics Avatar

    wrong function name in the sample tests.