Q:

Modular arithmetic in VB

I have this number x which i need to find in the (40 mod x) = 1
a possible answer for x is 3, or 39 as it goes into the number 40 and leaves a remainder of 1.
What kind of code would I need if I was to find all possible answers of x?

A:

Mathematically, to solve (a mod x) = b, just find all of the divisors of a-b that aren't divisors of a. e.g. for (40 mod x) = 1, find the divisors of 40 - 1 (i.e. 39), which are 3, 13, and 39. The divisors of 40 are 2, 4, 5, 8, 10, 20, 40. None of the numbers in the first set are in the second, so the solutions are 3, 13, and 19.
For (40 mod x) = 5, you find the divisors of 40 - 5 (i.e. 35), which are 5, 7, and 35. 5 is on the list of divisors of 40, but the other two aren't, so the solutions are 7 and 35.
Of course, for such small numbers, it's more work to find all of the factors of a and a-b than it is to simply do all of the trial divisions of a by x, so the right way to solve your problem is to take exactly the question you asked and put it into code (forgive my VB, I haven't written any in the past 15 years or so...)
for x = 2 to 39
   if (40 % x) = 1
      MsgBox(x)
   end if
 next

