File tree Expand file tree Collapse file tree 3 files changed +118
-0
lines changed
docs/programming_and_computer_usage Expand file tree Collapse file tree 3 files changed +118
-0
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . IO ;
3
+
4
+ class Program
5
+ {
6
+ static int factorial ( int n )
7
+ {
8
+ if ( n == 0 )
9
+ return 1 ;
10
+ else
11
+ return checked ( factorial ( n - 1 ) * n ) ;
12
+ }
13
+
14
+ static void Main ( )
15
+ {
16
+ Console . WriteLine ( int . MaxValue ) ;
17
+
18
+ int x = int . MaxValue ;
19
+ int y = x + 1 ;
20
+ Console . WriteLine ( y ) ;
21
+
22
+ try
23
+ {
24
+ y = checked ( x + 1 ) ;
25
+ }
26
+ catch ( System . OverflowException )
27
+ {
28
+ Console . WriteLine ( "An overflow exception was thrown." ) ;
29
+ }
30
+
31
+ int count0 = 0 ;
32
+ int n0 = 3 ;
33
+ try
34
+ {
35
+ while ( true )
36
+ {
37
+ count0 ++ ;
38
+ n0 = checked ( n0 + 1 ) ;
39
+ }
40
+ }
41
+ catch ( System . OverflowException )
42
+ {
43
+ Console . WriteLine ( "An overflow exception was thrown after " + count0 + " iterations." ) ;
44
+ // An overflow exception was thrown after 2147483645 iterations.
45
+ }
46
+
47
+ int count1 = 0 ;
48
+ int n1 = 3 ;
49
+ try
50
+ {
51
+ while ( true )
52
+ {
53
+ count1 ++ ;
54
+ n1 = checked ( n1 * n1 ) ;
55
+ }
56
+ }
57
+ catch ( System . OverflowException )
58
+ {
59
+ Console . WriteLine ( "An overflow exception was thrown after " + count1 + " iterations." ) ;
60
+ // An overflow exception was thrown after 5 iterations.
61
+ }
62
+
63
+ int count2 = 0 ;
64
+ int n2 = 3 ;
65
+ try
66
+ {
67
+ while ( true )
68
+ {
69
+ count2 ++ ;
70
+ n2 = factorial ( n2 ) ;
71
+ }
72
+ }
73
+ catch ( System . OverflowException )
74
+ {
75
+ Console . WriteLine ( "An overflow exception was thrown after " + count2 + " iterations." ) ;
76
+ // An overflow exception was thrown after 3 iterations.
77
+ }
78
+
79
+ int count3 = 0 ;
80
+ int n3 = 100 ;
81
+ try
82
+ {
83
+ while ( true )
84
+ {
85
+ count3 ++ ;
86
+ n3 = checked ( n3 * ( int ) Math . Log ( n3 , 2 ) ) ;
87
+ }
88
+ }
89
+ catch ( System . OverflowException )
90
+ {
91
+ Console . WriteLine ( "An overflow exception was thrown after " + count3 + " iterations." ) ;
92
+ // An overflow exception was thrown after 7 iterations.
93
+ }
94
+ }
95
+ }
Original file line number Diff line number Diff line change
1
+ # Some Notes on Complexity
2
+
3
+ Have a look at the [ Big-O complexity chart] ( https://www.bigocheatsheet.com/ ) :
4
+
5
+ ![ Big-O Complexity Chart] ( https://www.bigocheatsheet.com/img/big-o-complexity-chart.png )
6
+
7
+ A function [ has an order] ( https://en.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions ) , it can be for example
8
+
9
+ - constant (O(c)),
10
+ - logarithmic (O(log n)),
11
+ - linear (O(n)),
12
+ - [ linearithmic] ( https://en.wikipedia.org/wiki/Time_complexity#Quasilinear_time ) (O(n log n)),
13
+ - quadratic (O(n^2)),
14
+ - cubic (O(n^3)),
15
+ - exponential (O(c^n)),
16
+ - factorial (O(n!)).
17
+
18
+ This can make a * very* significant difference, as exemplified in the following code:
19
+
20
+ ```
21
+ !include code/snippets/complexity.cs
22
+ ```
Original file line number Diff line number Diff line change 15
15
./docs/programming_and_computer_usage/keyboard_shortcuts.md
16
16
./docs/programming_and_computer_usage/datatypes_in_csharp.md
17
17
./docs/programming_and_computer_usage/uml_class.md
18
+ ./docs/programming_and_computer_usage/complexity.md
18
19
./lectures/
19
20
./lectures/intro/
20
21
./lectures/intro/computers_and_programming.md
You can’t perform that action at this time.
0 commit comments