LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-02-2009, 06:10 AM   #1
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Rep: Reputation: 16
How to calculate ((a+b)!/a!b!) mod p


Hi

I am looking for an efficient way to calculate ((a+b)!/a!b!) mod p. p is prime.

for example if a=10 and b=7 and p=3 then we need to find (17!/10!.7!) mod 3
answer =2

The above can be reduced to (17.16.15.14.13.12.11/7.6.5.4.3.2.1) mod 3

The problem i am facing is a and b can have a very high value i.e 1<=a<=1000000000 and 1<=b<=1000000000 and value of p is say 531169.
So how would i programmatically calculate the value, since intermediary calculations might overflow an int variable( i am using c++).
Since (a/b)mod p != ((a%p)/(b%p)) mod p. Right ?

Is there a trick involved ?

Thanks for your patience
Regards
lali
 
Old 05-02-2009, 05:14 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
Sounds like homework.
 
Old 05-02-2009, 05:17 PM   #3
giftlftr_23
Member
 
Registered: Oct 2008
Location: I live where I'm currently standing on
Distribution: fedora, opensuse
Posts: 34

Rep: Reputation: 17
1000000000! is a pretty big number. none of the basic data types in c/c++ can handle big value. consider using a library for that, here http://gmplib.org/
 
Old 05-02-2009, 05:30 PM   #4
astrogeek
Member
 
Registered: Oct 2008
Distribution: Slackware: 12.1, 13.0, 13.1, 13.37, 64
Posts: 475

Rep: Reputation: 88
Efficiency is one problem, precision is another...

Quote:
Originally Posted by lali.p View Post
I am looking for an efficient way to calculate ((a+b)!/a!b!) mod p. p is prime.
You might find some of the ideas here to be useful.

You definitely need a big number library for C/C++, or use bc which has arbitrary precision. Hint: Think of the difference in results between using libraries which support arbitrary number SIZE, and using a floating point library which would introduce a limit on PRECISION... (did I get that right?).

My first thoughts as programmer would be to craft a carefully designed recursive algorithm for the job... good luck!

EDIT:
Thinking about this has stirred a few brain cells that I have not recently used, but the key for such a problem (once you have the right numeric types libraries in place) will be a tail-recursive algorithm as explained in this article on Tail Recursion. This is what I had in mind with my comment about using a carefully designed recursive algorithm for the job. And it should be simple for a factorial calculation - I thinks...

If you write a tail-recursive algorithm for the factorials using the big number libraries, it will be optimized into a very efficient iterative function by the compiler (gcc can handle this AFAIK).
Hope this points you in a good direction!
/EDIT:

Last edited by astrogeek; 05-02-2009 at 06:00 PM. Reason: More info...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calculate totals in shell wolfipa Programming 7 04-02-2008 06:12 PM
Apache2 mod vhost_alias - problems with .htaccess mod rewrite d_t_baker Linux - Server 1 08-16-2007 07:32 PM
Calculate e^x in Java ckoniecny Programming 6 07-25-2007 10:28 AM
How to calculate bandwidth on linux epsharma Linux - Software 4 07-02-2006 10:45 AM
How to calculate Server power needs psychobyte Linux - Hardware 1 08-11-2005 02:59 AM


All times are GMT -5. The time now is 03:21 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration