Created
July 22, 2018 14:53
-
-
Save FredrikAugust/0f2899fb765e7e2661e59c3e49e563da to your computer and use it in GitHub Desktop.
/r/dailyprogrammer #365 - "Up Arrow Notation" (Knuth)
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
#include <cassert> | |
#include <cmath> | |
#include <iostream> | |
long calc(long a, long n, long b) | |
{ | |
if (n == 1) | |
return static_cast<long>(pow(a, b)); | |
else if (n >= 1 && b <= 0) | |
return 1; | |
return calc(a, n-1, calc(a, n, b-1)); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
long a = std::stoi(argv[1]); // left-side of operator | |
long n = std::stoi(argv[2]); // the factor of the arrow / number of arrows | |
long b = std::stoi(argv[3]); // right-side of operator | |
std::cout << "a=" << a << " n=" << n << " b=" << b << std::endl; | |
long result = calc(a, n, b); | |
std::cout << "Result: " << result << std::endl; | |
// testing | |
std::cout << "Starting tests." << std::endl; | |
assert(calc(2, 1, 4) == 16); | |
assert(calc(2, 2, 3) == 16); | |
assert(calc(2, 2, 4) == 65536); | |
assert(calc(2, 3, 3) == 65536); | |
assert(calc(1, 1, 0) == 1); | |
assert(calc(1, 2, 0) == 1); | |
assert(calc(-1, 3, 3) == -1); | |
std::cout << "Testing complete." << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment