Ad
  • Custom User Avatar

    Most sequencies can be solved mathematically instead of with loops or recursion…

    Yup. My first try was recursive and completely off. Then I saw your comment, thought about the simple 1 x N case, checked whether the induction holds and came to the same result as everyone else. I really should use the stuff I studied more often.

  • Custom User Avatar

    Whenever I encounter a kata that's "Number of x in y" or "X at position N" or something like that, my first question is always "Can I math it?" Most sequencies can be solved mathematically instead of with loops or recursion or what have you. At that point, it's just a matter of crunching the sequence into an equation. The result is nearly always faster than programmatically calculating each N until you arrive at the one you want.

    There are, of course, exceptions (prime numbers, for instance).

  • Custom User Avatar

    It's a similar solution to the one that is up voted the most just factored differently.