0

I am in the midst of learning 'Modular Exponentiation' and I have to now write the code to bring the two together, within a calculator program within my VB 2013.

However, as I am very new indeed to this but my studies rely on this module, I'm hoping some kind souls can give me some pointers / guidance.

I've created the easy part of the form design;

http://s2.postimg.org/ppyvvx9q1/mod_vb_design.jpg

Now I need to write a function to evaluate Modular Exponentiation using the below, however the hypothetical user must be able to choose ANY 'Power' figure, even if they were to choose a power number above for below example of 1-4.

NextPow = 1     Solution = 13 * 1 mod 53 = 13
NextPow = 2     Solution = 13 * 13 mod 53 = 10
NextPow = 3     Solution = 13 * 10 mod 53 = 24
NextPow = 4     Solution = 13 * 24 mod 53 = 47

I'm a little lost and hoping somebody can offer me some guidance, I've watched so many videos and researched that many sites r.e. modulo but I think I've just confused myself.

If there's any other aspect of the above that isn't explained correctly by myself in your mind, then please don't hesitate to let me know and I will try to re-clarify.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
PhoenixUK
  • 1
  • 1
  • Is the base always 13? – KSFT Feb 28 '15 at 16:03
  • Hi KSFT, thank you for the reply much appreciated. It is indeed always 13 in this particular instance. – PhoenixUK Feb 28 '15 at 16:07
  • Do you need to use the efficient modular exponentiation method or can you just calculate the result of exponentiation and then take it modulo the other number? – KSFT Feb 28 '15 at 16:11
  • Do you have to write the code yourself? If not, the `BigInteger` data type has a built-in [ModPow shared method](https://msdn.microsoft.com/en-us/library/system.numerics.biginteger.modpow(v=vs.100).aspx#pow). Check it out and see if it helps you. – Josh Part Feb 28 '15 at 16:16
  • There is no mention of efficient modular exponentiation, so I have to take it that it's just the most straight forward of the two. Bear with me, I'm sooo fresh to all this it's unbelievable :) – PhoenixUK Feb 28 '15 at 16:21
  • @JoshPart yes I do unfortunately but I need to increase my understanding more and by physically doing the code, it will help once I get my middle aged brain up to that kind of speed. – PhoenixUK Feb 28 '15 at 16:23
  • Although @JoshPart it wouldn't harm for me to see how your way above would work and then show it to my lecturer and ask if that suffices? – PhoenixUK Feb 28 '15 at 16:26
  • You are supposed to calculate X * Math.Pow(X, NextPow) Mod Y where X = 13 and Y = 53. The mysterious 1, 13, 10, 24 is a shortcut to solve it with integers instead of floating point. It is the value of Pow(X, NextPow) Mod Y. – Hans Passant Feb 28 '15 at 19:01
  • @HansPassant So if it's as you allude to in your comment, how do I go about inputting this in to my VB to enable the user to input the Power of their choice and then when the user selects the calculate button, it will input the list of mod mathematical answers in the resulting list box please? I have to have the calculator work out 11 mod lines in effect and list them in the list box if that makes sense. – PhoenixUK Mar 01 '15 at 17:14

1 Answers1

0

In VB, ^ is the exponentiation operator (https://msdn.microsoft.com/en-us/library/zh100ckf.aspx), and Mod is the modulo operator (https://msdn.microsoft.com/en-us/library/se0w9esz.aspx), so you should be able to just do something like this:

Solution = 13 ^ exponent Mod modulo
KSFT
  • 1,774
  • 11
  • 17
  • KSFT, ok so now i have that narrowed down to your solution, am I able to ask which section of my form I place this in, to then be able to input the chosen 'Enter Power', and output to the Divisor text box and Calculate the Remainder finally putting the results in the list box? *as per the linked image of my form* Thank you – PhoenixUK Feb 28 '15 at 16:51
  • I don't know how to use forms in VB (or really anything in VB), but you should get input, calulate the result(s), and output it. You should put that code in the event method/function/procedure/whatever that gets called when the button is clicked. – KSFT Feb 28 '15 at 16:53
  • @PhoenixUK Whoops, I forgot to use "@PhoenixUK" in that last comment. Does my answer work? – KSFT Feb 28 '15 at 19:30
  • I can't get it working no not within the design of my windowsform within VB 2013 :( it's saying that the use of the ^ is a syntax error. – PhoenixUK Mar 01 '15 at 14:16