Results 1 to 4 of 4

Thread: x ^ y Mod z

Hybrid View

  1. #1
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: x ^ y Mod z

    Yup, the usual algorithm is exponentiation by squaring with modular reduction after each step, and I imagine better ones exist. It's a native command in Python if I remember correctly, so it's a common operation. Here's a Wikipedia section discussing it. You can write a recursive version of this algorithm extremely quickly, eg. using the pseudocode in the section following the one I linked and reducing the result each time before returning.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  2. #2

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    8

    Re: x ^ y Mod z

    Thank you very much this was helpful the algorithm seems to be quick too. I was wondering if there's an even more generic algorithm for if y is a negative integer (in my case if it's negative it will always be -1).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width