Merge pull request #2 from William103/Modification
Add insert_digit_anywhere_prime for java
This commit is contained in:
commit
6bf8205823
3 changed files with 173 additions and 0 deletions
20
java/insert_digit_anywhere_prime/Main.java
Normal file
20
java/insert_digit_anywhere_prime/Main.java
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
package insert_digit_anywhere_prime;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Main
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
Util.init();
|
||||||
|
|
||||||
|
/* By changing the args[0] we can modify which prime we want to be the
|
||||||
|
* 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();
|
||||||
|
Util.printList(tree.longestPath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
84
java/insert_digit_anywhere_prime/Tree.java
Normal file
84
java/insert_digit_anywhere_prime/Tree.java
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
package insert_digit_anywhere_prime;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
class Tree
|
||||||
|
{
|
||||||
|
private long value;
|
||||||
|
private ArrayList<Tree> children;
|
||||||
|
|
||||||
|
public Tree(long tree_value, ArrayList<Long> tree_children)
|
||||||
|
{
|
||||||
|
value = tree_value;
|
||||||
|
children = new ArrayList<Tree>();
|
||||||
|
for (long child : tree_children) {
|
||||||
|
children.add(new Tree(child, new ArrayList<Long>()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void step()
|
||||||
|
{
|
||||||
|
if (children.isEmpty()) {
|
||||||
|
ArrayList<Long> xs = next(value);
|
||||||
|
for (long val : xs) {
|
||||||
|
children.add(new Tree(val, new ArrayList<Long>()));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (Tree child : children) {
|
||||||
|
child.step();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Long> longestPath()
|
||||||
|
{
|
||||||
|
ArrayList<Long> retval = new ArrayList<Long>();
|
||||||
|
if (children.isEmpty()) {
|
||||||
|
retval.add(value);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
int max_length = 0;
|
||||||
|
for (Tree child : children) {
|
||||||
|
ArrayList<Long> temp = child.longestPath();
|
||||||
|
if (temp.size() > max_length) {
|
||||||
|
max_length = temp.size();
|
||||||
|
retval = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
retval.add(0, value);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* modify this function to change functionality */
|
||||||
|
public ArrayList<Long> next(long val)
|
||||||
|
{
|
||||||
|
/* Super messy but does work
|
||||||
|
* TODO cleaning/comment
|
||||||
|
*/
|
||||||
|
ArrayList<Long> new_xs = new ArrayList<Long>();
|
||||||
|
String[] s = Long.toString(val).split("");
|
||||||
|
for(int i = 0; i <= s.length; i++){
|
||||||
|
String[] temp = new String[s.length + 1];
|
||||||
|
int j;
|
||||||
|
for(j = 0; j < i; j++){
|
||||||
|
temp[j] = s[j];
|
||||||
|
}
|
||||||
|
for(int k = j + 1; k < temp.length; k++){
|
||||||
|
temp[k] = s[k - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int num = 0; num <= 9; num++){
|
||||||
|
temp[j] = String.valueOf(num);
|
||||||
|
String str = Arrays.toString(temp)
|
||||||
|
.replaceAll("[^\\d.]", "")
|
||||||
|
.replaceAll("^0+(?!$)", "");
|
||||||
|
Long test = Long.parseLong(str);
|
||||||
|
if(Util.isPrime(test) && test != val){
|
||||||
|
new_xs.add(test);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new_xs;
|
||||||
|
}
|
||||||
|
}
|
||||||
69
java/insert_digit_anywhere_prime/Util.java
Normal file
69
java/insert_digit_anywhere_prime/Util.java
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
package insert_digit_anywhere_prime;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
class Util
|
||||||
|
{
|
||||||
|
private static HashMap<Long, Boolean> primes;
|
||||||
|
private static HashMap<Long, Boolean> squareFree;
|
||||||
|
|
||||||
|
public static void init()
|
||||||
|
{
|
||||||
|
primes = new HashMap<Long, Boolean>();
|
||||||
|
squareFree = new HashMap<Long, Boolean>();
|
||||||
|
}
|
||||||
|
|
||||||
|
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)) {
|
||||||
|
return primes.get(val);
|
||||||
|
}
|
||||||
|
if (val < 2) {
|
||||||
|
primes.put(val, false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/* I tried to make this process more readable but I'm not sure about the efficiency
|
||||||
|
* TODO check the efficiency of this new method
|
||||||
|
*/
|
||||||
|
boolean temp = (val % 2) != 0
|
||||||
|
&&
|
||||||
|
IntStream.rangeClosed(3, (int) Math.sqrt(val))
|
||||||
|
.filter(n -> n % 2 != 0)
|
||||||
|
.noneMatch(n -> (val % n == 0));
|
||||||
|
primes.put(val, temp);
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isSquareFree(long val)
|
||||||
|
{
|
||||||
|
if (squareFree.containsKey(val)) {
|
||||||
|
return squareFree.get(val);
|
||||||
|
}
|
||||||
|
for (int i = 2; i * i <= val; i++) {
|
||||||
|
if (isPrime(i)) {
|
||||||
|
if (val % (i * i) == 0) {
|
||||||
|
squareFree.put(val, false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
squareFree.put(val, true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Did some cleaning to this util method
|
||||||
|
public static void printList(ArrayList<Long> ls)
|
||||||
|
{
|
||||||
|
Iterator<Long> it = ls.iterator();
|
||||||
|
System.out.printf("[%s",it.next());
|
||||||
|
do{
|
||||||
|
System.out.printf(", %s", it.next());
|
||||||
|
}while(it.hasNext());
|
||||||
|
System.out.print("]\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue