Last active
August 29, 2015 14:16
-
-
Save soltrinox/619b745480f40ea1fc38 to your computer and use it in GitHub Desktop.
Prime number discovery from sequence transposed to a grid.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
a program that prints out a list of 5 digit prime-numbers that can be found in a given table. Let's called this program FindPrime. | |
It takes in string of numbers as the table. For simplicity, let's assume that the table size is always 5x5. This is an example of how you run FindPrime: | |
FindPrime 5203830594313459791285023 | |
In this case, the table is: | |
5 2 0 3 8 | |
3 0 5 9 4 | |
3 1 3 4 5 | |
9 7 9 1 2 | |
8 5 0 2 3 | |
FindPrime will find ALL possible 5 digit prime numbers that can be constructed from the numbers of sequentially adjacent numbers, where "adjacent" numbers are those horizontally, vertically, and diagonally neighboring. | |
One of the prime number from the above table is 17921 and here's how FindPrime found it: | |
- 1 is from col 2, row 3 | |
- 7 is vertically adjacent to 1 (down) | |
- 9 is horizontally adjacent to 7 (right) | |
- 2 is diagonally adjacent to 9 (down right) | |
- 1 is vertically adjacent to 2 (up) | |
*/ | |
import java.util.Random; | |
import java.util.List; | |
import java.util.ArrayList; | |
public class FindPrime { | |
public static void main(String args[]) { | |
String randStringOfNumbers = ""; | |
for(int x =0; x < 25; x++){ | |
int o = randInt(1, 9); | |
//System.out.printf("%d:%d%n", x, o); | |
String str = Integer.toString(o); | |
randStringOfNumbers += str; | |
} | |
System.out.printf("%n%n%n@@@@@@@@@@@@@@@@@@%n"); | |
System.out.printf("RAND NUMBER LIST: "); | |
System.out.printf("%s%n=================%n", randStringOfNumbers); | |
List<String> horiz = buildHoriz(randStringOfNumbers); | |
System.out.printf("DISPLAY IN A NUMBER GRID:%n"); | |
System.out.printf("=================%n%n"); | |
// print grid | |
for(int p =0; p < 5; p++){ | |
System.out.printf("%s%n", horiz.get(p)); | |
} | |
System.out.printf("%n=================%n"); | |
// HORIZONTAL SEARCH | |
for(int y = 0; y < 5; y++){ | |
String line = horiz.get(y); | |
int number = (int)Integer.parseInt(line); | |
if(isPrimeOrNot(number)){ | |
System.out.printf("%d ltr row is a prime %n", number ); | |
} | |
line = reverseString(line); | |
number = (int)Integer.parseInt(line); | |
if(isPrimeOrNot(number)){ | |
System.out.printf("%d ltr row is a prime %n", number ); | |
} | |
} | |
// VERTICAL SEARCH | |
List<String> vert = buildVert(horiz); | |
for(int z =0; z < 5; z++){ | |
String line = vert.get(z); | |
int number = (int)Integer.parseInt(vert.get(z)); | |
if(isPrimeOrNot(number)){ | |
System.out.printf("%d ttb col is a prime %n", number ); | |
} | |
line = reverseString(line); | |
number = (int)Integer.parseInt(line); | |
if(isPrimeOrNot(number)){ | |
System.out.printf("%d btt col is a prime %n", number ); | |
} | |
} | |
// DIAGONAL (in a 5x5 grid there are only two 5 digit diagonal values ) | |
List<String> diag = buildDiag(horiz); | |
for(int d =0; d < 2; d++){ | |
String line = diag.get(d); | |
int number = (int)Integer.parseInt(diag.get(d)); | |
if(isPrimeOrNot(number)){ | |
System.out.printf("%d ttb dgn is a prime %n", number ); | |
} | |
line = reverseString(line); | |
number = (int)Integer.parseInt(line); | |
if(isPrimeOrNot(number)){ | |
System.out.printf("%d btt dgn is a prime %n", number ); | |
} | |
} | |
} | |
public static List<String> buildHoriz(String guess) { | |
String[] cArray = guess.split("(?!^)"); | |
List<String> newl = new ArrayList<String>(); | |
String row0 = ""; String row1 = ""; String row2 = ""; String row3 = ""; String row4 = ""; | |
for (int place = 0; place < cArray.length; place++ ) { | |
String ch = cArray[place]; | |
int i = Integer.parseInt(ch); | |
//System.out.printf("%d : %d %n", place, i); | |
if((place <= 4) && (place > -1)){ row0 += i; | |
}else if((place <= 9) && (place > 4)){ row1 += i; | |
}else if((place <= 14) && (place > 9)){ row2 += i; | |
}else if((place <= 19) && (place > 14)){ row3 += i; | |
}else if(place > 19){ row4 += i; | |
} | |
} | |
newl.add(row0); newl.add(row1); newl.add(row2); newl.add(row3); newl.add(row4); | |
return newl; | |
} | |
public static List<String> buildVert(List<String> horiz) { | |
List<String> newc = new ArrayList<String>(); | |
String col0 = ""; String col1 = ""; String col2 = ""; String col3 = ""; String col4 = ""; | |
for (int row = 0; row <= 4; row++ ) { | |
String colStr = ""; | |
String[] cArray = horiz.get(row).split("(?!^)"); | |
for(int col = 0; col <= 4; col++){ | |
String ch = cArray[col]; | |
int i = Integer.parseInt(ch); | |
// System.out.printf("%d : %d %n", col, i); | |
if(col == 0){ col0 += i; | |
}else if(col == 1){ col1 += i; | |
}else if(col == 2){ col2 += i; | |
}else if(col ==3){ col3 += i; | |
}else if(col == 4){ col4 += i; | |
} | |
} | |
} | |
newc.add(col0); newc.add(col1); newc.add(col2); newc.add(col3); newc.add(col4); | |
return newc; | |
} | |
public static List<String> buildDiag(List<String> horiz) { | |
List<String> newd = new ArrayList<String>(); | |
String diag0 = ""; String diag1 = ""; | |
// System.out.printf("%d : %d %n", col, i); | |
for (int r = 0; r <= 4; r++ ) { | |
String colStr = ""; | |
String[] cArray = horiz.get(r).split("(?!^)"); | |
for(int c = 0; c <= 4; c++){ | |
String ch = cArray[c]; | |
int i = Integer.parseInt(ch); | |
if(c == 0){ | |
if(r == 0){ diag0 += i; }else if(r == 4){ diag1 += i; } | |
}else if(c == 1){ | |
if(r == 1){ diag0 += i; }else if(r == 3){ diag1 += i; } | |
}else if(c == 2){ | |
if(r == 2){ diag0 += i; diag1 += i; } | |
}else if(c ==3){ | |
if(r == 3){ diag0 += i; }else if(r == 1){ diag1 += i; } | |
}else if(c == 4){ | |
if(r == 4){ diag0 += i; }else if(r == 0){ diag1 += i; } | |
} | |
} | |
} | |
newd.add(diag0); newd.add(diag1); | |
return newd; | |
} | |
public static String reverseString(String source) { | |
int i, len = source.length(); | |
StringBuilder dest = new StringBuilder(len); | |
for (i = (len - 1); i >= 0; i--){ dest.append(source.charAt(i)); } | |
return dest.toString(); | |
} | |
public static int randInt(int min, int max) { | |
Random rand = new Random(); | |
int randomNum = rand.nextInt((max - min) + 1) + min; | |
return randomNum; | |
} | |
public static boolean isPrimeOrNot(int num) { | |
if (num < 0) { return false; } | |
if (num == 0 || num == 1) { return false; } | |
if (num == 2 || num == 3) { return true; } | |
if ((num * num - 1) % 24 == 0) { return true; } | |
else { return false; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment