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)
370 if (stack.
size() == 1)
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",
416 TCLAP::ValueArg<string>
methodArg(
"m",
"method",
417 "Method to use (all, recursive, iterative, stack)",
421 TCLAP::SwitchArg
timeArg(
"t",
"time",
422 "Show execution time for each method",
428 int n =
nArg.getValue();
432 cout <<
"Fibonacci Number Computation" <<
endl;
433 cout <<
"============================" <<
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;
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;
477 cout <<
"Recursive: SKIPPED (n > 40 too slow)" <<
endl;
482 if (
method ==
"all" && n <= 40)
486 cout <<
"Verification: All methods agree!" <<
endl;
488 cout <<
"ERROR: Methods disagree!" <<
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(format, args...)
Print an error message (always enabled).
Stack implemented with simple dynamic array and with bounds verification.
T & top() const
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
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.