Skip to content

Commit 8b8542e

Browse files
authored
Revised and added a bunch of files.
1 parent d5627dd commit 8b8542e

9 files changed

+619
-260
lines changed

Factorial.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Factorial {
2+
public static void main(String[] args) {
3+
Factorial factorial = new Factorial();
4+
System.out.println(factorial.getRecursiveFactorial(6));
5+
System.out.println(factorial.getIterativeFactorial(6));
6+
}
7+
8+
public int getRecursiveFactorial(int n) {
9+
if (n < 0) return -1;
10+
else if (n < 2) return 1;
11+
else return (n * getRecursiveFactorial(n-1));
12+
}
13+
14+
public int getIterativeFactorial(int n) {
15+
if (n < 0) return -1;
16+
int fact = 1;
17+
for (int i = 1; i <= n; i++)
18+
fact *= i;
19+
return fact;
20+
}
21+
}

LICENSE

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
MIT License
2-
3-
Copyright (c) 2017 Joe James
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
MIT License
2+
3+
Copyright (c) 2017 Joe James
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LinkedList.java

+103-103
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,104 @@
1-
// Linked List in Java. Written by Joe James.
2-
public class LinkedList {
3-
Node root;
4-
int size;
5-
6-
public LinkedList() {
7-
root = new Node();
8-
size = 0;
9-
}
10-
11-
// Test code - main function
12-
public static void main(String[] args) {
13-
LinkedList ll = new LinkedList();
14-
System.out.println(ll.getSize());
15-
ll.add(8);
16-
System.out.println(ll.getSize());
17-
ll.add(17);
18-
ll.add(5);
19-
ll.add(10);
20-
System.out.println(ll.find(17).getData());
21-
ll.remove(5);
22-
System.out.println(ll.getSize());
23-
System.out.println(ll.find(5));
24-
}
25-
26-
public void setSize(int s) {
27-
this.size = s;
28-
}
29-
30-
public int getSize() {
31-
return this.size;
32-
}
33-
34-
public Node add(int data) {
35-
Node newNode = new Node(data, root);
36-
this.root = newNode;
37-
this.size++;
38-
return newNode;
39-
}
40-
41-
public Node find(int data) {
42-
Node thisNode = this.root;
43-
44-
while (thisNode != null) {
45-
if (thisNode.getData() == data)
46-
return thisNode;
47-
thisNode = thisNode.getNextNode();
48-
}
49-
return null;
50-
}
51-
52-
public boolean remove(int data) {
53-
Node thisNode = this.root;
54-
Node prevNode = null;
55-
56-
while (thisNode != null) {
57-
if (thisNode.getData() == data) {
58-
if (prevNode != null)
59-
prevNode.setNextNode(thisNode.getNextNode());
60-
else
61-
this.root = null;
62-
this.setSize(this.getSize()-1);
63-
return true;
64-
}
65-
prevNode = thisNode;
66-
thisNode = thisNode.getNextNode();
67-
}
68-
return false;
69-
}
70-
71-
// Node class
72-
private class Node {
73-
private Node nextNode;
74-
private int data;
75-
76-
// 0-arg constructor, 1-arg constructor, 2-arg constructor
77-
private Node() { }
78-
79-
private Node(int val) {
80-
data = val;
81-
}
82-
83-
private Node(int val, Node next) {
84-
data = val;
85-
nextNode = next;
86-
}
87-
88-
private void setData(int val) {
89-
this.data = val;
90-
}
91-
92-
private int getData() {
93-
return this.data;
94-
}
95-
96-
private void setNextNode(Node n) {
97-
this.nextNode = n;
98-
}
99-
100-
private Node getNextNode() {
101-
return this.nextNode;
102-
}
103-
}
1+
// Linked List in Java. Written by Joe James.
2+
public class LinkedList {
3+
Node root;
4+
int size;
5+
6+
public LinkedList() {
7+
root = new Node();
8+
size = 0;
9+
}
10+
11+
// Test code - main function
12+
public static void main(String[] args) {
13+
LinkedList ll = new LinkedList();
14+
System.out.println(ll.getSize());
15+
ll.add(8);
16+
System.out.println(ll.getSize());
17+
ll.add(17);
18+
ll.add(5);
19+
ll.add(10);
20+
System.out.println(ll.find(17).getData());
21+
ll.remove(5);
22+
System.out.println(ll.getSize());
23+
System.out.println(ll.find(5));
24+
}
25+
26+
public void setSize(int s) {
27+
this.size = s;
28+
}
29+
30+
public int getSize() {
31+
return this.size;
32+
}
33+
34+
public Node add(int data) {
35+
Node newNode = new Node(data, root);
36+
this.root = newNode;
37+
this.size++;
38+
return newNode;
39+
}
40+
41+
public Node find(int data) {
42+
Node thisNode = this.root;
43+
44+
while (thisNode != null) {
45+
if (thisNode.getData() == data)
46+
return thisNode;
47+
thisNode = thisNode.getNextNode();
48+
}
49+
return null;
50+
}
51+
52+
public boolean remove(int data) {
53+
Node thisNode = this.root;
54+
Node prevNode = null;
55+
56+
while (thisNode != null) {
57+
if (thisNode.getData() == data) {
58+
if (prevNode != null)
59+
prevNode.setNextNode(thisNode.getNextNode());
60+
else
61+
this.root = null;
62+
this.setSize(this.getSize()-1);
63+
return true;
64+
}
65+
prevNode = thisNode;
66+
thisNode = thisNode.getNextNode();
67+
}
68+
return false;
69+
}
70+
71+
// Node class
72+
private class Node {
73+
private Node nextNode;
74+
private int data;
75+
76+
// 0-arg constructor, 1-arg constructor, 2-arg constructor
77+
private Node() { }
78+
79+
private Node(int val) {
80+
data = val;
81+
}
82+
83+
private Node(int val, Node next) {
84+
data = val;
85+
nextNode = next;
86+
}
87+
88+
private void setData(int val) {
89+
this.data = val;
90+
}
91+
92+
private int getData() {
93+
return this.data;
94+
}
95+
96+
private void setNextNode(Node n) {
97+
this.nextNode = n;
98+
}
99+
100+
private Node getNextNode() {
101+
return this.nextNode;
102+
}
103+
}
104104
}

Primes.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// version 1
2+
public class Primes {
3+
4+
public static void main(String[] args) {
5+
int max = 10;
6+
for (int x = 2; x <= max; x++) {
7+
boolean isPrime = true;
8+
for (int y = 2; y < x; y++)
9+
if (x % y == 0)
10+
isPrime = false;
11+
if (isPrime)
12+
System.out.println(x);
13+
}
14+
}
15+
}
16+
//--------------------------------------------
17+
// version 2: break
18+
public class Primes {
19+
20+
public static void main(String[] args) {
21+
int max = 10;
22+
for (int x = 2; x <= max; x++) {
23+
boolean isPrime = true;
24+
for (int y = 2; y < x; y++)
25+
if (x % y == 0) {
26+
isPrime = false;
27+
break;
28+
}
29+
if (isPrime)
30+
System.out.println(x);
31+
}
32+
}
33+
}
34+
//--------------------------------------------
35+
// version 3: break, add to list
36+
import java.util.ArrayList;
37+
38+
public class Primes {
39+
40+
public static void main(String[] args) {
41+
ArrayList<Integer> primeList = new ArrayList<>();
42+
43+
int max = 10000;
44+
for (int x = 2; x <= max; x++) {
45+
boolean isPrime = true;
46+
for (int y = 2; y < x; y++)
47+
if (x % y == 0) {
48+
isPrime = false;
49+
break;
50+
}
51+
if (isPrime)
52+
primeList.add(x);
53+
}
54+
System.out.println(primeList);
55+
}
56+
}
57+
//--------------------------------------------
58+
// version 4: break, add to list, square root
59+
import java.util.ArrayList;
60+
61+
public class Primes {
62+
63+
public static void main(String[] args) {
64+
ArrayList<Integer> primeList = new ArrayList<>();
65+
66+
int max = 200000;
67+
for (int x = 2; x <= max; x++) {
68+
boolean isPrime = true;
69+
for (int y = 2; y < Math.sqrt(x); y++)
70+
if (x % y == 0) {
71+
isPrime = false;
72+
break;
73+
}
74+
if (isPrime)
75+
primeList.add(x);
76+
}
77+
System.out.println(primeList);
78+
}
79+
}

0 commit comments

Comments
 (0)