Module ExpMod
In: lib/exp_mod.rb
Author:Nicolas Pouillard <nicolas.pouillard@gmail.com>.
Copyright:Copyright (c) 2006 Nicolas Pouillard. All rights reserved.
License:GNU General Public License (GPL).
Revision:$Id: /w/fey/ruby_ex/trunk/lib/exp_mod.rb 53899 2007-01-13T11:21:51.227355Z ertai $

Methods

exp  

Public Instance methods

[Source]

# File lib/exp_mod.rb, line 8
  def exp ( m, p )
    case m
    when 0 then 1
    when 1 then self % p
    else
      q, r = m.divmod(2)
      tmp1 = self.exp(q, p)
      tmp2 = (tmp1 * tmp1) % p
      if r == 0
        tmp2
      else
        (tmp2 * self) % p
      end
    end
  end

[Validate]