-
'A' mod 'B' Problem
Hi there ppl. This might be very basic to ask but i have only just started college and i have been given homework in my programming class.
I have been asked to make a program which completes basic mathematical functions. One of my tasks is to make a code that can answer - ('A' mod 'B').
I know what the questions means and i would possibly know how to code it, but i do not know the formula for ('A' mod 'B')
One of my questions is to write 'A' to the power 6 so i wrote - (num1^6) and it works fine.
By the way, 'A' is the number 12 and 'B' is the number 5. so obviously the answer is 2 but i need a formula for it.
Any help would be greatly Appreciated
Thank You :) :wave:
-
Re: 'A' mod 'B' Problem
A mod B = the remainder of the division A/B = the remainder of the division (A-B)/B assuming A-B is positive. To code it you could use this (crude) identity, or you could also infer another without too much hassle. It just takes a bit of algebra and reasoning. That is, the formula you're looking for is simple enough that giving you any good hint would just give it away completely, and probably ruin the exercise. You could also try to code the algorithm your brain uses--how did you know 12 mod 5 = 2? If you reasoned it well, you can make a computer do the same thing.
I'm not really sure what you mean about the exponent stuff, most programming languages have powerful exponent operators built in so I guess that's what you should use?
-
Re: 'A' mod 'B' Problem
You can derive a simple enough formula from reasoning exactly how you would calculate it, just like jemidiah said.
One hint: the formula does not only involve the usual +, -, * and / ...
-
Re: 'A' mod 'B' Problem
in the IDE click on Help search and type in
mod
then press enter
-
Re: 'A' mod 'B' Problem
Hmm, I (and I think jemidiah aswell) was under the impression the poster wants a formula for the Mod operator, instead of actually using the Mod operator... But maybe we're mistaken..?
If you don't really care how the operation is done you can use VB's inbuilt "Mod" operator:
If you need to find a different mathematical expression that will allow you to calculate A Mod B without actually using the "Mod" operator you can use this:
Code:
Result = A - B * Math.Floor(A/B)
-
Re: 'A' mod 'B' Problem
First off we need more information. For one thing, we need to know the language. We assume VB, and NickThissen has gone further in assuming VB.NET, but is that correct?
Also, what is the objective of the assignment? There are at least two VERY simple ways to get the Mod that you ask for (one being the Mod statement and the other being integer division followed by a simple subtraction), but that seems too trivial. Are you trying to replicate manual long division? That would take quite a bit more coding (though only quite a bit because the other solutions are one and two lines, such that a ten line program would be 5-10x as many lines).