Closed Thread
Results 1 to 6 of 6

Thread: Large powers

  1. #1
    tim1234 is offline Newbie
    Join Date
    Dec 2006
    Posts
    4
    Rep Power
    0

    Large powers

    How would you write an algorithm in C to calculate large numbers raised to large numbers modulo n. (numbers in the 10,000s) so obviously can't just do it straight off.

    I tried working out x, x^2, x^4 and somehow using the binary representation of the power to work it out but I have no idea how to program this.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143
    Code:
    int power(int base, int exponent, int modulo)
    {
      if exponent=1 return base % modulo;
      if not (exponent % 2)
         return power(base*base%modulo,exponent/2,modulo)
      else
         return power(base*base%modulo,exponent/2,modulo)*base%modulo;
    }
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    DevilsCharm's Avatar
    DevilsCharm is offline Programming God
    Join Date
    Jul 2006
    Posts
    884
    Rep Power
    0
    A power to the 10,000? That's really large, although I've always thought if supercomputers can process really, really, really big numbers.

  5. #4
    Join Date
    Oct 2006
    Location
    Hendersonville, NC
    Posts
    1,700
    Blog Entries
    3
    Rep Power
    0
    Sounds like we are doing your HW for you? 0_o

    Good Luck


  6. #5
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101
    Quote Originally Posted by WingedPanther View Post
    Code:
    int power(int base, int exponent, int modulo)
    {
      if exponent=1 return base % modulo;
      if not (exponent % 2)
         return power(base*base%modulo,exponent/2,modulo)
      else
         return power(base*base%modulo,exponent/2,modulo)*base%modulo;
    }
    When theres Math.. WingedPanther is the one
    Good Job

  7. #6
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143
    Maybe I should write some tutorials about math in programming.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How can I tell how large my program is?
    By jcampos8782 in forum C and C++
    Replies: 3
    Last Post: 03-08-2011, 09:32 PM
  2. C++ Calculator for very large numbers.
    By TheZea in forum C and C++
    Replies: 6
    Last Post: 03-29-2010, 06:13 AM
  3. Suggestions about large Matrices???
    By jakopo in forum General Programming
    Replies: 10
    Last Post: 03-01-2010, 01:23 AM
  4. Powers of ten
    By seph6664 in forum Java Help
    Replies: 3
    Last Post: 10-26-2009, 07:24 PM
  5. Large Project
    By silver163 in forum MarketPlace
    Replies: 3
    Last Post: 11-19-2008, 10:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

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