Pushing everything again, accidentally deleted my remote repository

This commit is contained in:
jakob.schratter 2025-12-09 22:06:13 +01:00
commit 1bee3e8e5b
101 changed files with 9428 additions and 0 deletions

View file

@ -0,0 +1,31 @@
#include "special_sum.h"
#include "../utils/timing.h"
#include <iostream>
#include <chrono>
#include <stack>
using namespace std;
int main(int argc, char **argv)
{
// check results and compare speeds
for(size_t n : {15, 1001, 1432987})
{
cout << "n = " << n << endl;
size_t sum_1, sum_2;
tic();
for(size_t i = 0; i < 1000; ++i)
sum_1 = special_sum_loop(n);
double time_1 = toc();
tic();
for(size_t i = 0; i < 1000; ++i)
sum_2 = special_sum_noloop(n);
double time_2 = toc();
cout << "loop: " << sum_1 << "\t\tDuration: " << time_1 << endl;
cout << "no loop: " << sum_2 << "\t\tDuration: " << time_2 << endl << "---------------------------------------------------" << endl;
}
return 0;
}

View file

@ -0,0 +1,28 @@
#include "special_sum.h"
size_t gauss_sum(size_t n)
{
return (n*(n+1))/2;
}
size_t special_sum_loop(size_t n)
{
size_t sum = 0;
for (size_t i = 1; i < n+1; ++i)
{
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
return sum;
}
size_t special_sum_noloop(size_t n)
{
size_t factor_3 = gauss_sum(n/3); // dividing int by int automatically gets rounded off
size_t factor_5 = gauss_sum(n/5);
size_t factor_15 = gauss_sum(n/15);
return factor_3*3 + factor_5*5 - factor_15*15;
}

View file

@ -0,0 +1,18 @@
#include <cstddef>
/**
This function returns the sum of all positive integers less or equal n which are a multiples of 3 or of 5, WITH using a loop.
@param[in] n
@param[out] M
*/
size_t special_sum_loop(size_t n);
/**
This function returns the sum of all positive integers less or equal n which are a multiples of 3 or of 5, WITHOUT using a loop.
Example: For n=15, we have 60 = 3+5+6+9+10+12+15 = (1+2+3+4+5)*3 + (1+2+3)*5 - 1*15
Formula: M = (\sum_{i=1}^{k_3} i)*3 + (\sum_{i=1}^{k_5} i)*5 - (\sum_{i=1}^{k_15} i)*15
@param[in] n
@param[out] M
*/
size_t special_sum_noloop(size_t n);