jrcppintro 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. jrcppintro-0.1.0/PKG-INFO +12 -0
  2. jrcppintro-0.1.0/jrcppintro/__init__.py +3 -0
  3. jrcppintro-0.1.0/jrcppintro/__version__.py +1 -0
  4. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter1/exercise/pi.cpp +21 -0
  5. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter1/exercise/pi.py +15 -0
  6. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter1/exercise/pi_numpy.py +14 -0
  7. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter2/demo/01-hello.cpp +6 -0
  8. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter2/demo/02-output.cpp +14 -0
  9. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter2/exercise/hello.cpp +6 -0
  10. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter2/exercise/table.cpp +8 -0
  11. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter2/solution/hello.cpp +6 -0
  12. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter2/solution/table.cpp +12 -0
  13. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter3/demo/01-types.cpp +17 -0
  14. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter3/demo/02-arithmetic.cpp +13 -0
  15. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter3/demo/02-arithmetic.py +11 -0
  16. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter3/demo/03-sizes.cpp +19 -0
  17. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter3/exercise/factorial.cpp +10 -0
  18. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter3/exercise/harmonic.cpp +15 -0
  19. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter3/solution/auto.cpp +11 -0
  20. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter3/solution/factorial.cpp +10 -0
  21. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter3/solution/harmonic.cpp +21 -0
  22. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/demo/01-branches.cpp +20 -0
  23. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/demo/02-loops.cpp +15 -0
  24. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/demo/03-break-continue.cpp +15 -0
  25. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/demo/04-vector.cpp +22 -0
  26. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/exercise/newton.cpp +11 -0
  27. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/exercise/powers.cpp +12 -0
  28. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/exercise/primes.cpp +33 -0
  29. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/exercise/primes.py +21 -0
  30. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/exercise/sieve.cpp +12 -0
  31. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/exercise/stats.cpp +17 -0
  32. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/solution/newton.cpp +18 -0
  33. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/solution/powers.cpp +10 -0
  34. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/solution/sieve.cpp +27 -0
  35. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter4/solution/stats.cpp +30 -0
  36. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/demo/01-functions.cpp +16 -0
  37. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/demo/02-copies.cpp +24 -0
  38. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/demo/03-two-files/Makefile +10 -0
  39. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/demo/03-two-files/main.cpp +10 -0
  40. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/demo/03-two-files/stats.cpp +20 -0
  41. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/demo/03-two-files/stats.h +6 -0
  42. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/exercise/fill.cpp +15 -0
  43. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/exercise/integrate.cpp +16 -0
  44. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/exercise/stats.cpp +30 -0
  45. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/solution/integrate.cpp +26 -0
  46. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter5/solution/stats.cpp +35 -0
  47. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/demo/01-addresses.cpp +21 -0
  48. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/demo/02-arrays.cpp +21 -0
  49. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/demo/03-owning.cpp +22 -0
  50. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/exercise/addresses.cpp +10 -0
  51. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/exercise/bug1.cpp +14 -0
  52. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/exercise/bug2.cpp +14 -0
  53. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/exercise/scale.cpp +18 -0
  54. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/exercise/walk.cpp +11 -0
  55. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/solution/addresses.cpp +18 -0
  56. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/solution/bug1.cpp +14 -0
  57. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/solution/bug2.cpp +14 -0
  58. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/solution/scale.cpp +32 -0
  59. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter6/solution/walk.cpp +11 -0
  60. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/demo/Makefile +15 -0
  61. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/demo/main.cpp +10 -0
  62. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/demo/stats.cpp +20 -0
  63. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/demo/stats.h +6 -0
  64. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/exercise/Makefile +15 -0
  65. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/exercise/main.cpp +10 -0
  66. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/exercise/stats.cpp +20 -0
  67. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/exercise/stats.h +6 -0
  68. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/solution/Makefile +18 -0
  69. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/solution/main.cpp +10 -0
  70. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/solution/stats.cpp +20 -0
  71. jrcppintro-0.1.0/jrcppintro/examples/cpp_scripts/chapter7/solution/stats.h +6 -0
  72. jrcppintro-0.1.0/jrcppintro/examples.py +30 -0
  73. jrcppintro-0.1.0/pyproject.toml +13 -0
@@ -0,0 +1,12 @@
1
+ Metadata-Version: 2.1
2
+ Name: jrcppintro
3
+ Version: 0.1.0
4
+ Summary: Jumping Rivers: Introduction to C++
5
+ Author: Jumping Rivers
6
+ Author-email: info@jumpingrivers.com
7
+ Requires-Python: >=3.10
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Requires-Dist: numpy (>=2.0,<3.0)
@@ -0,0 +1,3 @@
1
+ from .__version__ import __version__ # noqa: F401
2
+
3
+ from . import examples # noqa: F401
@@ -0,0 +1 @@
1
+ __version__ = "0.1.0"
@@ -0,0 +1,21 @@
1
+ #include <chrono>
2
+ #include <iostream>
3
+
4
+ int main() {
5
+ const int n = 10000000;
6
+
7
+ auto start = std::chrono::steady_clock::now();
8
+
9
+ double total = 0.0;
10
+ for (int i = 0; i < n; ++i) {
11
+ double x = (i + 0.5) / n;
12
+ total += 4.0 / (1.0 + x * x);
13
+ }
14
+ double pi = total / n;
15
+
16
+ auto end = std::chrono::steady_clock::now();
17
+ double elapsed = std::chrono::duration<double>(end - start).count();
18
+
19
+ std::cout << "C++: pi is about " << pi << ", took " << elapsed << " s\n";
20
+ return 0;
21
+ }
@@ -0,0 +1,15 @@
1
+ import time
2
+
3
+ N = 10_000_000
4
+
5
+ start = time.perf_counter()
6
+
7
+ total = 0.0
8
+ for i in range(N):
9
+ x = (i + 0.5) / N
10
+ total += 4.0 / (1.0 + x * x)
11
+ pi = total / N
12
+
13
+ elapsed = time.perf_counter() - start
14
+
15
+ print(f"pure Python: pi is about {pi:.5f}, took {elapsed:.2f} s")
@@ -0,0 +1,14 @@
1
+ import time
2
+
3
+ import numpy as np
4
+
5
+ N = 10_000_000
6
+
7
+ start = time.perf_counter()
8
+
9
+ x = (np.arange(N) + 0.5) / N
10
+ pi = np.sum(4.0 / (1.0 + x * x)) / N
11
+
12
+ elapsed = time.perf_counter() - start
13
+
14
+ print(f"NumPy: pi is about {pi:.5f}, took {elapsed:.2f} s")
@@ -0,0 +1,6 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ std::cout << "Hello from C++\n";
5
+ return 0;
6
+ }
@@ -0,0 +1,14 @@
1
+ #include <iomanip>
2
+ #include <iostream>
3
+
4
+ int main() {
5
+ std::cout << "Two pieces, " << "one line\n";
6
+ std::cout << "A number: " << 42 << "\n";
7
+ std::cout << "An expression: " << 6 * 7 << "\n";
8
+ std::cout << "A decimal: " << 2.5 * 3 << "\n";
9
+
10
+ std::cout << std::setw(6) << "x" << std::setw(8) << "x*x" << "\n";
11
+ std::cout << std::setw(6) << 2 << std::setw(8) << 2 * 2 << "\n";
12
+ std::cout << std::setw(6) << 12 << std::setw(8) << 12 * 12 << "\n";
13
+ return 0;
14
+ }
@@ -0,0 +1,6 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ std::cout << "Hello from C++\n";
5
+ return 0;
6
+ }
@@ -0,0 +1,8 @@
1
+ #include <iomanip>
2
+ #include <iostream>
3
+
4
+ int main() {
5
+ std::cout << std::setw(4) << "n" << std::setw(8) << "n*n" << std::setw(8) << "n*n*n" << "\n";
6
+ std::cout << std::setw(4) << 1 << std::setw(8) << 1 * 1 << std::setw(8) << 1 * 1 * 1 << "\n";
7
+ return 0;
8
+ }
@@ -0,0 +1,6 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ std::cout << "Hello from C++\n";
5
+ return 0;
6
+ }
@@ -0,0 +1,12 @@
1
+ #include <iomanip>
2
+ #include <iostream>
3
+
4
+ int main() {
5
+ std::cout << std::setw(4) << "n" << std::setw(8) << "n*n" << std::setw(8) << "n*n*n" << "\n";
6
+ std::cout << std::setw(4) << 1 << std::setw(8) << 1 * 1 << std::setw(8) << 1 * 1 * 1 << "\n";
7
+ std::cout << std::setw(4) << 2 << std::setw(8) << 2 * 2 << std::setw(8) << 2 * 2 * 2 << "\n";
8
+ std::cout << std::setw(4) << 3 << std::setw(8) << 3 * 3 << std::setw(8) << 3 * 3 * 3 << "\n";
9
+ std::cout << std::setw(4) << 4 << std::setw(8) << 4 * 4 << std::setw(8) << 4 * 4 * 4 << "\n";
10
+ std::cout << std::setw(4) << 5 << std::setw(8) << 5 * 5 << std::setw(8) << 5 * 5 * 5 << "\n";
11
+ return 0;
12
+ }
@@ -0,0 +1,17 @@
1
+ #include <iostream>
2
+ #include <string>
3
+
4
+ int main() {
5
+ int count = 3;
6
+ double ratio = 2.5;
7
+ bool done = false;
8
+ char grade = 'B';
9
+ std::string name = "Liverpool";
10
+
11
+ const double pi = 3.14159265358979;
12
+ auto total = count * ratio;
13
+
14
+ std::cout << count << " " << ratio << " " << done << " " << grade << " " << name << "\n";
15
+ std::cout << "pi = " << pi << ", total = " << total << "\n";
16
+ return 0;
17
+ }
@@ -0,0 +1,13 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ int big = 2147483647;
5
+
6
+ std::cout << "7 / 2 = " << 7 / 2 << "\n";
7
+ std::cout << "-7 / 2 = " << -7 / 2 << "\n";
8
+ std::cout << "10 / 3 * 3 = " << 10 / 3 * 3 << "\n";
9
+ std::cout << "1.0 / 3 = " << 1.0 / 3 << "\n";
10
+ std::cout << "'a' + 1 = " << 'a' + 1 << "\n";
11
+ std::cout << "big + 1 = " << big + 1 << "\n";
12
+ return 0;
13
+ }
@@ -0,0 +1,11 @@
1
+ big = 2147483647
2
+
3
+ print("7 / 2 =", 7 / 2)
4
+ print("-7 // 2 =", -7 // 2)
5
+ print("10 / 3 * 3 =", 10 / 3 * 3)
6
+ print("1.0 / 3 =", 1.0 / 3)
7
+ try:
8
+ print("'a' + 1 =", "a" + 1)
9
+ except TypeError as error:
10
+ print("'a' + 1 = TypeError:", error)
11
+ print("big + 1 =", big + 1)
@@ -0,0 +1,19 @@
1
+ #include <cstddef>
2
+ #include <iomanip>
3
+ #include <iostream>
4
+ #include <limits>
5
+
6
+ int main() {
7
+ std::cout << "int holds up to " << std::numeric_limits<int>::max() << "\n";
8
+ std::cout << "long long holds up to " << std::numeric_limits<long long>::max() << "\n";
9
+
10
+ float third_f = 1.0f / 3;
11
+ double third_d = 1.0 / 3;
12
+ std::cout << std::setprecision(17);
13
+ std::cout << "float 1/3 = " << third_f << "\n";
14
+ std::cout << "double 1/3 = " << third_d << "\n";
15
+
16
+ std::size_t n = 0;
17
+ std::cout << "size_t 0 - 1 = " << n - 1 << "\n";
18
+ return 0;
19
+ }
@@ -0,0 +1,10 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ int product = 1;
5
+ for (int n = 1; n <= 20; ++n) {
6
+ product = product * n;
7
+ std::cout << n << "! = " << product << "\n";
8
+ }
9
+ return 0;
10
+ }
@@ -0,0 +1,15 @@
1
+ #include <iomanip>
2
+ #include <iostream>
3
+
4
+ int main() {
5
+ const int n = 10000000;
6
+
7
+ double sum_double = 0.0;
8
+ for (int i = 1; i <= n; ++i) {
9
+ sum_double += 1.0 / i;
10
+ }
11
+
12
+ std::cout << std::setprecision(10);
13
+ std::cout << "double: " << sum_double << "\n";
14
+ return 0;
15
+ }
@@ -0,0 +1,11 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ auto half = 1 / 2;
5
+
6
+ auto count = 0;
7
+ count = count + 0.5;
8
+
9
+ std::cout << "half = " << half << ", count = " << count << "\n";
10
+ return 0;
11
+ }
@@ -0,0 +1,10 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ long long product = 1;
5
+ for (int n = 1; n <= 25; ++n) {
6
+ product = product * n;
7
+ std::cout << n << "! = " << product << "\n";
8
+ }
9
+ return 0;
10
+ }
@@ -0,0 +1,21 @@
1
+ #include <iomanip>
2
+ #include <iostream>
3
+
4
+ int main() {
5
+ const int n = 10000000;
6
+
7
+ double sum_double = 0.0;
8
+ for (int i = 1; i <= n; ++i) {
9
+ sum_double += 1.0 / i;
10
+ }
11
+
12
+ float sum_float = 0.0f;
13
+ for (int i = 1; i <= n; ++i) {
14
+ sum_float += 1.0f / i;
15
+ }
16
+
17
+ std::cout << std::setprecision(10);
18
+ std::cout << "double: " << sum_double << "\n";
19
+ std::cout << "float: " << sum_float << "\n";
20
+ return 0;
21
+ }
@@ -0,0 +1,20 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ double x = 0.3;
5
+
6
+ if (x < 0.0) {
7
+ std::cout << "negative\n";
8
+ } else if (x < 0.5) {
9
+ std::cout << "small\n";
10
+ } else {
11
+ std::cout << "large\n";
12
+ }
13
+
14
+ bool in_range = x >= 0.0 && x <= 1.0;
15
+ bool at_edge = x == 0.0 || x == 1.0;
16
+ if (in_range && !at_edge) {
17
+ std::cout << "strictly inside\n";
18
+ }
19
+ return 0;
20
+ }
@@ -0,0 +1,15 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ for (int i = 0; i < 5; ++i) {
5
+ std::cout << i << " ";
6
+ }
7
+ std::cout << "\n";
8
+
9
+ int n = 1;
10
+ while (n < 100) {
11
+ n = n * 2;
12
+ }
13
+ std::cout << "first power of two past 100: " << n << "\n";
14
+ return 0;
15
+ }
@@ -0,0 +1,15 @@
1
+ #include <iostream>
2
+
3
+ int main() {
4
+ for (int i = 0; i < 100; ++i) {
5
+ if (i % 2 == 0) {
6
+ continue;
7
+ }
8
+ if (i > 7) {
9
+ break;
10
+ }
11
+ std::cout << i << " ";
12
+ }
13
+ std::cout << "\n";
14
+ return 0;
15
+ }
@@ -0,0 +1,22 @@
1
+ #include <cstddef>
2
+ #include <iostream>
3
+ #include <vector>
4
+
5
+ int main() {
6
+ std::vector<double> values;
7
+ values.push_back(2.5);
8
+ values.push_back(4.0);
9
+ values.push_back(1.5);
10
+
11
+ std::cout << "size " << values.size() << ", first " << values[0] << "\n";
12
+
13
+ for (std::size_t i = 0; i < values.size(); ++i) {
14
+ values[i] = values[i] * 2;
15
+ }
16
+
17
+ for (double v : values) {
18
+ std::cout << v << " ";
19
+ }
20
+ std::cout << "\n";
21
+ return 0;
22
+ }
@@ -0,0 +1,11 @@
1
+ #include <cmath>
2
+ #include <iostream>
3
+
4
+ int main() {
5
+ const double tolerance = 1e-10;
6
+ double x = 1.0;
7
+ int steps = 0;
8
+
9
+ std::cout << "x = " << x << ", steps " << steps << ", tolerance " << tolerance << "\n";
10
+ return 0;
11
+ }
@@ -0,0 +1,12 @@
1
+ #include <iomanip>
2
+ #include <iostream>
3
+
4
+ int main() {
5
+ std::cout << std::setw(4) << "n" << std::setw(8) << "n*n" << std::setw(8) << "n*n*n" << "\n";
6
+ std::cout << std::setw(4) << 1 << std::setw(8) << 1 * 1 << std::setw(8) << 1 * 1 * 1 << "\n";
7
+ std::cout << std::setw(4) << 2 << std::setw(8) << 2 * 2 << std::setw(8) << 2 * 2 * 2 << "\n";
8
+ std::cout << std::setw(4) << 3 << std::setw(8) << 3 * 3 << std::setw(8) << 3 * 3 * 3 << "\n";
9
+ std::cout << std::setw(4) << 4 << std::setw(8) << 4 * 4 << std::setw(8) << 4 * 4 * 4 << "\n";
10
+ std::cout << std::setw(4) << 5 << std::setw(8) << 5 * 5 << std::setw(8) << 5 * 5 * 5 << "\n";
11
+ return 0;
12
+ }
@@ -0,0 +1,33 @@
1
+ #include <chrono>
2
+ #include <iostream>
3
+ #include <vector>
4
+
5
+ int main() {
6
+ const int limit = 10000000;
7
+
8
+ auto start = std::chrono::steady_clock::now();
9
+
10
+ std::vector<bool> is_prime(limit + 1, true);
11
+ is_prime[0] = false;
12
+ is_prime[1] = false;
13
+ for (int i = 2; i * i <= limit; ++i) {
14
+ if (is_prime[i]) {
15
+ for (int j = i * i; j <= limit; j += i) {
16
+ is_prime[j] = false;
17
+ }
18
+ }
19
+ }
20
+
21
+ int count = 0;
22
+ for (int i = 0; i <= limit; ++i) {
23
+ if (is_prime[i]) {
24
+ ++count;
25
+ }
26
+ }
27
+
28
+ auto end = std::chrono::steady_clock::now();
29
+ double elapsed = std::chrono::duration<double>(end - start).count();
30
+
31
+ std::cout << "C++: " << count << " primes below " << limit << ", took " << elapsed << " s\n";
32
+ return 0;
33
+ }
@@ -0,0 +1,21 @@
1
+ import time
2
+
3
+ limit = 10_000_000
4
+
5
+ start = time.perf_counter()
6
+
7
+ is_prime = [True] * (limit + 1)
8
+ is_prime[0] = False
9
+ is_prime[1] = False
10
+ i = 2
11
+ while i * i <= limit:
12
+ if is_prime[i]:
13
+ for j in range(i * i, limit + 1, i):
14
+ is_prime[j] = False
15
+ i += 1
16
+
17
+ count = sum(is_prime)
18
+
19
+ elapsed = time.perf_counter() - start
20
+
21
+ print(f"pure Python: {count} primes below {limit}, took {elapsed:.2f} s")
@@ -0,0 +1,12 @@
1
+ #include <iostream>
2
+ #include <vector>
3
+
4
+ int main() {
5
+ const int limit = 100;
6
+ std::vector<bool> is_prime(limit + 1, true);
7
+ is_prime[0] = false;
8
+ is_prime[1] = false;
9
+
10
+ std::cout << "set up " << is_prime.size() << " flags\n";
11
+ return 0;
12
+ }
@@ -0,0 +1,17 @@
1
+ #include <iostream>
2
+ #include <vector>
3
+
4
+ int main() {
5
+ std::vector<double> values;
6
+ values.push_back(2.0);
7
+ values.push_back(4.0);
8
+ values.push_back(4.0);
9
+ values.push_back(4.0);
10
+ values.push_back(5.0);
11
+ values.push_back(5.0);
12
+ values.push_back(7.0);
13
+ values.push_back(9.0);
14
+
15
+ std::cout << "n = " << values.size() << "\n";
16
+ return 0;
17
+ }
@@ -0,0 +1,18 @@
1
+ #include <cmath>
2
+ #include <iomanip>
3
+ #include <iostream>
4
+
5
+ int main() {
6
+ const double tolerance = 1e-10;
7
+ double x = 1.0;
8
+ int steps = 0;
9
+
10
+ while (std::abs(x * x - 2.0) > tolerance) {
11
+ x = x - (x * x - 2.0) / (2.0 * x);
12
+ ++steps;
13
+ }
14
+
15
+ std::cout << std::setprecision(12);
16
+ std::cout << "x = " << x << ", steps " << steps << "\n";
17
+ return 0;
18
+ }
@@ -0,0 +1,10 @@
1
+ #include <iomanip>
2
+ #include <iostream>
3
+
4
+ int main() {
5
+ std::cout << std::setw(4) << "n" << std::setw(8) << "n*n" << std::setw(8) << "n*n*n" << "\n";
6
+ for (int n = 1; n <= 10; ++n) {
7
+ std::cout << std::setw(4) << n << std::setw(8) << n * n << std::setw(8) << n * n * n << "\n";
8
+ }
9
+ return 0;
10
+ }
@@ -0,0 +1,27 @@
1
+ #include <iostream>
2
+ #include <vector>
3
+
4
+ int main() {
5
+ const int limit = 100;
6
+ std::vector<bool> is_prime(limit + 1, true);
7
+ is_prime[0] = false;
8
+ is_prime[1] = false;
9
+
10
+ for (int i = 2; i * i <= limit; ++i) {
11
+ if (is_prime[i]) {
12
+ for (int j = i * i; j <= limit; j += i) {
13
+ is_prime[j] = false;
14
+ }
15
+ }
16
+ }
17
+
18
+ int count = 0;
19
+ for (int i = 0; i <= limit; ++i) {
20
+ if (is_prime[i]) {
21
+ std::cout << i << " ";
22
+ ++count;
23
+ }
24
+ }
25
+ std::cout << "\n" << count << " primes below " << limit << "\n";
26
+ return 0;
27
+ }
@@ -0,0 +1,30 @@
1
+ #include <cmath>
2
+ #include <iostream>
3
+ #include <vector>
4
+
5
+ int main() {
6
+ std::vector<double> values;
7
+ values.push_back(2.0);
8
+ values.push_back(4.0);
9
+ values.push_back(4.0);
10
+ values.push_back(4.0);
11
+ values.push_back(5.0);
12
+ values.push_back(5.0);
13
+ values.push_back(7.0);
14
+ values.push_back(9.0);
15
+
16
+ double total = 0.0;
17
+ for (double v : values) {
18
+ total += v;
19
+ }
20
+ double mean = total / values.size();
21
+
22
+ double squares = 0.0;
23
+ for (double v : values) {
24
+ squares += (v - mean) * (v - mean);
25
+ }
26
+ double stddev = std::sqrt(squares / values.size());
27
+
28
+ std::cout << "n = " << values.size() << ", mean " << mean << ", standard deviation " << stddev << "\n";
29
+ return 0;
30
+ }
@@ -0,0 +1,16 @@
1
+ #include <iostream>
2
+ #include <vector>
3
+
4
+ double mean(std::vector<double> values) {
5
+ double total = 0.0;
6
+ for (double v : values) {
7
+ total += v;
8
+ }
9
+ return total / values.size();
10
+ }
11
+
12
+ int main() {
13
+ std::vector<double> values = {2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0};
14
+ std::cout << "mean = " << mean(values) << "\n";
15
+ return 0;
16
+ }
@@ -0,0 +1,24 @@
1
+ #include <iostream>
2
+ #include <vector>
3
+
4
+ void by_value(std::vector<double> v) {
5
+ std::cout << " inside by_value, v lives at " << &v << "\n";
6
+ v[0] = 100.0;
7
+ }
8
+
9
+ void by_reference(std::vector<double>& v) {
10
+ std::cout << " inside by_reference, v lives at " << &v << "\n";
11
+ v[0] = 100.0;
12
+ }
13
+
14
+ int main() {
15
+ std::vector<double> values(1000000, 1.0);
16
+ std::cout << "in main, values lives at " << &values << "\n";
17
+
18
+ by_value(values);
19
+ std::cout << "after by_value, values[0] = " << values[0] << "\n";
20
+
21
+ by_reference(values);
22
+ std::cout << "after by_reference, values[0] = " << values[0] << "\n";
23
+ return 0;
24
+ }
@@ -0,0 +1,10 @@
1
+ # Chapter 5 builds this project with the single g++ command on the slide; chapter 7 explains what a Makefile does.
2
+ CXX = g++
3
+ CXXFLAGS = -std=c++17 -Wall -Wextra
4
+
5
+ stats: main.cpp stats.cpp stats.h
6
+ $(CXX) $(CXXFLAGS) -o stats main.cpp stats.cpp
7
+
8
+ .PHONY: clean
9
+ clean:
10
+ rm -f stats
@@ -0,0 +1,10 @@
1
+ #include <iostream>
2
+ #include <vector>
3
+
4
+ #include "stats.h"
5
+
6
+ int main() {
7
+ std::vector<double> values = {2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0};
8
+ std::cout << "mean " << mean(values) << ", standard deviation " << stddev(values) << "\n";
9
+ return 0;
10
+ }
@@ -0,0 +1,20 @@
1
+ #include "stats.h"
2
+
3
+ #include <cmath>
4
+
5
+ double mean(const std::vector<double>& values) {
6
+ double total = 0.0;
7
+ for (double v : values) {
8
+ total += v;
9
+ }
10
+ return total / values.size();
11
+ }
12
+
13
+ double stddev(const std::vector<double>& values) {
14
+ double m = mean(values);
15
+ double total = 0.0;
16
+ for (double v : values) {
17
+ total += (v - m) * (v - m);
18
+ }
19
+ return std::sqrt(total / values.size());
20
+ }
@@ -0,0 +1,6 @@
1
+ #pragma once
2
+
3
+ #include <vector>
4
+
5
+ double mean(const std::vector<double>& values);
6
+ double stddev(const std::vector<double>& values);