diff --git a/3sem/data structures and algos/01/program.cpp b/3sem/data structures and algos/01/program.cpp new file mode 100644 index 0000000..c992d9a --- /dev/null +++ b/3sem/data structures and algos/01/program.cpp @@ -0,0 +1,76 @@ +// dsaa_01.cpp +// Горбацевич Андрей +#include +#include +#include + +typedef double long_type; + +void time_passed(std::chrono::system_clock::time_point start, long_type& holder); +void test(int N); +void algo_01(int N, long_type& out_val); // Naive way +void algo_02(int N, long_type& out_val); // Somewhat efficient way + +int main() { + int i; + std::cout << "N="; + std::cin >> i; + + test(i); + + return 0; +} + +inline void time_passed(std::chrono::system_clock::time_point start, long_type& holder) { + auto stop = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(stop - start); + holder = duration.count(); +} + +void test(int N) { + long_type fft, sft = fft = 0; + long_type ffv, sfv = ffv = 0; + + { + auto start = std::chrono::high_resolution_clock::now(); + algo_01(N, ffv); + time_passed(start, fft); + } + { + auto start = std::chrono::high_resolution_clock::now(); + algo_02(N, sfv); + time_passed(start, sft); + } + + printf( + "algo_01 is %s than algo_02 by %.0f microseconds (%.0f against %.0f; N=%d; %.0f; %.0f)\n" + "Sum of i! from 1 to %d = %.0f", + (fft > sft? "slower" : "faster"), fabs(fft - sft), fft, sft, N, ffv, sfv, N, ffv + ); +} + +void algo_01(int N, long_type& out_val) { // O(N^2) + long_type sum = 0; + + for (int k = 1; k <= N; k++) { // O(N) + long_type acc = 1; + for (int i = 1; i <= k; i++) { // O(N) + acc *= i; + } + sum += acc; + } + + out_val = sum; +} + +void algo_02(int N, long_type& out_val) { // O(N) + long_type sum = 0; + long_type acc = 1; + + for (int k = 1; k <= N; k++) { // O(N) + sum += (acc *= k); + } + + out_val = sum; +} +// Вычислить суммы факториалов K первых целых чисел для K=1..N diff --git a/3sem/data structures and algos/01/аисд отчёт прак 1.pdf b/3sem/data structures and algos/01/аисд отчёт прак 1.pdf new file mode 100644 index 0000000..529fc87 Binary files /dev/null and b/3sem/data structures and algos/01/аисд отчёт прак 1.pdf differ