Last active
October 11, 2015 14:06
-
-
Save zyuiop/fd45a041679101bb82d0 to your computer and use it in GitHub Desktop.
Because we all want to count in base 42.
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
| import java.util.Arrays; | |
| import java.util.List; | |
| /** | |
| * @author zyuiop | |
| */ | |
| public class TetraHexaDecaunalInteger { | |
| private int[] factors; | |
| private List<Character> characters = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F'); | |
| public TetraHexaDecaunalInteger(int... factors) { | |
| this.factors = factors; | |
| } | |
| public TetraHexaDecaunalInteger(TetraHexaDecaunalInteger integer) { | |
| this.factors = integer.factors; | |
| } | |
| public TetraHexaDecaunalInteger(String tetraHexaString) { | |
| if (tetraHexaString.startsWith("~")) | |
| tetraHexaString = tetraHexaString.substring(1); | |
| int[] factors = new int[tetraHexaString.length()]; | |
| int i = 0; | |
| for (char c : tetraHexaString.toCharArray()) { | |
| factors[i] = characters.indexOf(c); | |
| i++; | |
| } | |
| this.factors = factors; | |
| } | |
| public Integer toInteger() { | |
| int value = 0; | |
| int maxPower = factors.length - 1; | |
| for (int i = maxPower; i >= 0; i--) { | |
| value += Math.pow(42, i) * factors[maxPower - i]; | |
| } | |
| return value; | |
| } | |
| public static TetraHexaDecaunalInteger fromDecimal(Long decimalValue) { | |
| return new TetraHexaDecaunalInteger(toFactors(decimalValue)); | |
| } | |
| /** | |
| * Returns all the factors of 42 powers | |
| * For example, the number 45 will return the following array : | |
| * [1, 3] | |
| * Because it can be written 1 * 42^1 + 3 * 42^0 | |
| * @return An array of ints | |
| */ | |
| public static int[] toFactors(long decimalValue) { | |
| int maxPow = getMaxPower(decimalValue); | |
| int[] factors = new int[maxPow + 1]; | |
| long value = decimalValue; | |
| for (int i = maxPow; i >= 0; i--) { | |
| long power = (long) Math.pow(42, i); | |
| int decimalResult = (int) Math.floorDiv(value, power); | |
| value -= (decimalResult * power); | |
| factors[maxPow - i] = decimalResult; | |
| } | |
| return factors; | |
| } | |
| private static int getMaxPower(long decimalValue) { | |
| int i = 0; | |
| while (Math.pow(42, i) <= decimalValue) { | |
| i++; | |
| } | |
| return (i >= 1 ? i-1 : 0); | |
| } | |
| public int[] getFactors() { | |
| return factors; | |
| } | |
| @Override | |
| public String toString() { | |
| StringBuilder builder = new StringBuilder(); | |
| boolean first = true; | |
| int power = factors.length - 1; | |
| for (int factor : factors) { | |
| if (factor > 0) { | |
| if (!first) | |
| builder.append(" + "); | |
| builder.append(factor).append(" * 42^(").append(power).append(")"); | |
| } | |
| if (first) | |
| first = false; | |
| power--; | |
| } | |
| return builder.toString(); | |
| } | |
| public String toTetraHexaDecaunalString() { | |
| StringBuilder sb = new StringBuilder(); | |
| for (int power : getFactors()) { | |
| sb.append(characters.get(power)); | |
| } | |
| return "~" + sb.toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment