-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
68 lines (49 loc) · 1.89 KB
/
Program.cs
File metadata and controls
68 lines (49 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Numerics; //To use BigInteger
#region working with big integers
const int width = 40;
WriteLine("ulong.MaxValue vs a 30-digit BigInteger");
WriteLine(new string('-', width));
ulong big = ulong.MaxValue;
WriteLine($"{big,width:N0}");
BigInteger bigger = BigInteger.Parse("123456789012345678901234567890");
WriteLine($"{bigger,width:N0}");
#endregion
#region Multiplying big integers
WriteLine();
WriteLine("Multiplying big integers");
int number1 = 2_000_000_000;
int number2 = 2;
WriteLine($"number1: {number1:N0}");
WriteLine($"number2: {number2:N0}");
WriteLine($"number1 * number2: {number1 * number2:N0}"); // This overflows an int
WriteLine($"Math.BigMul(number1, number2): {Math.BigMul(number1, number2):N0}"); // This works correctly, because it returns a long
WriteLine($"int.BigMul(number1, number2): {int.BigMul(number1, number2):N0}"); // This works correctly, because it returns a long
#endregion
#region working with complex numbers
Complex c1 = new(real: 4, imaginary: 2);
Complex c2 = new(real: 3, imaginary: 7);
Complex c3 = c1 + c2;
WriteLine();
WriteLine($"{c1} added to {c2} is {c3}"); //using old method
// using new method
WriteLine("{0} + {1}i added to {2} + {3}i is {4} + {5}i",
c1.Real, c1.Imaginary,
c2.Real, c2.Imaginary,
c3.Real, c3.Imaginary);
#endregion
#region random numbers with the random class
Random r = Random.Shared; // static property to get a shared instance
WriteLine();
int dieRoll = r.Next(minValue: 1, maxValue: 7); // roll a 6-sided die
WriteLine($"Random die roll: {dieRoll}");
double randomReal = r.NextDouble(); // random real number between 0.0 and 1.0
WriteLine($"Random double: {randomReal}");
byte[] arrayOfBytes = new byte[256];
r.NextBytes(arrayOfBytes);
Write("Random bytes: ");
for (int i = 0; i < arrayOfBytes.Length; i++)
{
Write($"{arrayOfBytes[i]}:X2"); //:XQ2 formats as hexadecimal with 2 digits
}
WriteLine();
#endregion