177# include <tclap/CmdLine.h>
182using namespace Aleph;
238 for (
int i = 2; i <= n; i++)
272constexpr char P1 = 1;
273constexpr char P2 = 2;
276# define NUM(p) ((p)->n)
277# define F1(p) ((p)->f1)
278# define RESULT(p) ((p)->result)
279# define RETURN_POINT(p) ((p)->return_point)
323 if (
NUM(current_ar) <= 1)
325 RESULT(caller_ar) = (
NUM(current_ar) <= 0) ? 0 : 1;
326 goto return_from_fib;
337 caller_ar = &stack.
top(1);
338 current_ar = &stack.
top();
344 F1(current_ar) =
RESULT(current_ar);
354 caller_ar = &stack.
top(1);
355 current_ar = &stack.
top();
370 if (stack.
size() == 1)
374 caller_ar = &stack.
top(1);
375 current_ar = &stack.
top();
398int main(
int argc,
char *argv[])
403 "Fibonacci number computation using three methods:\n"
404 " recursive - Classic recursive (slow for large n)\n"
405 " iterative - Bottom-up loop (fast)\n"
406 " stack - Explicit activation records (educational)\n",
409 TCLAP::ValueArg<int> nArg(
"n",
"number",
410 "Fibonacci index to compute",
414 vector<string> allowedMethods = {
"all",
"recursive",
"iterative",
"stack"};
415 TCLAP::ValuesConstraint<string> methodConstraint(allowedMethods);
416 TCLAP::ValueArg<string> methodArg(
"m",
"method",
417 "Method to use (all, recursive, iterative, stack)",
418 false,
"all", &methodConstraint);
421 TCLAP::SwitchArg timeArg(
"t",
"time",
422 "Show execution time for each method",
426 cmd.parse(argc, argv);
428 int n = nArg.getValue();
429 string method = methodArg.getValue();
430 bool showTime = timeArg.getValue();
432 cout <<
"Fibonacci Number Computation" << endl;
433 cout <<
"============================" << endl;
434 cout <<
"Computing fib(" << n <<
")" << endl << endl;
437 if ((method ==
"all" || method ==
"recursive") && n > 40)
439 cout <<
"WARNING: n > 40 with recursive method will be very slow!" << endl;
440 cout <<
" Consider using -m iterative or -m stack" << endl << endl;
443 auto measure_time = [](
auto func,
int n,
const string& name,
bool show) {
444 auto start = chrono::high_resolution_clock::now();
445 long long result = func(n);
446 auto end = chrono::high_resolution_clock::now();
447 auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
449 cout << name <<
": fib(" << n <<
") = " << result;
451 cout <<
" [" << duration.count() <<
" us]";
457 long long result_iter = 0, result_rec = 0, result_stack = 0;
459 if (method ==
"all" || method ==
"iterative")
461 result_iter = measure_time(
fib_iterative, n,
"Iterative", showTime);
464 if (method ==
"all" || method ==
"stack")
466 result_stack = measure_time(
fib_stack, n,
"Stack ", showTime);
469 if (method ==
"all" || method ==
"recursive")
473 result_rec = measure_time(
fib_recursive, n,
"Recursive", showTime);
477 cout <<
"Recursive: SKIPPED (n > 40 too slow)" << endl;
482 if (method ==
"all" && n <= 40)
485 if (result_iter == result_rec && result_rec == result_stack)
486 cout <<
"Verification: All methods agree!" << endl;
488 cout <<
"ERROR: Methods disagree!" << endl;
491 cout << endl <<
"Done." << endl;
493 catch (TCLAP::ArgException &e)
495 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() << endl;
Exception handling system with formatted messages for Aleph-w.
#define AH_ERROR(...)
Print an error message (always enabled).
Stack implemented with simple dynamic array and with bounds verification.
T & top()
Return a modifiable reference to youngest element of stack (called the top)
size_t size() const noexcept
Return the number of elements stored in the stack.
T & pushn(const size_t &n=1)
Push n cells into the stack.
T pop()
Extract the last more recently inserted element.
long long fib_recursive(int n)
Compute Fibonacci number recursively.
long long fib_iterative(int n)
Compute Fibonacci number iteratively.
constexpr char P2
Return point after second recursive call fib(n-2)
constexpr char P1
Return point labels (simulating goto targets after recursive calls)
long long fib_stack(int n)
Compute Fibonacci number using explicit stack management.
#define NUM(p)
Accessor macros for top of stack (current activation record)
Main namespace for Aleph-w library functions.
Activation record structure for stack-based Fibonacci.
long long f1
Local variable: stores result of fib(n-1)
int n
Parameter: which Fibonacci number to compute.
char return_point
Continuation: where to resume after return.
long long result
Return value to pass to caller.
Stack implementations backed by dynamic or fixed arrays.