Skip to content

Commit 903830c

Browse files
ClémentClément
authored andcommitted
Working on dictionary notes.
1 parent 5210294 commit 903830c

9 files changed

Lines changed: 868 additions & 714 deletions

File tree

source/code/projects/Dictionary/Dictionary/Dictionary.cs

Lines changed: 248 additions & 230 deletions
Large diffs are not rendered by default.
Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
public static class PrimeHelper
22
{
3-
public static bool IsPrime(int n)
4-
{
5-
bool returned = true;
6-
if (n < 2)
7-
returned = false;
8-
if (n == 2 || n == 3)
9-
returned = true;
10-
if (n % 2 == 0)
11-
returned = false;
12-
// We use uint so that we can compute larger primes, since
13-
// we know that i will not need to be negative.
14-
uint i = 3;
3+
public static bool IsPrime(int n)
4+
{
5+
bool returned = true;
6+
if (n < 2)
7+
returned = false;
8+
if (n == 2 || n == 3)
9+
returned = true;
10+
if (n % 2 == 0)
11+
returned = false;
12+
// We use uint so that we can compute larger primes, since
13+
// we know that i will not need to be negative.
14+
uint i = 3;
1515

16-
/*
17-
* Our goal is to find i > 1 and j > 1 such that
18-
* i * j = n
19-
* to prove that n is not prime.
20-
* Assuming i <= j, it suffices to look "up to" i * i <= n (i.e, so that i is less than √n).
21-
* We also exit our loop as soon as returned is proved false.
22-
*/
16+
/*
17+
* Our goal is to find i > 1 and j > 1 such that
18+
* i * j = n
19+
* to prove that n is not prime.
20+
* Assuming i <= j, it suffices to look "up to" i * i <= n (i.e, so that i is less than √n).
21+
* We also exit our loop as soon as returned is proved false.
22+
*/
2323

24-
while (i * i <= n && returned)
25-
{
26-
if (n % i == 0)
27-
returned = false;
28-
i += 2;
29-
}
30-
return returned;
24+
while (i * i <= n && returned)
25+
{
26+
if (n % i == 0)
27+
returned = false;
28+
i += 2;
3129
}
30+
return returned;
31+
}
3232

33-
public static int NextPrime(int n)
33+
public static int NextPrime(int n)
34+
{
35+
int returned = n;
36+
if (returned <= 2)
37+
{
38+
// The smallest prime greater than or equal to 2
39+
// is … 2!
40+
returned = 2;
41+
}
42+
else
3443
{
35-
int returned = n;
36-
if (returned <= 2)
37-
{
38-
// The smallest prime greater than or equal to 2
39-
// is … 2!
40-
returned = 2;
41-
}
42-
else
43-
{
44-
// Since 2 is the only even prime,
45-
// we make the returned value even
46-
// if it is divisible by 2.
47-
if (returned % 2 == 0)
48-
returned++;
44+
// Since 2 is the only even prime,
45+
// we make the returned value even
46+
// if it is divisible by 2.
47+
if (returned % 2 == 0)
48+
returned++;
4949

50-
// Then, we simply look for the next Prime value,
51-
// incrementing two by two.
52-
while (!IsPrime(returned))
53-
{
54-
returned += 2;
55-
}
56-
}
57-
return returned;
50+
// Then, we simply look for the next Prime value,
51+
// incrementing two by two.
52+
while (!IsPrime(returned))
53+
{
54+
returned += 2;
55+
}
5856
}
57+
return returned;
58+
}
5959
}

source/code/projects/Dictionary/Dictionary/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ static void Main(string[] args)
99
CDictionary<string, int> ht = new CDictionary<
1010
string,
1111
int
12-
>(13, CDictionary<string, int>.CollisionRes.Linear);
12+
>(13, CDictionary<string, int>.PSSType.Linear);
1313
// Key of type string, value of type int.
1414
ht.Add("one", 1);
1515
ht.Add("twenty", 20);
Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
public static class PrimeHelper
22
{
3-
public static bool IsPrime(int n)
4-
{
5-
bool returned = true;
6-
if (n < 2)
7-
returned = false;
8-
if (n == 2 || n == 3)
9-
returned = true;
10-
if (n % 2 == 0)
11-
returned = false;
12-
// We use uint so that we can compute larger primes, since
13-
// we know that i will not need to be negative.
14-
uint i = 3;
3+
public static bool IsPrime(int n)
4+
{
5+
bool returned = true;
6+
if (n < 2)
7+
returned = false;
8+
if (n == 2 || n == 3)
9+
returned = true;
10+
if (n % 2 == 0)
11+
returned = false;
12+
// We use uint so that we can compute larger primes, since
13+
// we know that i will not need to be negative.
14+
uint i = 3;
1515

16-
/*
17-
* Our goal is to find i > 1 and j > 1 such that
18-
* i * j = n
19-
* to prove that n is not prime.
20-
* Assuming i <= j, it suffices to look "up to" i * i <= n (i.e, so that i is less than √n).
21-
* We also exit our loop as soon as returned is proved false.
22-
*/
16+
/*
17+
* Our goal is to find i > 1 and j > 1 such that
18+
* i * j = n
19+
* to prove that n is not prime.
20+
* Assuming i <= j, it suffices to look "up to" i * i <= n (i.e, so that i is less than √n).
21+
* We also exit our loop as soon as returned is proved false.
22+
*/
2323

24-
while (i * i <= n && returned)
25-
{
26-
if (n % i == 0)
27-
returned = false;
28-
i += 2;
29-
}
30-
return returned;
24+
while (i * i <= n && returned)
25+
{
26+
if (n % i == 0)
27+
returned = false;
28+
i += 2;
3129
}
30+
return returned;
31+
}
3232

33-
public static int NextPrime(int n)
33+
public static int NextPrime(int n)
34+
{
35+
int returned = n;
36+
if (returned <= 2)
37+
{
38+
// The smallest prime greater than or equal to 2
39+
// is … 2!
40+
returned = 2;
41+
}
42+
else
3443
{
35-
int returned = n;
36-
if (returned <= 2)
37-
{
38-
// The smallest prime greater than or equal to 2
39-
// is … 2!
40-
returned = 2;
41-
}
42-
else
43-
{
44-
// Since 2 is the only even prime,
45-
// we make the returned value even
46-
// if it is divisible by 2.
47-
if (returned % 2 == 0)
48-
returned++;
44+
// Since 2 is the only even prime,
45+
// we make the returned value even
46+
// if it is divisible by 2.
47+
if (returned % 2 == 0)
48+
returned++;
4949

50-
// Then, we simply look for the next Prime value,
51-
// incrementing two by two.
52-
while (!IsPrime(returned))
53-
{
54-
returned += 2;
55-
}
56-
}
57-
return returned;
50+
// Then, we simply look for the next Prime value,
51+
// incrementing two by two.
52+
while (!IsPrime(returned))
53+
{
54+
returned += 2;
55+
}
5856
}
57+
return returned;
58+
}
5959
}

source/code/projects/Dictionary_prelim/Dictionary_prelim/Program.cs

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,79 @@
33

44
class Program
55
{
6-
public enum Level
6+
public enum Level
7+
{
8+
Low,
9+
Medium,
10+
High,
11+
}
12+
13+
static void Main(string[] args)
14+
{
15+
// Demonstrating enum type.
16+
Console.WriteLine("Demonstrating enumerated types:");
17+
Level lvl1 = Level.Medium; // To access the value, we prefix with Level.
18+
Level lvl2 = (Level)0; // We can cast an int into a Level.
19+
Console.WriteLine(lvl1);
20+
Console.WriteLine(lvl2.ToString());
21+
// Will display "Medium" then "Low": the ToString() method is given by default.
22+
23+
// Demonstrating PrimeHelper class:
24+
Console.WriteLine("\nDemonstrating PrimeHelper class:");
25+
// Testing IsPrime
26+
int[] testingValues =
727
{
8-
Low,
9-
Medium,
10-
High
28+
89,
29+
6700417,
30+
2147483646,
31+
2147483647,
32+
};
33+
// 2147483647 remained the largest known prime until 1867,
34+
// and is the largest value that a signed 32-bit integer field can hold!
35+
// cf. https://en.wikipedia.org/wiki/2,147,483,647
36+
foreach (int i in testingValues)
37+
{
38+
Console.WriteLine(
39+
$"{i:N00} is prime: {PrimeHelper.IsPrime(i)}."
40+
);
1141
}
12-
13-
static void Main(string[] args)
42+
// Testing NextPrime
43+
for (int i = 0; i < 10; i++)
1444
{
15-
// Demonstrating enum type.
16-
Console.WriteLine("Demonstrating enumerated types:");
17-
Level lvl1 = Level.Medium; // To access the value, we prefix with Level.
18-
Level lvl2 = (Level)0; // We can cast an int into a Level.
19-
Console.WriteLine(lvl1);
20-
Console.WriteLine(lvl2.ToString());
21-
// Will display "Medium" then "Low": the ToString() method is given by default.
22-
23-
// Demonstrating PrimeHelper class:
24-
Console.WriteLine("\nDemonstrating PrimeHelper class:");
25-
// Testing IsPrime
26-
int[] testingValues = { 89, 6700417, 2147483646, 2147483647 };
27-
// 2147483647 remained the largest known prime until 1867,
28-
// and is the largest value that a signed 32-bit integer field can hold!
29-
// cf. https://en.wikipedia.org/wiki/2,147,483,647
30-
foreach (int i in testingValues)
31-
{
32-
Console.WriteLine($"{i:N00} is prime: {PrimeHelper.IsPrime(i)}.");
33-
}
34-
// Testing NextPrime
35-
for (int i = 0; i < 10; i++)
36-
{
37-
Console.WriteLine(
38-
"The smallest prime greater than or equal to "
39-
+ i
40-
+ " is "
41-
+ PrimeHelper.NextPrime(i)
42-
+ "."
43-
);
44-
}
45-
Console.WriteLine("The smallest prime greater than or equal to 2,147,483,000 is: " + PrimeHelper.NextPrime(2147483000));
46-
47-
// Demonstrating GetHashCode:
48-
Console.WriteLine("\nDemonstrating GetHashCode method:");
49-
Console.WriteLine(
50-
"The hash code of an empty array of 12 int is: "
51-
+ (new int[12]).GetHashCode() // 156563611
52-
+ "."
53-
);
54-
Console.WriteLine(
55-
"The hash code of an empty array of 14 string is: "
56-
+ (new string[14]).GetHashCode() // -800837957
57-
+ "."
58-
);
59-
Console.WriteLine(
60-
"The hash code of \"test string\" is: "
61-
+ "test string".GetHashCode() // 1040790544
62-
+ "."
63-
);
64-
Console.WriteLine(
65-
"The hash code of 12 is: " + 12.GetHashCode() + "." // 12
66-
);
45+
Console.WriteLine(
46+
"The smallest prime greater than or equal to "
47+
+ i
48+
+ " is "
49+
+ PrimeHelper.NextPrime(i)
50+
+ "."
51+
);
6752
}
53+
Console.WriteLine(
54+
"The smallest prime greater than or equal to 2,147,483,000 is: "
55+
+ PrimeHelper.NextPrime(2147483000)
56+
);
57+
58+
// Demonstrating GetHashCode:
59+
Console.WriteLine(
60+
"\nDemonstrating GetHashCode method:"
61+
);
62+
Console.WriteLine(
63+
"The hash code of an empty array of 12 int is: "
64+
+ (new int[12]).GetHashCode() // 156563611
65+
+ "."
66+
);
67+
Console.WriteLine(
68+
"The hash code of an empty array of 14 string is: "
69+
+ (new string[14]).GetHashCode() // -800837957
70+
+ "."
71+
);
72+
Console.WriteLine(
73+
"The hash code of \"test string\" is: "
74+
+ "test string".GetHashCode() // 1040790544
75+
+ "."
76+
);
77+
Console.WriteLine(
78+
"The hash code of 12 is: " + 12.GetHashCode() + "." // 12
79+
);
80+
}
6881
}

0 commit comments

Comments
 (0)