Skip to content

Commit f539c29

Browse files
committed
added is-prime algorithm
1 parent 0be6569 commit f539c29

File tree

8 files changed

+129
-60
lines changed

8 files changed

+129
-60
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ exports.efficient = require('./modules/sorts/efficient.js');
44
exports.inefficient = require('./modules/sorts/inefficient.js');
55
exports.simple = require('./modules/sorts/simple.js');
66
exports.other = require('./modules/sorts/other.js');
7-
exports.Stack = require('./modules/data-types/stack.js');
7+
exports.Stack = require('./modules/data-types/stack.js');
8+
exports.isPrime = require('./modules/algorithms/is-prime.js');

modules/algorithms/is-prime.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var common = require('../common.js');
2+
3+
var isPrime = (n) => {
4+
//-n, 0, 1 not allowed
5+
if (n < 2) return false
6+
7+
// if even not prime
8+
if (n % 2 == 0) return false
9+
10+
//check for single digit primes
11+
if (common.inArray(n, [2, 3, 5, 7])) return true
12+
13+
//prime numbers end in 1, 3, 7 or 9 no matter how long they are
14+
//we know we are using only o0d numbers so check if it ends in 5. If so false.
15+
if (String(n).substr(-1) == 5) return false
16+
17+
18+
// check if it is divisible by the any of the numbers below it
19+
for (var i = 3; i <= Math.sqrt(n); i+=2)
20+
if (n % i == 0) return false
21+
22+
//num is prime - divisible by itself and 1 only
23+
return true
24+
}
25+
26+
27+
28+
/**
29+
* Export functions out to the calling code
30+
*/
31+
module.exports = isPrime;

modules/algorithms/readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Algorithms
2+
3+
Various algorithms developed for different purposes. Some are standard, some are not.
4+
5+
## Current list of Algorithms
6+
7+
* isPrime(number) ![status](https://img.shields.io/badge/status-completed-brightgreen.svg)
8+
* *This checks to see if the number passed to it is prime.*
9+

modules/common.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,12 @@ module.exports = {
3131
for(var i = 0; i < a.length; i++){
3232
a[i] = b[i];
3333
}
34+
},
35+
'inArray': function(needle, haystack){
36+
var length = haystack.length
37+
for(var i = 0; i < length; i++)
38+
if(haystack[i] == needle) return true
39+
40+
return false
3441
}
3542
};

modules/data-types/readme.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Data Types
2+
3+
These are different computer science data types implemented in javascript.
4+
5+
## Current List of Data Types
6+
Link for reference of data types: [https://en.wikipedia.org/wiki/List_of_data_structures](https://en.wikipedia.org/wiki/List_of_data_structures)
7+
8+
| Data Type | Status | Source Link |
9+
|-----------------------------|--------------------------------------------------------------------------|-------------|
10+
| array | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
11+
| dynamic array | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
12+
| stack | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) | [View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/data-types/stack.js) |
13+
| tree | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
14+
| balanced tree | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
15+
| container | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
16+
| dictionary | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
17+
| graph adjacency list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
18+
| graph adjacency matrix | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
19+
| graph incidence matrix | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
20+
| list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
21+
| linked list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
22+
| doubly linked list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
23+
| multiply linked list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
24+
| circular linked list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
25+
| map | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
26+
| multimap | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
27+
| set | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
28+
| multiset | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
29+
| queue | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
30+
| priority queue | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
31+
| double ended queue | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
32+
| double ended priority queue | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |

modules/sorts/readme.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Sorts
2+
3+
These are basic sorting algorithms implemented in javascript. They try to stick to generic programming structure and not javascript oriented style.
4+
5+
## Current Array Sorting Algorithms
6+
7+
| Sorting Algorithm | Function Call | Status | Source Link |
8+
|----------------------|-------------------------------|--------------------------------------------------------------------------|-------------|
9+
| Selection Sort | simple.selection(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/simple.js#L39)|
10+
| Insertion Sort | simple.insertion(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/simple.js#L16)|
11+
| Top Down Merge Sort | efficient.mergeTopDown(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/efficient.js#L3)|
12+
| Bottom Up Merge Sort | efficient.mergeBottomUp(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/efficient.js#L41)|
13+
| Heap Sort | efficient.heap(data) | ![status](https://img.shields.io/badge/status-in%20progress-yellow.svg) ||
14+
| Heap Sort Bottom Up | efficient.heapBottomUp(data) | ![status](https://img.shields.io/badge/status-not%20started-red.svg) ||
15+
| Quick Sort | efficient.quick(data) | ![status](https://img.shields.io/badge/status-not%20started-red.svg) ||
16+
| Bubble Sort | bubble.bubble(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/bubble.js#L4)|
17+
| Shell Sort | bubble.shell(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/bubble.js#L16)|
18+
| Comb Sort | bubble.comb(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/bubble.js#L30)|
19+
| Cocktail Sort | bubble.cocktail(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/bubble.js#L48)|
20+
| Counting Sort | distribution.counting(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/distribution.js#L6)|
21+
| Bucket Sort | distribution.bucket(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/distribution.js#L39)|
22+
| Radix Sort | distribution.radix(data) | ![status](https://img.shields.io/badge/status-not%20started-red.svg) ||
23+
| Bogo Sort | inefficient.bogo(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/inefficient.js#L4)|
24+
| Stooge Sort | inefficient.stooge(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/inefficient.js#L9)|
25+
| Codex Sort | other.codexSort(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/other.js#L18)|

readme.md

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,15 @@ about the sorting algorithms.
1717

1818
## Current Array Sorting Algorithms
1919

20-
| Sorting Algorithm | Function Call | Status | Source Link |
21-
|----------------------|-------------------------------|--------------------------------------------------------------------------|-------------|
22-
| Selection Sort | simple.selection(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/simple.js#L39)|
23-
| Insertion Sort | simple.insertion(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/simple.js#L16)|
24-
| Top Down Merge Sort | efficient.mergeTopDown(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/efficient.js#L3)|
25-
| Bottom Up Merge Sort | efficient.mergeBottomUp(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/efficient.js#L41)|
26-
| Heap Sort | efficient.heap(data) | ![status](https://img.shields.io/badge/status-in%20progress-yellow.svg) ||
27-
| Heap Sort Bottom Up | efficient.heapBottomUp(data) | ![status](https://img.shields.io/badge/status-not%20started-red.svg) ||
28-
| Quick Sort | efficient.quick(data) | ![status](https://img.shields.io/badge/status-not%20started-red.svg) ||
29-
| Bubble Sort | bubble.bubble(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/bubble.js#L4)|
30-
| Shell Sort | bubble.shell(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/bubble.js#L16)|
31-
| Comb Sort | bubble.comb(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/bubble.js#L30)|
32-
| Cocktail Sort | bubble.cocktail(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/bubble.js#L48)|
33-
| Counting Sort | distribution.counting(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/distribution.js#L6)|
34-
| Bucket Sort | distribution.bucket(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/distribution.js#L39)|
35-
| Radix Sort | distribution.radix(data) | ![status](https://img.shields.io/badge/status-not%20started-red.svg) ||
36-
| Bogo Sort | inefficient.bogo(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/inefficient.js#L4)|
37-
| Stooge Sort | inefficient.stooge(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/inefficient.js#L9)|
38-
| Codex Sort | other.codexSort(data) | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) |[View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/sorts/other.js#L18)|
20+
[View Sorts Folder](https://github.com/shadowcodex/data-theory/tree/master/modules/sorts)
3921

4022
## Current List of Data Types
4123

42-
Link for reference of data types: [https://en.wikipedia.org/wiki/List_of_data_structures](https://en.wikipedia.org/wiki/List_of_data_structures)
24+
[View Data Types Folder](https://github.com/shadowcodex/data-theory/tree/master/modules/data-types)
4325

44-
| Data Type | Status | Source Link |
45-
|-----------------------------|--------------------------------------------------------------------------|-------------|
46-
| array | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
47-
| dynamic array | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
48-
| stack | ![status](https://img.shields.io/badge/status-completed-brightgreen.svg) | [View Source](https://github.com/shadowcodex/data-theory/blob/master/modules/data-types/stack.js) |
49-
| tree | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
50-
| balanced tree | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
51-
| container | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
52-
| dictionary | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
53-
| graph adjacency list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
54-
| graph adjacency matrix | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
55-
| graph incidence matrix | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
56-
| list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
57-
| linked list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
58-
| doubly linked list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
59-
| multiply linked list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
60-
| circular linked list | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
61-
| map | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
62-
| multimap | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
63-
| set | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
64-
| multiset | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
65-
| queue | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
66-
| priority queue | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
67-
| double ended queue | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
68-
| double ended priority queue | ![status](https://img.shields.io/badge/status-not%20started-red.svg) | |
26+
## Current List of Algorithms
27+
28+
[View Algorithms Folder](https://github.com/shadowcodex/data-theory/tree/master/modules/algorithms)
6929

7030
### Note for contributors
7131

test/simpletest.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
var dt = require('../index.js');
22

3-
// var x = [1,5,7,2,43,6,26,7,2356,7,346,1,42,0];
3+
var x = [1,5,7,2,43,6,26,7,2356,7,346,1,42,0];
44
// var y = ['a', 'b','d', 'c','h','z','a','y','w','m'];
55
// var z = ['apples', 'oranges', 'banannas', 'pears', 'grapes', 'carrots', 'squash', 'lettuce'];
66

77

8-
var theStack = new dt.Stack();
8+
// var theStack = new dt.Stack();
99

10-
theStack.push(1);
11-
console.log(theStack.values);
12-
theStack.push(2);
13-
console.log(theStack.values);
14-
theStack.pop();
15-
console.log(theStack.values);
16-
theStack.push(3);
17-
console.log(theStack.values);
10+
// theStack.push(1);
11+
// console.log(theStack.values);
12+
// theStack.push(2);
13+
// console.log(theStack.values);
14+
// theStack.pop();
15+
// console.log(theStack.values);
16+
// theStack.push(3);
17+
// console.log(theStack.values);
1818

19-
console.log(theStack.pop());
20-
console.log(theStack.pop());
21-
console.log(theStack.pop());
22-
console.log(theStack.pop());
19+
// console.log(theStack.pop());
20+
// console.log(theStack.pop());
21+
// console.log(theStack.pop());
22+
// console.log(theStack.pop());
23+
24+
for(var i=0; i < x.length; i++){
25+
console.log(dt.isPrime(x[i]), x[i]);
26+
}

0 commit comments

Comments
 (0)