Package updates, more Ollama, fixes
This commit is contained in:
@@ -505,13 +505,13 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def execute_python(code):\n",
|
||||
" try:\n",
|
||||
" output = io.StringIO()\n",
|
||||
" sys.stdout = output\n",
|
||||
" exec(code)\n",
|
||||
" finally:\n",
|
||||
" sys.stdout = sys.__stdout__\n",
|
||||
" return output.getvalue()"
|
||||
" try:\n",
|
||||
" output = io.StringIO()\n",
|
||||
" sys.stdout = output\n",
|
||||
" exec(code)\n",
|
||||
" finally:\n",
|
||||
" sys.stdout = sys.__stdout__\n",
|
||||
" return output.getvalue()"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -581,14 +581,6 @@
|
||||
"\n",
|
||||
"ui.launch(inbrowser=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "77a80857-4632-4de8-a28f-b614bcbe2f40",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
@@ -607,7 +599,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.10"
|
||||
"version": "3.11.11"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -696,7 +696,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.10"
|
||||
"version": "3.11.11"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
BIN
week4/optimized
BIN
week4/optimized
Binary file not shown.
@@ -1,51 +1,72 @@
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <limits>
|
||||
#include <iomanip>
|
||||
|
||||
// Function to generate random numbers using Mersenne Twister
|
||||
std::mt19937 gen(42);
|
||||
using namespace std;
|
||||
using namespace chrono;
|
||||
|
||||
// Function to calculate maximum subarray sum
|
||||
int max_subarray_sum(int n, int min_val, int max_val) {
|
||||
std::uniform_int_distribution<> dis(min_val, max_val);
|
||||
int max_sum = std::numeric_limits<int>::min();
|
||||
int current_sum = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
current_sum += dis(gen);
|
||||
if (current_sum > max_sum) {
|
||||
max_sum = current_sum;
|
||||
}
|
||||
if (current_sum < 0) {
|
||||
current_sum = 0;
|
||||
}
|
||||
class LCG {
|
||||
private:
|
||||
uint64_t value;
|
||||
static const uint64_t a = 1664525;
|
||||
static const uint64_t c = 1013904223;
|
||||
static const uint64_t m = 1ULL << 32;
|
||||
|
||||
public:
|
||||
LCG(uint64_t seed) : value(seed) {}
|
||||
|
||||
uint64_t next() {
|
||||
value = (a * value + c) % m;
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
int64_t max_subarray_sum(int n, uint64_t seed, int min_val, int max_val) {
|
||||
LCG lcg(seed);
|
||||
vector<int64_t> random_numbers(n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
random_numbers[i] = lcg.next() % (max_val - min_val + 1) + min_val;
|
||||
}
|
||||
|
||||
int64_t max_sum = numeric_limits<int64_t>::min();
|
||||
int64_t current_sum = 0;
|
||||
int64_t min_sum = 0;
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
current_sum += random_numbers[i];
|
||||
max_sum = max(max_sum, current_sum - min_sum);
|
||||
min_sum = min(min_sum, current_sum);
|
||||
}
|
||||
|
||||
return max_sum;
|
||||
}
|
||||
|
||||
// Function to calculate total maximum subarray sum
|
||||
int total_max_subarray_sum(int n, int initial_seed, int min_val, int max_val) {
|
||||
gen.seed(initial_seed);
|
||||
int total_sum = 0;
|
||||
int64_t total_max_subarray_sum(int n, uint64_t initial_seed, int min_val, int max_val) {
|
||||
int64_t total_sum = 0;
|
||||
LCG lcg(initial_seed);
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
total_sum += max_subarray_sum(n, min_val, max_val);
|
||||
uint64_t seed = lcg.next();
|
||||
total_sum += max_subarray_sum(n, seed, min_val, max_val);
|
||||
}
|
||||
return total_sum;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n = 10000; // Number of random numbers
|
||||
int initial_seed = 42; // Initial seed for the Mersenne Twister
|
||||
int min_val = -10; // Minimum value of random numbers
|
||||
int max_val = 10; // Maximum value of random numbers
|
||||
const int n = 10000;
|
||||
const uint64_t initial_seed = 42;
|
||||
const int min_val = -10;
|
||||
const int max_val = 10;
|
||||
|
||||
// Timing the function
|
||||
auto start_time = std::chrono::high_resolution_clock::now();
|
||||
int result = total_max_subarray_sum(n, initial_seed, min_val, max_val);
|
||||
auto end_time = std::chrono::high_resolution_clock::now();
|
||||
auto start_time = high_resolution_clock::now();
|
||||
int64_t result = total_max_subarray_sum(n, initial_seed, min_val, max_val);
|
||||
auto end_time = high_resolution_clock::now();
|
||||
|
||||
std::cout << "Total Maximum Subarray Sum (20 runs): " << result << std::endl;
|
||||
std::cout << "Execution Time: " << std::setprecision(6) << std::fixed << std::chrono::duration<double>(end_time - start_time).count() << " seconds" << std::endl;
|
||||
auto duration = duration_cast<microseconds>(end_time - start_time);
|
||||
|
||||
cout << "Total Maximum Subarray Sum (20 runs): " << result << endl;
|
||||
cout << "Execution Time: " << fixed << setprecision(6) << duration.count() / 1e6 << " seconds" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user