2 kyu

Power tower modulo m

251 of 333ecolban
Description
Loading description...
Refactoring
  • Please sign in or sign up to leave a comment.
  • ahmet_popaj Avatar

    Pretty challenging kata mathematically speaking though.

  • metatable Avatar
  • Ahmedrt6 Avatar

    I could hardly deal with the case that a and m are coprime, but still can't deal with the case that they are not.
    Is there any hint?

  • Ahmedrt6 Avatar

    This comment has been hidden.

  • Ahmedrt6 Avatar

    I don't know what I am missing
    I think I searched correctly for the relation between numbers
    I have passed 163 tests

  • Ahmedrt6 Avatar

    from how long was this kata released?

  • LNTR Avatar

    "However, tower(13, 3, 31) == pow(13, tower(13, 2, 30), 31)."

    Is this correct for only b=13,h=3,m=31 or for all inputs?

  • ExistedGit Avatar

    This comment has been hidden.

  • MarkShcerbakov Avatar

    Very good Kata!!! Thanks to author! Obviously Kata needs perfomance version. =)

  • Daimona_ Avatar

    This comment has been hidden.

  • Maelvilian Avatar

    Hello everyone,

    I have a "good" solution in Cobol, by operating the powers by an occurs, but quickly the result is too big to my working. Is there a way to avoid calculating these results before the modulo ?

    Thanks

  • dfhwze Avatar
  • 66  Avatar

    Language: C++
    The reference solution expected incorrect result than it comes to small/medium/large tests. After numerous tests with solutions from other languages with constraints where exponent == 1 most of them gave wrong answers and could easily be invalidated if those translations covers that case.

  • Harold2017 Avatar

    is the random test of c++ version broken?

  • ihatemaths Avatar

    I have an issue with one of the tests: For (3, 4, 1001) my solution give 27 whereas your's says it should be 482. A quick python check of (3**19683)%1001 gives 27, so I don't know who is correct

  • user6084406 Avatar

    C++ Translation ready for your review. Go easy, this is my first!

  • davidmaamoaix Avatar

    This comment has been hidden.

  • gregor.seidel Avatar

    On my attempt I receive a fairly odd failed test (for random inputs):

    Test tower(6, 2, 8) 4 should equal 0

    Given that 6^6 mod 8 = 4 I am confident my code is correct in this case, while the Python test seems to be incorrect. My attempt also fails at multiple other tests for larger inputs, e.g.:

    Test tower(30318, 1648, 586) 582 should equal 430

    Here I don't know which is correct, but a brute force tower code I wrote for testing using totient phi(586)=292 produces the same result as the optimised code, i.e. 582. Now I am wondering whether this, too, is an issue with the tests or with my attempt at a solution.

  • Kacarott Avatar
  • dfhwze Avatar

    C# Translation ready for review

  • dfhwze Avatar

    Bizarre test case in Python. You are testing Python's builtin 'pow' method.

    test.it('Pushing it even further...')
    t_2_4 = pow(2, 2 ** 2 ** 2)
    test.assert_equals(t_2_4, 65536)
    
  • akar-0 Avatar

    Could you approve this fork of COBOL translation, with some improvements in tests? You approved an old version (I'm use to doing a new fork when performing some change, so it can be more easily checked): https://www.codewars.com/kumite/61ac7f4e3f4ec100574f1bd3?sel=61afbe8e46519d00079a4760

  • rospolis Avatar

    Nice kata! Might seem like difficult math but a lot of the math is actually given in kata description so all that is needed is profficiency and confidence in reasonably basic (for 2kyu level) mathematics. The math involved (that you are essentially given) is relevant to several important fields including encryption.

  • akar-0 Avatar
  • koala1st Avatar

    Hi everybody, I do not understand why I have an "Execution Time Out" when I do the tests on the server since all is fine at home. I do not use brute force. when I use the maximum value allowed by the instructions :

    tower(int(1e20),int(1e20),10000000)

    the result is obtained in 703 ns ± 6.63 ns

    When I do all the tests shown in the SampleTests part, the tests are ended in 10 µs ± 765 ns

    any idea that can help me solve that Time prob?

    I use python 3.8

  • wickedposh Avatar

    in the random test tower(6,1,4) has answer 0 but I think the answer should be 2( and my solution produced 2). could you explain why it is 0?

  • Voile Avatar

    Looking at the solutions, it seems that you forgot to from math import log yourself in the tests, and we have to do that for you in our code ;-)

  • Blind4Basics Avatar

    typo in the description:

    2 ** 2 ** 2 ** 2 == 2 ** (2 ** (2 ** (2 ** 2))) == 65536
                        ^^^^
                        shouldn't be there
    
  • Voile Avatar

    This comment has been hidden.

  • Voile Avatar

    This comment has been hidden.

  • think_tank_ Avatar

    This comment has been hidden.

  • think_tank_ Avatar

    Wolfram Alpha suggests that tower(28, 3, 25) or (28^(28^(28))) mod 25 == 21 and tower(28 % 25, 3, 25) or (3^(3^(3))) mod 25 == 12. https://www.wolframalpha.com/input/?i=28%5E(28%5E(28))+mod+25, https://www.wolframalpha.com/input/?i=3%5E(3%5E(3))+mod+25.

    I'm pretty sure I'm missing something so some clarification would be great :)

  • Voile Avatar

    Expected and actual values are flipped in the example tests (and maybe in the actual tests too).