Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Write a function f(a,b,c) that calculates a^b (mod c) in less than 10 seconds.

  • a, b < 10^100
  • c < 10000.

examples:

f(10^50, 10^50, 54123) = 46555
f(5^99, 10^99, 777) = 1
share|improve this question
1  
Inbuilt functions disallowed? (f=pow would work in Python). – Dogbert Feb 11 '11 at 21:41
Is division allowed in this one? What the input? Is it always positive? – Juan Feb 11 '11 at 21:42
He sets no more restrictions, so there should be no restrictions. – FUZxxl Feb 11 '11 at 22:03
@Dogbert, @Juan: as FUZxxl said, there are no more restrictions; you can assume that the input is always positive if that helps you. – Eelvex Feb 11 '11 at 22:19
@Dogbert: I thought you meant pow(x,y) but now it's too late to take it back :) – Eelvex Feb 11 '11 at 22:37

6 Answers

up vote 2 down vote accepted

dc 1 or 5 chars

In GNU dc, there is the operator |, doing exactly this. Quoted from the manual:

| Pops three values and computes a modular exponentiation. The first value popped is used as the reduction modulus; this value must be a non-zero number, and should be an integer. The second popped is used as the exponent; this value must be a non-negative number, and any fractional part of this exponent will be ignored. The third value popped is the base which gets exponentiated, which should be an integer. For small integers this is like the sequence Sm^Lm%, but, unlike ^, this command will work with arbitrarily large exponents.

You may assign it to "function" as following:

[|]sf

(also 5 chars...) This assigns | to f. You may call it like lfx.

share|improve this answer

Python - 5 chars

f=pow

Test

>>> f(10**50, 10**50, 54123)
46555L
>>> f(5**99, 10**99, 777)
1L
share|improve this answer
On the one hand, it is implicit that build-in functions that exactly solve the problem should not be used; on the other hand, I learned a new option of pow. – Eelvex Feb 11 '11 at 22:36

Haskell, 65

Takes the liberty of taking the modulus only on set bits of binary b, but given the range constraints that won't be a problem.

f a b c|b==0=1|odd b=mod(a*f a(b-1)c)c|0<1=f(mod(a^2)c)(div b 2)c
share|improve this answer

Java 56 chars

My humble attempt with Java.

Object a(BigInteger...a){return a[0].modPow(a[1],a[2]);}

How to use :

a(new BigInteger("95"), new BigInteger("56"), new BigInteger("67"));
share|improve this answer

Haskell (54)

f a=g a a
g d a b c|b>0=g(mod(d*a)c)a(b-1)c|True=mod d c

This is my algorithm:

  1. let d = a
  2. while b > 0:
    • d <- (d*a)%c
    • b --
  3. Return d%b
share|improve this answer
Not sure that algo can handle b=10^50 in less than 10 seconds. – J B Feb 11 '11 at 23:33

Python 80 Characters

def f(a,b,c):
 x=1;y=a
 while b:
  if b%2:x=(x*y)%c
  y=(y*y)%c;b/=2
 return x%c
share|improve this answer
Nice solution but not "golfy" enough for an upvote. – Eelvex Feb 12 '11 at 22:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.