Solutions
This commit is contained in:
parent
79b9099274
commit
d3aa42a3e0
64 changed files with 2726 additions and 0 deletions
6
sheet1/C/.vscode/settings.json
vendored
Normal file
6
sheet1/C/.vscode/settings.json
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"files.associations": {
|
||||
"iostream": "cpp",
|
||||
"ostream": "cpp"
|
||||
}
|
||||
}
|
||||
30
sheet1/C/Makefile
Normal file
30
sheet1/C/Makefile
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#
|
||||
# use GNU-Compiler tools
|
||||
COMPILER=GCC_
|
||||
# alternatively from the shell
|
||||
# export COMPILER=GCC_
|
||||
# or, alternatively from the shell
|
||||
# make COMPILER=GCC_
|
||||
|
||||
# use Intel compilers
|
||||
#COMPILER=ICC_
|
||||
|
||||
# use PGI compilers
|
||||
# COMPILER=PGI_
|
||||
|
||||
|
||||
SOURCES = main.cpp
|
||||
OBJECTS = $(SOURCES:.cpp=.o)
|
||||
|
||||
PROGRAM = main.${COMPILER}
|
||||
|
||||
# uncomment the next to lines for debugging and detailed performance analysis
|
||||
CXXFLAGS += -g
|
||||
LINKFLAGS += -g
|
||||
# do not use -pg with PGI compilers
|
||||
|
||||
ifndef COMPILER
|
||||
COMPILER=GCC_
|
||||
endif
|
||||
|
||||
include ../${COMPILER}default.mk
|
||||
0
sheet1/C/Makefile:Zone.Identifier
Normal file
0
sheet1/C/Makefile:Zone.Identifier
Normal file
BIN
sheet1/C/main.GCC_
Executable file
BIN
sheet1/C/main.GCC_
Executable file
Binary file not shown.
70
sheet1/C/main.cpp
Normal file
70
sheet1/C/main.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#include <iostream>
|
||||
#include "timing.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
unsigned long multiplesOf3Or5(unsigned int n)
|
||||
{
|
||||
unsigned long sum = 0;
|
||||
for(unsigned int i=1; i<=n; i++)
|
||||
{
|
||||
if(i % 3 == 0 || i % 5 == 0)
|
||||
{
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
unsigned long gauss_sum(unsigned long n)
|
||||
{
|
||||
return n*(n+1)/2;
|
||||
}
|
||||
|
||||
unsigned long multiplesOf3Or5_noLoop(unsigned int n)
|
||||
{
|
||||
/*for n there are floor(n/3) multiples of 3 and floor(n/5) multiples of 5. Calculate them using Gauss summation and subtract the
|
||||
multiples of 15 (counted twice)*/
|
||||
unsigned long multiples3 = gauss_sum(n/3)*3;
|
||||
unsigned long multiples5 = gauss_sum(n/5)*5;
|
||||
unsigned long multiples15 = gauss_sum(n/15)*15;
|
||||
|
||||
return multiples3 + multiples5 - multiples15;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
unsigned long mA,mB,mC,mD,mE,mF;
|
||||
|
||||
tic();
|
||||
for(unsigned int i = 0; i< 10000; i++)
|
||||
{
|
||||
mA = multiplesOf3Or5(15);
|
||||
mB = multiplesOf3Or5(1001);
|
||||
mC = multiplesOf3Or5(1432987);
|
||||
}
|
||||
double timeA = toc();
|
||||
|
||||
tic();
|
||||
for(unsigned int i = 0; i< 10000; i++)
|
||||
{
|
||||
mD = multiplesOf3Or5_noLoop(15);
|
||||
mE = multiplesOf3Or5_noLoop(1001);
|
||||
mF = multiplesOf3Or5_noLoop(1432987);
|
||||
}
|
||||
double timeB = toc();
|
||||
|
||||
cout << "n = 15, result = " << mA << endl;
|
||||
cout << "n = 1001, result = " << mB << endl;
|
||||
cout << "n = 1432987, result = " << mC << endl;
|
||||
cout << "time: " << timeA << endl;
|
||||
cout << "--------------------------------------------------------------" <<endl;
|
||||
cout << "n = 15, result = " << mD << endl;
|
||||
cout << "n = 1001, result = " << mE << endl;
|
||||
cout << "n = 1432987, result = " << mF << endl;
|
||||
cout << "time: " << timeB << endl;
|
||||
return 0;
|
||||
}
|
||||
BIN
sheet1/C/main.o
Normal file
BIN
sheet1/C/main.o
Normal file
Binary file not shown.
51
sheet1/C/timing.h
Normal file
51
sheet1/C/timing.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
//
|
||||
// Gundolf Haase, Oct 18 2024
|
||||
//
|
||||
#pragma once
|
||||
#include <chrono> // timing
|
||||
#include <stack>
|
||||
|
||||
//using Clock = std::chrono::system_clock; //!< The wall clock timer chosen
|
||||
using Clock = std::chrono::high_resolution_clock;
|
||||
using TPoint= std::chrono::time_point<Clock>;
|
||||
|
||||
// [Galowicz, C++17 STL Cookbook, p. 29]
|
||||
|
||||
std::stack<TPoint> MyStopWatch; //!< starting time of stopwatch
|
||||
|
||||
/** Starts stopwatch timer.
|
||||
* Use as @code tic(); myfunction(...) ; double tsec = toc(); @endcode
|
||||
*
|
||||
* The timining can be nested and the recent time point is stored on top of the stack.
|
||||
*
|
||||
* @return recent time point
|
||||
* @see toc
|
||||
*/
|
||||
auto tic()
|
||||
{
|
||||
MyStopWatch.push(Clock::now());
|
||||
return MyStopWatch.top();
|
||||
}
|
||||
|
||||
/** Returns the elapsed time from stopwatch.
|
||||
*
|
||||
* The time point from top of the stack is used
|
||||
* if time point @p t_b is not passed as input parameter.
|
||||
* Use as @code tic(); myfunction(...) ; double tsec = toc(); @endcode
|
||||
* or as @code auto t_b = tic(); myfunction(...) ; double tsec = toc(t_b); @endcode
|
||||
* The last option is to be used in the case of
|
||||
* non-nested but overlapping time measurements.
|
||||
*
|
||||
* @param[in] t_b start time of some stop watch
|
||||
* @return elapsed time in seconds.
|
||||
*
|
||||
*/
|
||||
double toc(TPoint const &t_b = MyStopWatch.top())
|
||||
{
|
||||
// https://en.cppreference.com/w/cpp/chrono/treat_as_floating_point
|
||||
using Unit = std::chrono::seconds;
|
||||
using FpSeconds = std::chrono::duration<double, Unit::period>;
|
||||
auto t_e = Clock::now();
|
||||
MyStopWatch.pop();
|
||||
return FpSeconds(t_e-t_b).count();
|
||||
}
|
||||
0
sheet1/C/timing.h:Zone.Identifier
Normal file
0
sheet1/C/timing.h:Zone.Identifier
Normal file
Loading…
Add table
Add a link
Reference in a new issue