Merge pull request #1 from William103/Some-cleaning-in-Util.java

Cleaning in Util.java and Main.java
This commit is contained in:
William103 2020-07-19 17:28:55 -07:00 committed by GitHub
commit e3b6d583b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 17 deletions

View file

@ -1,3 +1,5 @@
package append_primes;
import java.util.ArrayList; import java.util.ArrayList;
public class Main public class Main
@ -6,8 +8,11 @@ public class Main
{ {
Util.init(); Util.init();
Tree tree = new Tree(0, new ArrayList<Long>()); /* By changing the args[0] we can modify which prime we want to be the
for (int i = 0; i < 9; i++) { * first in the row. Same for the number of iteration.
*/
Tree tree = new Tree(Integer.parseInt(args[0]), new ArrayList<Long>());
for (int i = 0; i < Integer.parseInt(args[1]); i++) {
tree.step(); tree.step();
Util.printList(tree.longestPath()); Util.printList(tree.longestPath());
} }

View file

@ -1,3 +1,5 @@
package append_primes;
import java.util.ArrayList; import java.util.ArrayList;
class Tree class Tree

View file

@ -1,4 +1,8 @@
package append_primes;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.stream.IntStream;
import java.util.ArrayList; import java.util.ArrayList;
class Util class Util
@ -14,6 +18,8 @@ class Util
public static boolean isPrime(long val) public static boolean isPrime(long val)
{ {
// Add 2 to the HashMap in advance in order to quickly exclude even number later
primes.put(2l, true);
if (primes.containsKey(val)) { if (primes.containsKey(val)) {
return primes.get(val); return primes.get(val);
} }
@ -21,14 +27,16 @@ class Util
primes.put(val, false); primes.put(val, false);
return false; return false;
} }
for (int i = 2; i * i <= val; i++) { /* I tried to make this process more readable but I'm not sure about the efficiency
if (val % i == 0) { * TODO check the efficiency of this new method
primes.put(val, false); */
return false; boolean temp = (val % 2) != 0
} &&
} IntStream.rangeClosed(3, (int) Math.sqrt(val))
primes.put(val, true); .filter(n -> n % 2 != 0)
return true; .noneMatch(n -> (val % n == 0));
primes.put(val, temp);
return temp;
} }
public static boolean isSquareFree(long val) public static boolean isSquareFree(long val)
@ -48,14 +56,14 @@ class Util
return true; return true;
} }
// Did some cleaning to this util method
public static void printList(ArrayList<Long> ls) public static void printList(ArrayList<Long> ls)
{ {
System.out.print("["); Iterator<Long> it = ls.iterator();
for (int i = 0; i < ls.size() - 1; i++) { System.out.printf("[%s",it.next());
System.out.print(ls.get(i)); do{
System.out.print(", "); System.out.printf(", %s", it.next());
} }while(it.hasNext());
System.out.print(ls.get(ls.size() - 1));
System.out.print("]\n"); System.out.print("]\n");
} }
} }

View file

@ -1,3 +1,5 @@
package template;
import java.util.ArrayList; import java.util.ArrayList;
/* Here is where you can tweak the starting conditions */ /* Here is where you can tweak the starting conditions */
@ -10,7 +12,7 @@ public class Main
/* here we create a new list with value 0 and no children and check 20 /* here we create a new list with value 0 and no children and check 20
* iterations, feel free to tweak this to your heart's content * iterations, feel free to tweak this to your heart's content
*/ */
Tree tree = new Tree(0, new ArrayList<Long>()); Tree tree = new Tree(2, new ArrayList<Long>());
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
tree.step(); tree.step();
Util.printList(tree.longestPath()); Util.printList(tree.longestPath());

View file

@ -1,3 +1,5 @@
package template;
import java.util.ArrayList; import java.util.ArrayList;
/* Basic tree stuff. I wouldn't recommend tweaking this unless you have some /* Basic tree stuff. I wouldn't recommend tweaking this unless you have some

View file

@ -1,3 +1,5 @@
package template;
import java.util.HashMap; import java.util.HashMap;
import java.util.ArrayList; import java.util.ArrayList;