Skip to content

Commit 308b84c

Browse files
committed
Improved complexity notes.
1 parent 06e7d48 commit 308b84c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

source/docs/programming_and_computer_usage/complexity.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,22 @@ Have a look at the [Big-O complexity chart](https://www.bigocheatsheet.com/):
7777

7878
![Big-O Complexity Chart](https://www.bigocheatsheet.com/img/big-o-complexity-chart.png)
7979

80+
### Experimentation
81+
82+
How fast a function grows can make a *very* significant difference, as exemplified in [this code available to download](./code/projects/GrowthMagnitudes.zip).
83+
In it, we "experiment" to see how fast factorial, exponential, cubic, quadratic, linearithmic, linear and logarithmic functions can produce an integer that is larger than `int.MaxValue`, whose value is $2,147,483,647$.
84+
85+
For example, the test for exponential growth is as follows:
8086

81-
This can make a *very* significant difference, as exemplified in [the following code](./code/projects/GrowthMagnitudes.zip):
8287

8388
```{download="./code/projects/GrowthMagnitudes.zip"}
8489
!include`snippetStart="// Exponential",snippetEnd="// Cubic"` code/projects/GrowthMagnitudes/GrowthMagnitudes/Program.cs
8590
```
8691

92+
C#'s default `Math.Pow` method returns a `double` (because, as you will see, producing something an `int` cannot hold goes *very fast*), so we cast it back to an `int`, in a `checked` environment so that C# will raise an exception if an overflow occurs.
93+
Our `while(true)` loop *will* terminate, since we are bound to produce a value overflowing, and then we simply display the value our input reached: in this case, it was enough to "feed" the value $31$ to the exponential function to make it go over `int.MaxValue`.
8794

88-
89-
Do not hesitate to edit this code to have all input values starting at 0 to "feel" the difference it makes in terms of time!
90-
95+
[Download the project](./code/projects/GrowthMagnitudes.zip) to see how fast other magnitude produce a value exceeding `int`'s capacity, and do not hesitate to edit this code to have all input values starting at 0 to "feel" the difference it makes in terms of time!
9196

9297
# The Example of Integers
9398

0 commit comments

Comments
 (0)