#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <string>
#include <cmath>
// Structure to represent a computational task
struct ComputationalTask {
std::string name;
int priority;
double complexity;
ComputationalTask(std::string n, int p, double c) : name(n), priority(p), complexity(c) {}
// Overload the comparison operator for priority queue
bool operator<(const ComputationalTask& other) const {
return priority < other.priority;
}
};
// Priority Queue for managing computational tasks
class TaskScheduler {
private:
std::priority_queue<ComputationalTask> tasks;
public:
void addTask(const ComputationalTask& task) {
tasks.push(task);
}
ComputationalTask getNextTask() {
if (tasks.empty()) {
throw std::runtime_error("No tasks available");
}
ComputationalTask nextTask = tasks.top();
tasks.pop();
return nextTask;
}
bool hasTasks() const {
return !tasks.empty();
}
};
// Recursive function to compute factorial
unsigned long long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
// Recursive function for numerical integration using Simpson's rule
double simpsonIntegration(std::function<double(double)> f, double a, double b, int depth, int maxDepth) {
double c = (a + b) / 2;
double h = b - a;
double fa = f(a), fb = f(b), fc = f(c);
double s = (h / 6) * (fa + 4 * fc + fb);
if (depth >= maxDepth) {
return s;
}
double left = simpsonIntegration(f, a, c, depth + 1, maxDepth);
double right = simpsonIntegration(f, c, b, depth + 1, maxDepth);
return left + right;
}
int main() {
TaskScheduler scheduler;
// Add some computational tasks
scheduler.addTask(ComputationalTask("Matrix Multiplication", 3, 100.0));
scheduler.addTask(ComputationalTask("FFT", 2, 50.0));
scheduler.addTask(ComputationalTask("Neural Network Training", 1, 500.0));
scheduler.addTask(ComputationalTask("Data Preprocessing", 4, 30.0));
std::cout << "Processing tasks in priority order:\n";
while (scheduler.hasTasks()) {
ComputationalTask task = scheduler.getNextTask();
std::cout << "Executing task: " << task.name
<< " (Priority: " << task.priority
<< ", Complexity: " << task.complexity << ")\n";
}
std::cout << "\nDemonstrating recursive factorial calculation:\n";
int n = 5;
std::cout << "Factorial of " << n << " is: " << factorial(n) << "\n";
std::cout << "\nDemonstrating recursive numerical integration:\n";
auto f = [](double x) { return std::sin(x); }; // Function to integrate
double a = 0, b = M_PI;
int maxDepth = 10;
double result = simpsonIntegration(f, a, b, 0, maxDepth);
std::cout << "Integral of sin(x) from 0 to pi: " << result << "\n";
return 0;
}