How to do hyperoperation math?

Hyperoperation math is like so: 1[1]1 = 2, or 1[2]1 = 1. Basically the [] integer is the symbol, so 1 is +, 2 is *, and 3 is ^ etc. So how would I do this in Luau?

print(f(2, 4, 3))

What I would want: 16

Any ideas?

3 Likes

Why not just see what the value is and handle it accordingly using if statements?

2 Likes

I’m not just going to use a bunch of if statements, plus, what if I did 2[5]2, luau doesn’t have 2^^^2 built in.

2 Likes

Like @awry_y said, use if statements or make a module for it.

1 Like

No other way. That’s the only way.

1 Like

I’m SURE there is another way.

Kind of yes. Kind of no. The exact syntax you show probably won’t natively work. Though if you want to pursue it look into how interpreted programming languages work. While those are more complex, the overall ideas would work for this.

1 Like
function H(a, n, b)
	if n == 0 then return a + 1 end
	if n == 1 then return a + b end
	if n == 2 then return a * b end
	if b == 0 then return 1 end
	return H(a, n-1, H(a, n, b-1))
end

print(H(3, 4, 3)) → 7625597484987

note that due to integer limits within Luau, this will not be able to construct numbers greater than 2^63-1

2 Likes

This looks great and is exactly what I’m looking for! Thanks @1waffle1!

That is what we said btw. Using if statements. That user just providing a pre-built code.

Maybe you should try understanding us rather than waiting for someone to give you the entire code. It’s also notable that it’s against forum laws.

1 Like

Yes, but I was wondering on how to do the BIGGER BASES such as 6 or so, not how to do addition, for that I can just do 1 + 1.

More generally, the solution to this is nothing to do with “if statements” which are a given in almost anything, but it is recursion. Each hyperoperator is a recursive iteration of the previous hyperoperator, i.e. a[n]b = a[n-1](a[n](b-1)), with some base cases for n=0 and b=0 so that the recursive definition eventually terminates.

Then you should further clarify about your goal.

Anyways, this is solved. No need to argue about this. I am going. Bye.

Like ECMAScript, there’s semantically no explicit integer data type in Luau.
The “number” data type at a visible virtual machine level in Luau is IEEE-754 binary64. That means the significand (excluding cases like subnormal numbers) is 53 binary digits precise, which is scaled by powers of twos. Therefore, any integer representation after 2^53 will only be precise to the first 53 significant binary digits.


To OP,
I understand you got your solution, but I want to comment something I believe is more important:
Do you have any use for this? Even in mathematics, it’s hard to see any uses for hyperoperations.
In fact, I wouldn’t think multiplication as a repeated addition and exponentiation as repeated multiplication. It is already unclear on how would multiplication of two real numbers be interpreted in terms of repeated addition.

The fact that you’re posting this topic suggests that you’re not confident in applying the concept of hyperations, at least in high level programming languages.
Therefore I would like to ask, can you construct a definition of hyperoperation in mathematics?
The reason why I’m asking this is that if you can construct a mathematical definition of hyperoperation, you ought to be able to implement a simple mathematical definition like hyperoperations in any high level programming languages like Luau, and maybe even addressing the semantic gaps. Therefore what is the part that you are struggling implementing the mathematical definition (hyperoperation) to Luau with?
If you cannot, it would be reasonable for me to suggest that you start from practicing mathematics instead of jumping into programming a mathematical subroutine like hyperoperation without having an idea on how to construct a mathematical definition beforehand. Start from here Hyperoperation - Wikipedia and take your time to understand what is going on (instead of just memorising though try not to forget shortly), then apply that to target programming language (Luau in your case).

This is assuming that you’re not concerned about performance.

Yes, I probably should have done that I admit. Thanks @Blockzez and @awry_y

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.