-5

Can anyone explain me how modular arithmetic works in programming? I know it is used to operate on large values.

For example, to calculate the binomial coefficient of B(1000000, 2) using int data-type. i assume we couldn't multiply using int data-type, since it involves calculating factorials of big values like 1000000! which has millions of digits, which don't fit in an 32-bit or 64-bit integer.

I know modular arithmetic is used to these type of problems, But i don't understand exactly how that works.

  • 3
    it just means "remainder". `3 modulo 2 = 1`. e.g. "3 divided by 2 gives a remainder (aka modulo) of 1" – Marc B Oct 06 '14 at 19:46
  • 2
    I literally googled your title and found answers. Although, I didn't find this answered on Stackoverflow... – But I'm Not A Wrapper Class Oct 06 '14 at 19:47
  • In general, people can't answer such broad questions. "HOW DOES A CAT WORK?" Well, there are a ton of parts to explain to answer that. "How does a cat get oxygen?" Through the lungs, breathing every 15 seconds and...so on. Understand? – Alex K Oct 06 '14 at 19:47
  • 1 example:: (3*6)%5=((3%5)*(6%5))%5 – coderzz027 Oct 06 '14 at 19:47
  • Ref. [Modulo operation](http://en.wikipedia.org/wiki/Modulo_operation) for a general background. The exact operation can vary between languages, particularly when there are negative operands or dividend sign-matching, but such cursory reading is generally advisable. – user2864740 Oct 06 '14 at 19:48
  • @coderzz027 Not helping, if you don't know what modular arithmetic does ;) – MrHug Oct 06 '14 at 19:49
  • @MrHug lol.. :P Just a try.. :P – coderzz027 Oct 06 '14 at 20:12
  • @MarcB, if you want to talk like a Mathematician, then don't use "modulo" as the name of an operation. Use it as a modifier for the "congruency" operator: "3 is congruent to 1 modulo 2", or `3≡1 (mod) 2` – Solomon Slow Oct 06 '14 at 20:32
  • @jameslarge: who said anything about mathematician? this is a programming forum. don't barge into a swimming pool and expect to find a desert. – Marc B Oct 06 '14 at 20:40

1 Answers1

2

The modulo operation is a simple operation that calculates the remainder of a division. For instance 5 % 3 = 2 as dividing 5 by 3 will give you a a remainder of .

A common usecase for this is checking whether a number is even or odd. number % 2 == 0 means the number is even.

For more information please check Wikipedia.

MrHug
  • 1,315
  • 10
  • 27