C# How do i reverse modulus when using a ^ sign - c#

At the moment i have this formula:
13^2 mod 5 = 4
I want to calculate back the 2 here like:
13^X mod 5 = 4
X = ?
I found several formula's/codes to do this online but i didn't find any which do it with a ^ symbol.
Would appreciate some help
My client received everything besides the TEST_PRIVATE so i want to calculate that.
This is the code i use (server sided)
(This is the encryption, not the decryption)
string TEST_GENERATED = "13";
string TEST_PRIVATE = "2";
string TEST_PRIME = "5";
BigIntegerTEST TESTMOD_1 = new BigIntegerTEST(TEST_GENERATED, 10);
BigIntegerTEST TESTMOD_2 = new BigIntegerTEST(TEST_PRIVATE, 10);
BigIntegerTEST TESTMOD_3 = new BigIntegerTEST(TEST_PRIME, 10);
BigIntegerTEST TESTMOD_4 = TESTMOD_1.modPow(TESTMOD_2, TESTMOD_3);
So basicly i want to reverse TESTMOD_4 to TESTMOD_2
By only using TESTMOD_4, TESTMOD_3 and TESTMOD_1
(I know modPow usually has 3 parameters i'm using a special class for it)
TDLR;
Working example:
(6 + 7) MOD 10 = 3
(3 - 6 + 10) MOD 10 = 7
This is the main result i want:
( I want to retrieve the 7)
(6^7) MOD 10 = 6
? = 7

it is here and much simple like this function:
public double DoCalc(double Number1, double Number2, double Number3)
{ return (Math.Pow(Number1, Number2)) % Number3; }
Then call it from the button click event, like this:
private void btmDoCalc_Click(object sender, EventArgs e)
{
// Here show it up in message box directly.
MessageBox.Show("DoCalc= " + DoCalc(13, 2, 5).ToString());
// Here assign it to some double variables.
double N1 = 0, N2 = 0, N3 = 0;
N1 = DoCalc(13, 2, 4);
N2 = DoCalc(13, 2, 3);
N3 = DoCalc(13, 2, 2);
MessageBox.Show("DoCalc= " + N1);
MessageBox.Show("DoCalc= " + N2);
MessageBox.Show("DoCalc= " + N3);
}
Here is some more images for the results.
I hope this answers your questions ^_^

Related

Get range in multiplication of 5

I have a number. For instance, my number is 19 . Then I want to populate my drop down with range in multiplication of 5. So my dropdownlist will consist of items of:
1-5
6-10
11-15
16-19
I tried modulus and division, however, I can't seems to get the range. Is there a fixed method?
Sample code
List<string> range = new List<string>();
int number = 19;
int numOfOccur = (19/5);
for (int i = 1; i < numOfOccur ; i++)
{
range.Add(i + " - " + (i * 5))
}
Sometime I think that old school code, without fancy linq is a bit more clear
int maximum = 19;
int multiple = 5;
int init = 1;
while (init + multiple <= maximum )
{
string addToDDL = init.ToString() + "-" + (init + multiple - 1).ToString();
Console.WriteLine(addToDDL);
init += multiple;
}
if(init <= maximum)
{
string last = init.ToString() + "-" + maximum.ToString();
Console.WriteLine(last);
}
Linq solution (modern techs allow us to put it consize):
int number = 19;
int div = 5;
List<string> range = Enumerable
.Range(0, number / div + (number % div == 0 ? 0 : 1))
.Select(i => $"{i * div + 1} - {Math.Min((i + 1) * div, number)}")
.ToList();
Test
Console.Write(string.Join(Environment.NewLine, range));
Returns
1 - 5
6 - 10
11 - 15
16 - 19
When using modulo arithmetics, do not forget about remainders: you have an error in int numOfOccur = (19/5); line. It should be
int numOfOccur = 19 / 5 + (19 % 5 == 0 ? 0 : 1);
for the last incomplete 16 - 19 range to be proceeded.
Add this package to your project : https://www.nuget.org/packages/System.Interactive/
Then you can do this:
IEnumerable<IList<int>> buffers2 = Enumerable.Range(1, 19).Buffer(5);
IList<int>[] result2 = buffers2.ToArray();
// { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, ...
Don't forget to add System.Interactive namespace to your using block.

x++ operator for playing gif animations: How do it work together in this example? [duplicate]

I have next code
int a,b,c;
b=1;
c=36;
a=b%c;
What does "%" operator mean?
It is the modulo (or modulus) operator:
The modulus operator (%) computes the remainder after dividing its first operand by its second.
For example:
class Program
{
static void Main()
{
Console.WriteLine(5 % 2); // int
Console.WriteLine(-5 % 2); // int
Console.WriteLine(5.0 % 2.2); // double
Console.WriteLine(5.0m % 2.2m); // decimal
Console.WriteLine(-5.2 % 2.0); // double
}
}
Sample output:
1
-1
0.6
0.6
-1.2
Note that the result of the % operator is equal to x – (x / y) * y and that if y is zero, a DivideByZeroException is thrown.
If x and y are non-integer values x % y is computed as x – n * y, where n is the largest possible integer that is less than or equal to x / y (more details in the C# 4.0 Specification in section 7.8.3 Remainder operator).
For further details and examples you might want to have a look at the corresponding Wikipedia article:
Modulo operation (on Wikipedia)
% is the remainder operator in many C-inspired languages.
3 % 2 == 1
789 % 10 = 9
It's a bit tricky with negative numbers. In e.g. Java and C#, the result has the same sign as the dividend:
-1 % 2 == -1
In e.g. C++ this is implementation defined.
See also
Wikipedia/Modulo operation
References
MSDN/C# Language Reference/% operator
That is the Modulo operator. It will give you the remainder of a division operation.
It's the modulus operator. That is, 2 % 2 == 0, 4 % 4 % 2 == 0 (2, 4 are divisible by 2 with 0 remainder), 5 % 2 == 1 (2 goes into 5 with 1 as remainder.)
It is the modulo operator. i.e. it the remainder after division 1 % 36 == 1 (0 remainder 1)
That is the modulo operator, which finds the remainder of division of one number by another.
So in this case a will be the remainder of b divided by c.
It's is modulus, but you example is not a good use of it. It gives you the remainder when two integers are divided.
e.g. a = 7 % 3 will return 1, becuase 7 divided by 3 is 2 with 1 left over.
It is modulus operator
using System;
class Test
{
static void Main()
{
int a = 2;
int b = 6;
int c = 12;
int d = 5;
Console.WriteLine(b % a);
Console.WriteLine(c % d);
Console.Read();
}
}
Output:
0
2
is basic operator available in almost every language and generally known as modulo operator.
it gives remainder as result.
Okay well I did know this till just trying on a calculator and playing around so basically:
5 % 2.2 = 0.6 is like saying on a calculator 5/2.2 = 2.27 then you multiply that .27 times the 2.27 and you round and you get 0.6. Hope this helps, it helped me =]
Nobody here has provided any examples of exactly how an equation can return different results, such as comparing 37/6 to 37%6, and before some of you get upset thinking that you did, pause for a moment and think about it for a minute, according to Dirk Vollmar in here the int x % int y parses as (x - (x / y) * y) which seems fairly straightforward at first glance, but not all Math is performed in the same order.
Since not every equation has it's proper brackets, some Schools will teach that the Equation is to be parsed as ((x - (x / y)) * y) whilst other Schools teach (x - ((x / y) * y)).
Now I experimented with my example (37/6 & 37%6) and figured out which selection was intended (it's (x - ((x / y) * y))), and I even displayed a nicely built if Loop (even though I forgot every End of Line Semicolon) to simulate the Divide Equation at the most Fundamental Scale, as that was in fact my point, the Equation is similar, yet Fundamentally different.
Here's what I can remember from my deleted Post (this took me around an Hour to Type from my Phone, indents are Double Spaced)
using System;
class Test
{
static void Main()
{
float exact0 = (37 / 6); //6.1666e∞
float exact1 = (37 % 6); //1
float exact2 = (37 - (37 / 6) * 6); //0
float exact3 = ((37 - (37 / 6)) * 6); //0
float exact4 = (37 - ((37 / 6) * 6)); //185
int a = 37;
int b = 6;
int c = 0;
int d = a;
int e = b;
string Answer0 = "";
string Answer1 = "";
string Answer2 = "";
string Answer0Alt = "";
string Answer1Alt = "";
string Answer2Alt = "";
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("a: " + a + ", b: " + b + ", c: " + c + ", d: " + d + ", e: " + e);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.WriteLine("Init Complete, starting Math...");
Loop
{
if (a !< b) {
a - b;
c +1;}
if else (a = b) {
a - b;
c +1;}
else
{
String Answer0 = c + "." + a; //6.1
//this is = to 37/6 in the fact that it equals 6.1 ((6*6=36)+1=37) or 6 remainder 1,
//which according to my Calculator App is technically correct once you Round Down the .666e∞
//which has been stated as the default behavior of the C# / Operand
//for completion sake I'll include the alternative answer for Round Up also
String Answer0Alt = c + "." + (a + 1); //6.2
Console.WriteLine("Division Complete, Continuing...");
Break
}
}
string Answer1 = ((d - (Answer0)) * e); //185.4
string Answer1Alt = ((d - (Answer0Alt​)) * e); // 184.8
string Answer2 = (d - ((Answer0) * e)); //0.4
string Answer2Alt = (d - ((Answer0Alt​) * e)); //-0.2
Console.WriteLine("Math Complete, Summarizing...");
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.Read();
}
}
This also CLEARLY demonstrated how an outcome can be different for the exact same Equation.

Encryption/Decryption crash

I have to write a "encryption" program for my C# intro class. I'm encountering 2 problems:
When a negative is entered or it calculates a negative during the decryption process, it crashes. - it returns and unhandled exception error: System.FormatException: Input string was not in a correct format.
The decryption should be the reverse of the encryption without using the original variable. How do you reverse a remainder calculation?
I know it's part of the do while loop that's the issue, I'm just not sure how to continue to prompt for a value if a negative is entered and if encountered during the decryption calculation to keep it from crashing. Any guidance would be much appreciated. Thanks for taking a look at it!
public class MainClass
{
public static void Main(string[] args)
{
int num = 0;
int dnum = 0;
do
{
Console.Write("Please enter a non-negative integer to encrypt: ");
num = Convert.ToInt32(Console.ReadLine());
string numstr = Convert.ToString(num);
string num1 = Convert.ToString(numstr.Substring(0, 1));
string num2 = Convert.ToString(numstr.Substring(1, 1));
string num3 = Convert.ToString(numstr.Substring(2, 1));
string num4 = Convert.ToString(numstr.Substring(3, 1));
int enum1 = ((Convert.ToInt32(num1) + 7) % 10);
int enum2 = ((Convert.ToInt32(num2) + 7) % 10);
int enum3 = ((Convert.ToInt32(num3) + 7) % 10);
int enum4 = ((Convert.ToInt32(num4) + 7) % 10);
Console.WriteLine("Encrypted Integer: {0:D4}", (1000 * enum3 + 100 * enum4 + 10 * enum1 + enum2));
Console.Write("Please enter a non-negative integer to decrypt: ");
dnum = Convert.ToInt32(Console.ReadLine());
string dnumstr = Convert.ToString(dnum);
string num1d = Convert.ToString(dnumstr.Substring(0, 1));
string num2d = Convert.ToString(dnumstr.Substring(1, 1));
string num3d = Convert.ToString(dnumstr.Substring(2, 1));
string num4d = Convert.ToString(dnumstr.Substring(3, 1));
int dnum1 = ((Convert.ToInt32(num1d) - 7) * 10);
int dnum2 = ((Convert.ToInt32(num2d) - 7) * 10);
int dnum3 = ((Convert.ToInt32(num3d) - 7) * 10);
int dnum4 = ((Convert.ToInt32(num4d) - 7) * 10);
Console.WriteLine("Decrypted Integer: {0:D4}", (1000 * dnum1 + 100 * dnum2 + 10 * dnum3 + dnum4));
} while (num > 0);
} // end Main
}// end class
The policy is, do not proceed until user enters a 'correct' input. Here is code sample, note that I use numstr[0] - an index to get the first char instead of numstr.Substring(0, 1) so the code looks cleaner.
int num = 0;
bool isValid = false;
do
{
Console.Write("Please enter 4 digits to encrypt: ");
string numstr = Console.ReadLine();
if (numstr.Length == 4)
{
isValid = Char.IsDigit(numstr[0]) && Char.IsDigit(numstr[1])
&& Char.IsDigit(numstr[2]) && Char.IsDigit(numstr[3]);
}
if (isValid)
num = Convert.ToInt32(input);
}
while (!isValid);
As for your 2nd question, you can't use multiplication to reverse a remainder calculation ((d+7)%10), you should again use remainder operator (d+10-7)%10, the additional 10 is added to keep it from getting negative result.
There is another bug in your decryption process, you can turn to your debugger for help.

Index was outside the bounds of the array - integers

I have just run across a problem which is common but I'm not sure why it's happening in this instance.
string s;
int c1, c2, c3, c4;
private void button2_Click(object sender, EventArgs e)
{
String number;
s = textBox1.Text;
int[] d = s.Select(c => (int)c - (int)'0').ToArray();
try
{
c1 = (4 * d[1] + 10 * d[2] + 9 * d[3] + 2 * d[4] + d[5] + 7 * d[6]) % 11;
c2 = (7 * d[1] + 8 * d[2] + 7 * d[3] + d[4] + 9 * d[5] + 6 * d[6]) % 11;
c3 = (9 * d[1] + d[2] + 7 * d[3] + 8 * d[4] + 7 * d[5] + 7 * d[6]) % 11;
c4 = (d[1] + 2 * d[2] + 9 * d[3] + 10 * d[4] + 4 * d[5] + d[6]) % 11;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
number = d[1]+d[2]+d[3]+d[4]+d[5]+d[6]+c1+c2+c3+c4.ToString();
textBox2.Text = number;
}
It will accept the number in the first TextBox(es) fine. As soon as it moves onto the catch section it will pop up with an error Index was outside the bounds of the array Is there something obvious I'm missing? or is this quite unique to my program?
I bellieve that you think your array goes from 1 to 6.
Its from 0 to 5.
You should ensure that your TextBox contains at least 6 characters else it gives an exception:
if(textBox1.Text.Length >= 6)
{
//your code here
}
else
MessageBox.Show("You must insert at least 6 characters");
And then remember that the index of the array starts from 0 not 1.
How many chars are in the input string s = textBox1.Text;?
You don't perform any check on the user input.
For example
textBox1.Text = "1234"; // only 4 digits
then, when you try to use index 4/5/6 you get the error.
Of course, you should also consider that arrays indexes start at zero not at one.
In my input above, you will have only index from 0 to 3.
A simple check should be (Assuming you've already ruled out non-numeric data by other means)
s = textBox1.Text;
if(s.Length != 6)
MessageBox.Show("6 digits required!");
else
.......

What does the '%' operator mean?

I have next code
int a,b,c;
b=1;
c=36;
a=b%c;
What does "%" operator mean?
It is the modulo (or modulus) operator:
The modulus operator (%) computes the remainder after dividing its first operand by its second.
For example:
class Program
{
static void Main()
{
Console.WriteLine(5 % 2); // int
Console.WriteLine(-5 % 2); // int
Console.WriteLine(5.0 % 2.2); // double
Console.WriteLine(5.0m % 2.2m); // decimal
Console.WriteLine(-5.2 % 2.0); // double
}
}
Sample output:
1
-1
0.6
0.6
-1.2
Note that the result of the % operator is equal to x – (x / y) * y and that if y is zero, a DivideByZeroException is thrown.
If x and y are non-integer values x % y is computed as x – n * y, where n is the largest possible integer that is less than or equal to x / y (more details in the C# 4.0 Specification in section 7.8.3 Remainder operator).
For further details and examples you might want to have a look at the corresponding Wikipedia article:
Modulo operation (on Wikipedia)
% is the remainder operator in many C-inspired languages.
3 % 2 == 1
789 % 10 = 9
It's a bit tricky with negative numbers. In e.g. Java and C#, the result has the same sign as the dividend:
-1 % 2 == -1
In e.g. C++ this is implementation defined.
See also
Wikipedia/Modulo operation
References
MSDN/C# Language Reference/% operator
That is the Modulo operator. It will give you the remainder of a division operation.
It's the modulus operator. That is, 2 % 2 == 0, 4 % 4 % 2 == 0 (2, 4 are divisible by 2 with 0 remainder), 5 % 2 == 1 (2 goes into 5 with 1 as remainder.)
It is the modulo operator. i.e. it the remainder after division 1 % 36 == 1 (0 remainder 1)
That is the modulo operator, which finds the remainder of division of one number by another.
So in this case a will be the remainder of b divided by c.
It's is modulus, but you example is not a good use of it. It gives you the remainder when two integers are divided.
e.g. a = 7 % 3 will return 1, becuase 7 divided by 3 is 2 with 1 left over.
It is modulus operator
using System;
class Test
{
static void Main()
{
int a = 2;
int b = 6;
int c = 12;
int d = 5;
Console.WriteLine(b % a);
Console.WriteLine(c % d);
Console.Read();
}
}
Output:
0
2
is basic operator available in almost every language and generally known as modulo operator.
it gives remainder as result.
Okay well I did know this till just trying on a calculator and playing around so basically:
5 % 2.2 = 0.6 is like saying on a calculator 5/2.2 = 2.27 then you multiply that .27 times the 2.27 and you round and you get 0.6. Hope this helps, it helped me =]
Nobody here has provided any examples of exactly how an equation can return different results, such as comparing 37/6 to 37%6, and before some of you get upset thinking that you did, pause for a moment and think about it for a minute, according to Dirk Vollmar in here the int x % int y parses as (x - (x / y) * y) which seems fairly straightforward at first glance, but not all Math is performed in the same order.
Since not every equation has it's proper brackets, some Schools will teach that the Equation is to be parsed as ((x - (x / y)) * y) whilst other Schools teach (x - ((x / y) * y)).
Now I experimented with my example (37/6 & 37%6) and figured out which selection was intended (it's (x - ((x / y) * y))), and I even displayed a nicely built if Loop (even though I forgot every End of Line Semicolon) to simulate the Divide Equation at the most Fundamental Scale, as that was in fact my point, the Equation is similar, yet Fundamentally different.
Here's what I can remember from my deleted Post (this took me around an Hour to Type from my Phone, indents are Double Spaced)
using System;
class Test
{
static void Main()
{
float exact0 = (37 / 6); //6.1666e∞
float exact1 = (37 % 6); //1
float exact2 = (37 - (37 / 6) * 6); //0
float exact3 = ((37 - (37 / 6)) * 6); //0
float exact4 = (37 - ((37 / 6) * 6)); //185
int a = 37;
int b = 6;
int c = 0;
int d = a;
int e = b;
string Answer0 = "";
string Answer1 = "";
string Answer2 = "";
string Answer0Alt = "";
string Answer1Alt = "";
string Answer2Alt = "";
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("a: " + a + ", b: " + b + ", c: " + c + ", d: " + d + ", e: " + e);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.WriteLine("Init Complete, starting Math...");
Loop
{
if (a !< b) {
a - b;
c +1;}
if else (a = b) {
a - b;
c +1;}
else
{
String Answer0 = c + "." + a; //6.1
//this is = to 37/6 in the fact that it equals 6.1 ((6*6=36)+1=37) or 6 remainder 1,
//which according to my Calculator App is technically correct once you Round Down the .666e∞
//which has been stated as the default behavior of the C# / Operand
//for completion sake I'll include the alternative answer for Round Up also
String Answer0Alt = c + "." + (a + 1); //6.2
Console.WriteLine("Division Complete, Continuing...");
Break
}
}
string Answer1 = ((d - (Answer0)) * e); //185.4
string Answer1Alt = ((d - (Answer0Alt​)) * e); // 184.8
string Answer2 = (d - ((Answer0) * e)); //0.4
string Answer2Alt = (d - ((Answer0Alt​) * e)); //-0.2
Console.WriteLine("Math Complete, Summarizing...");
Console.WriteLine("37/6: " + exact0);
Console.WriteLine("37%6: " + exact1);
Console.WriteLine("(37 - (37 / 6) * 6): " + exact2);
Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3);
Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4);
Console.WriteLine("Answer0: " + Answer0);
Console.WriteLine("Answer0Alt: " + Answer0Alt);
Console.WriteLine("Answer1: " + Answer1);
Console.WriteLine("Answer0Alt: " + Answer1Alt);
Console.WriteLine("Answer2: " + Answer2);
Console.WriteLine("Answer2Alt: " + Answer2Alt);
Console.Read();
}
}
This also CLEARLY demonstrated how an outcome can be different for the exact same Equation.

Categories