Last active
February 26, 2018 04:14
-
-
Save alex-born/dcc397d184eda545306cf30a8efabd15 to your computer and use it in GitHub Desktop.
A implementation of hyperfunctions in Java.
This file contains 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
/** | |
* Hyperoperations. | |
* @author alexdborn <alex.born@alexdborn.com>, Aidan Epperly | |
* @version 1.0 | |
*/ | |
import java.util.Collections; | |
public class Hyperfunctions { | |
/** | |
* Provides abstrction for working with hyper fuctions. | |
*/ | |
public enum Operator { | |
SUCCESSION(0, "++"), | |
ADDITION(1, "+"), | |
MULTIPLICATION(2, "*"), | |
EXPONENTATION(3), | |
TETRATION(4), | |
PENTATION(5); | |
private final int order; | |
private final String symbol; | |
Operator(int o, String s){ | |
order = o; | |
symbol = s; | |
} | |
Operator(int o){ | |
order = o; | |
// Repeates order - 1 "^" | |
symbol = String.join("", Collections.nCopies(order - 2, "^")); // Ascii repersentation of the the up-arrow | |
} | |
public int getOrder(){ | |
return order; | |
} | |
public String getSymbol() { | |
return symbol; | |
} | |
} | |
/** | |
* Wrapes a operation with some abstraction. | |
* @param n integer operand | |
* @param m repitition operand | |
* @param operator | |
*/ | |
public static int preform(int n, int m, Operator operator){ | |
return h(n, m, operator.getOrder()); | |
} | |
// If you don't like postfix | |
public static int preform(int n, Operator operator, int m){ | |
return preform(n, m, operator); | |
} | |
/** | |
* A generalization of hyperoperators. | |
* @param n integer operand | |
* @param m repitition operand | |
* @param l order (level) | |
*/ | |
public static int h(int n, int m, int l){ | |
assert l >= 0 && n >= 0 && m >= 0 : "H is not defined."; | |
// DEBUG | |
// System.out.print("CALL: h(" + n + ", " + m + ", " + l + ")\n"); | |
/* | |
* | Level | Operation | Base Case: m = 0 | | |
* |-------|--------------------|----------------------------| | |
* | 0 | Succession | m + 1 | | |
* | 1 | Addition | n | | |
* | 2 | Multiplication | 0 | | |
* | 3 | Exponentiation | 1 | | |
* | l > 3 | Tetration, pen ... | 1 (successive powers at 0) | | |
*/ | |
if(l == 0) return m + 1; // H0 | |
if(m == 0) { | |
if(l == 1) return n; // H1 | |
if(l == 2) return 0; // H2 | |
if(l > 2) return 1; // H(l > 2) | |
} | |
/* | |
Repeate the l - 1 operation h(n, m - 1, l) times until m is reached. | |
4^3 = 4 * (4^2) | |
= 4 * 4 * (4^1) | |
= 4 * 4 * 4 * 1(Base case) | |
= Repeate until increment operation | |
Defined from the pattern: | |
a + b = a + (b - 1) + 1 | |
a * b = a + (a + (b - 1)) | |
a ^ b = a * (a^(b - 1)) | |
a ^^ b = a^(a^^(b - 1)) | |
*/ | |
return h(n, h(n, m - 1, l), l - 1); | |
} | |
public static void main(String[] args) { | |
// Example of preforming a single operation | |
// preform(3, Operation.MULTIPLICATION, 2); | |
// Java only allocates enough memory for me to do 2 ^^ 3 on my computer without hitting the heap | |
for (int l = 0; l < 4; l++) { | |
for (int m = 0; m < 5; m++) { | |
for (int n = 0; n < 5; n++) { | |
// Returns the symbol for the lth operation | |
System.out.print(n + " " + Operator.values()[l].getSymbol() + " " + m + " = " + h(n, m, l) + "\n"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment