Skip to content

Commit 7b662f5

Browse files
authored
Merge branch 'master' into b-tree
2 parents 8044590 + 32e8336 commit 7b662f5

18 files changed

+757
-277
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
name: Algorithm request
3+
about: Suggest a new algorithm for this project
4+
title: 'Algorithm request: [NAME]'
5+
labels: algorithm request
6+
assignees: ''
7+
8+
---
9+
10+
**Is your algorithm request related to a computational problem? Please describe.**
11+
A clear and concise description of what the problem is. For example, I am working on the Traveling Sales Man problem, LeetCode problem #123456 and I'd like to ...
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen. For example:
15+
16+
> I'd like to have an algorithm with following interface: [...], where I can define a graph-like map with an visitor iterator function and then ...
17+
18+
Or, more concretely:
19+
20+
> I'd like to have an implementation of the A* Search Algorithm, based on the following research paper: [Near Optimal Hierarchical Path-Finding - Botea, Mueller & Schaeffer](https://webdocs.cs.ualberta.ca/~mmueller/ps/hpastar.pdf).
21+
22+
**Describe alternatives you've considered**
23+
A clear and concise description of any alternative solutions or features you've considered.
24+
25+
**Additional context**
26+
Add any other context or screenshots about the feature request here.

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Desktop (please complete the following information):**
27+
- OS: [e.g. iOS]
28+
- Browser [e.g. chrome, safari]
29+
- Version [e.g. 22]
30+
31+
**Smartphone (please complete the following information):**
32+
- Device: [e.g. iPhone6]
33+
- OS: [e.g. iOS8.1]
34+
- Browser [e.g. stock browser, safari]
35+
- Version [e.g. 22]
36+
37+
**Additional context**
38+
Add any other context about the problem here.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
name: Data structure request
3+
about: Suggest a new data strcuture for this project
4+
title: 'Data structure request: [NAME]'
5+
labels: data structure request
6+
assignees: ''
7+
8+
---
9+
10+
**Is your data structure request related to a computational problem? Please describe.**
11+
A clear and concise description of what the problem is. For example, I am working on a new dictionary implementation and I'd like the internal functionality to be based on a BTree implementation.
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen. For example:
15+
16+
> I'd like to have a generic BTree implementation where I can specify the types of the keys and values, so that I can define a higher-level interface for a dictionary based on that.
17+
18+
Or, more concretely:
19+
20+
> I'd like to have an implementation of the BTree Data Structure, based on the following research paper: [Organization and Maintenance of Large Ordered Indexes - Bayer & McCreight](https://infolab.usc.edu/csci585/Spring2010/den_ar/indexing.pdf).
21+
22+
**Describe alternatives you've considered**
23+
A clear and concise description of any alternative solutions or features you've considered.
24+
25+
**Additional context**
26+
Add any other context or screenshots about the feature request here.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: feature
6+
assignees: ''
7+
8+
---
9+
10+
# THIS IS NOT A REQUEST FOR ALGORITHMS OR DATA STRUCTURES - FOR THAT USE THE APPROPRIATE TEMPLATE. THIS TEMPLATE IS FOR PROJECT-LEVEL FEATURES, E.G.: PACKAGING, CI/CD, NUGET ... ETC
11+
12+
**Is your feature request related to a problem? Please describe.**
13+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
14+
15+
**Describe the solution you'd like**
16+
A clear and concise description of what you want to happen.
17+
18+
**Describe alternatives you've considered**
19+
A clear and concise description of any alternative solutions or features you've considered.
20+
21+
**Additional context**
22+
Add any other context or screenshots about the feature request here.
Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,73 @@
1-
/***
2-
* Euclidean Algorithm to find the greatest common divisor of two numbers.
3-
*
4-
*/
5-
1+
using System;
62

73
namespace Algorithms.Numeric
84
{
95
public static class GreatestCommonDivisor
106
{
117
/// <summary>
12-
/// Finds and returns the greatest common divisor of two numbers
8+
/// Returns the greatest common divisor of two numbers using Euclidean Algorithm.
139
/// </summary>
14-
public static uint FindGCD(uint a, uint b)
10+
public static int FindGCDEuclidean(int a, int b)
1511
{
12+
a = Math.Abs(a);
13+
b = Math.Abs(b);
14+
1615
if (a == 0)
1716
return b;
1817
if (b == 0)
1918
return a;
19+
if (a == b)
20+
return a;
2021

21-
uint _a = a, _b = b;
22-
23-
//Bitwise operator '&' works on individual bits of each value
24-
//result is 0 or 1
25-
//it works like a modulus operator '%' but is more efficient
26-
uint r = _a & _b;
22+
return Euclidean(a, b);
23+
}
2724

28-
while(r != 0)
29-
{
30-
_a = _b;
31-
_b = r;
32-
r = _a & _b;
33-
}
25+
private static int Euclidean(int a, int b)
26+
{
27+
if (b == 0)
28+
return a;
3429

35-
return _b;
30+
return Euclidean(b, a % b);
3631
}
3732

3833
/// <summary>
39-
/// Determines given two numbers are relatively prime
34+
/// Returns the greatest common divisor of two numbers using Stein Algorithm.
4035
/// </summary>
41-
public static bool IsRelativelyPrime(uint a, uint b)
36+
public static int FindGCDStein(int a, int b)
37+
{
38+
a = Math.Abs(a);
39+
b = Math.Abs(b);
40+
41+
return Stein(a, b);
42+
}
43+
44+
private static int Stein(int a, int b)
4245
{
43-
return FindGCD(a, b) == 1;
46+
if (a == 0)
47+
return b;
48+
if (b == 0)
49+
return a;
50+
if (a == b)
51+
return a;
52+
53+
if ((~a & 1) == 1)
54+
{
55+
if ((b & 1) == 1)
56+
{
57+
return Stein(a >> 1, b);
58+
}
59+
else
60+
{
61+
return Stein(a >> 1, b >> 1) << 1;
62+
}
63+
}
64+
65+
if ((~b & 1) == 1)
66+
{
67+
return Stein(a, b >> 1);
68+
}
69+
70+
return a > b ? Stein((a - b) >> 1, b) : Stein(a, (b - a) >> 1);
4471
}
4572
}
4673
}

Algorithms/Numeric/SieveOfAtkin.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Collections.Generic;
2+
using System.Reflection.Metadata.Ecma335;
3+
using System.Threading.Tasks;
4+
5+
/***
6+
* Generates all prime numbers up to a given number
7+
* Wikipedia: https://en.wikipedia.org/wiki/Sieve_of_Atkin
8+
*/
9+
10+
11+
namespace Algorithms.Numeric
12+
{
13+
public static class SieveOfAtkin
14+
{
15+
/// <summary>
16+
/// Calculate primes up to a given number
17+
/// </summary>
18+
public static List<int> GeneratePrimesUpTo(int max)
19+
{
20+
if (max == 2)
21+
{
22+
return new List<int>() { 2 };
23+
}
24+
25+
if (max < 2)
26+
{
27+
return new List<int>();
28+
}
29+
30+
var isPrime = new bool[max + 1];
31+
var sqrt = (int)System.Math.Sqrt(max);
32+
33+
Parallel.For(1, sqrt, x =>
34+
{
35+
var xx = x * x;
36+
for (var y = 1; y <= sqrt; y++)
37+
{
38+
var yy = y * y;
39+
var n = 4 * xx + yy;
40+
if (n <= max && (n % 12 == 1 || n % 12 == 5))
41+
isPrime[n] ^= true;
42+
43+
n = 3 * xx + yy;
44+
if (n <= max && n % 12 == 7)
45+
isPrime[n] ^= true;
46+
47+
n = 3 * xx - yy;
48+
if (x > y && n <= max && n % 12 == 11)
49+
isPrime[n] ^= true;
50+
}
51+
});
52+
53+
var primes = new List<int>() { 2, 3 };
54+
for (var n = 5; n <= sqrt; n++)
55+
{
56+
if (isPrime[n])
57+
{
58+
primes.Add(n);
59+
var nn = n * n;
60+
for (var k = nn; k <= max; k += nn)
61+
isPrime[k] = false;
62+
}
63+
}
64+
65+
for (var n = sqrt + 1; n <= max; n++)
66+
if (isPrime[n])
67+
primes.Add(n);
68+
69+
return primes;
70+
}
71+
72+
}
73+
}

Algorithms/Numeric/SieveOfEratosthenes.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
/***
56
* Generates all prime numbers up to a given number
@@ -14,43 +15,43 @@ public static class SieveOfEratosthenes
1415
/// <summary>
1516
/// Calculate primes up to a given number
1617
/// </summary>
17-
public static List<int> GeneratePrimesUpTo(int x)
18+
public static IEnumerable<int> GeneratePrimesUpTo(int x)
1819
{
1920

20-
//The list of primes that will be returned
21-
List<int> primesList = new List<int>();
21+
//The hash of primes that will be returned
22+
var primes = new HashSet<int>();
2223

2324
//Returns an empty list if x is a value under 2
2425
if (x < 2)
2526
{
26-
return primesList;
27+
return primes.ToList();
2728
}
2829

29-
//Adds every number between 2 and x to the list
30+
//Adds every number between 2 and x to the hashset
3031
for (int i = 2; i <= x; i++)
3132
{
32-
primesList.Add(i);
33+
primes.Add(i);
3334
}
3435

35-
//integer that all multiples of will be removed from the list
36+
//integer that all multiples of will be removed from the hashset
3637
int removeMultiplesOf;
3738

38-
//Finds the next number in the list that hasn't been removed and removes all multiples of that number
39-
//from the list
39+
//Finds the next number that hasn't been removed and removes all multiples of that number
40+
//from the hashset
4041
for (int i = 2; i <= Math.Sqrt(x); i++)
4142
{
42-
if (primesList.Contains(i))
43+
if (primes.Contains(i))
4344
{
4445
removeMultiplesOf = i;
45-
for (int j = removeMultiplesOf*removeMultiplesOf; j <= x; j += removeMultiplesOf)
46+
for (int j = removeMultiplesOf * removeMultiplesOf; j <= x; j += removeMultiplesOf)
4647
{
47-
primesList.Remove(j);
48+
primes.Remove(j);
4849
}
4950
}
5051
}
5152

5253
//The list of primes is returned
53-
return primesList;
54+
return primes.ToList();
5455
}
5556

5657
}

0 commit comments

Comments
 (0)