1
+ #include < stdio.h>
2
+ #include < stdlib.h>
3
+ #include < math.h>
4
+ #include < string.h>
5
+
6
+ #define CRT_SECURE_NO_WARNINS
7
+
8
+ int get_number (const char * msg);
9
+
10
+ int is_prime (int n);
11
+ void print_first_primes (int n);
12
+ void print_primes (int n);
13
+ void approx_sqrt (int n, int p);
14
+ void print_first_digits_of_fraction (int n, int k, int m);
15
+ void print_pascal (int n);
16
+
17
+ void print_consecutives_sum (int n);
18
+
19
+ int main ()
20
+ {
21
+ while (1 )
22
+ {
23
+ printf (" \n\n " );
24
+ printf (" 0 - exit\n " );
25
+ printf (" 1 - show primes\n " );
26
+ printf (" 2 - show first n primes\n " );
27
+ printf (" 3 - consecutive numbers sum\n " );
28
+ printf (" 4 - first N digits of K/M (K<M)\n " );
29
+ printf (" 6 - Pascal triangle\n " );
30
+ printf (" 7 - approx sqrt(n)\n " );
31
+ int action = get_number (" >>> " );
32
+ if (!action) break ;
33
+
34
+ if (action == 1 )
35
+ {
36
+ int n = get_number (" N = " );
37
+ print_primes (n);
38
+ }
39
+ else if (action == 2 )
40
+ {
41
+ int n = get_number (" N = " );
42
+ print_first_primes (n);
43
+ }
44
+ else if (action == 3 )
45
+ {
46
+ int n = get_number (" N = " );
47
+ print_consecutives_sum (n);
48
+ }
49
+ else if (action == 4 )
50
+ {
51
+ int n = get_number (" N = " );
52
+ int k = get_number (" K = " );
53
+ int m = get_number (" M = " );
54
+ print_first_digits_of_fraction (n, k, m);
55
+ }
56
+ else if (action == 6 )
57
+ {
58
+ int n = get_number (" N = " );
59
+ print_pascal (n);
60
+ }
61
+ else if (action == 7 )
62
+ {
63
+ int n = get_number (" N = " );
64
+ int p = get_number (" precision = " );
65
+ approx_sqrt (n, p);
66
+ }
67
+ }
68
+ return 0 ;
69
+ }
70
+
71
+ // / <summary>
72
+ // / Reads int from input
73
+ // / </summary>
74
+ // / <param name="msg">message to display</param>
75
+ // / <returns>user input integer</returns>
76
+ int get_number (const char * msg)
77
+ {
78
+ if (msg != NULL )
79
+ {
80
+ printf (msg);
81
+ }
82
+ int result;
83
+ scanf_s (" %i" , &result);
84
+ return result;
85
+ }
86
+
87
+ // / <summary>
88
+ // / Checks if n is prime
89
+ // / </summary>
90
+ // / <param name="n">number to check</param>
91
+ // / <returns>1 if n is prime, 0 otherwise</returns>
92
+ int is_prime (int n)
93
+ {
94
+ if (n < 2 ) return 0 ;
95
+ for (int d = 2 ; d * d <= n; d++)
96
+ if (n % d == 0 )
97
+ return 0 ;
98
+ return 1 ;
99
+ }
100
+
101
+ // / <summary>
102
+ // / prints primes up to n (n>0)
103
+ // / </summary>
104
+ // / <param name="n">upper limit</param>
105
+ void print_primes (int n)
106
+ {
107
+ if (n <= 0 )
108
+ {
109
+ printf (" Number must be positive\n " );
110
+ return ;
111
+ }
112
+ printf (" Primes : " );
113
+ for (int i = 2 ; i < n; i++)
114
+ if (is_prime (i))
115
+ {
116
+ printf (" %i " , i);
117
+ }
118
+ printf (" \n " );
119
+ }
120
+
121
+ // / <summary>
122
+ // / prints first n primes
123
+ // / </summary>
124
+ // / <param name="n">number of primes to print</param>
125
+ void print_first_primes (int n)
126
+ {
127
+ if (n <= 0 )
128
+ {
129
+ printf (" N must be a non-zero positive.\n " );
130
+ return ;
131
+ }
132
+ int k = 1 ;
133
+ printf (" Primes : " );
134
+ for (; n > 0 ; k++)
135
+ {
136
+ if (is_prime (k))
137
+ {
138
+ printf (" %i " , k);
139
+ n--;
140
+ }
141
+ }
142
+ }
143
+
144
+ // / <summary>
145
+ // / Prints approximated sqrt(n) with p decimals
146
+ // / </summary>
147
+ // / <param name="n">sqrt argument (positive)</param>
148
+ // / <param name="p">number of decimals, p=0..7 </param>
149
+ void approx_sqrt (int n, int p)
150
+ {
151
+ if (p < 0 || p>7 )
152
+ {
153
+ printf (" Precision must be in range 0..7\n " );
154
+ return ;
155
+ }
156
+ if (n < 0 )
157
+ {
158
+ printf (" Sqrt argument cannot be negative" );
159
+ return ;
160
+ }
161
+ printf (" %.*f\n " , p, sqrt (n));
162
+ }
163
+
164
+
165
+ // / <summary>
166
+ // / prints first n digits of k/m (k<m)
167
+ // / </summary>
168
+ // / <param name="n">number of digits</param>
169
+ // / <param name="k">nominator</param>
170
+ // / <param name="m">denominator ??</param>
171
+ void print_first_digits_of_fraction (int n, int k, int m)
172
+ {
173
+ if (k >= m)
174
+ {
175
+ printf (" Fraction value must be <1\n " );
176
+ return ;
177
+ }
178
+ if (n <= 0 )
179
+ {
180
+ printf (" Number of digits must be a non-zero positive.\n " );
181
+ return ;
182
+ }
183
+ printf (" 0." );
184
+ for (int i = 0 ; i < n; i++)
185
+ {
186
+ k *= 10 ;
187
+ while (k < m)
188
+ {
189
+ k *= 10 ;
190
+ printf (" 0" );
191
+ }
192
+ printf (" %i" , k / m);
193
+ k = k % m;
194
+ }
195
+ }
196
+
197
+ // / <summary>
198
+ // / prints Pascal triangle of length n
199
+ // / </summary>
200
+ // / <param name="n">number of lines to print</param>
201
+ void print_pascal (int n)
202
+ {
203
+ int ** mat = (int **)malloc ((n + 1 ) * sizeof (int *));
204
+
205
+ for (int i = 0 ; i <= n; i++)
206
+ mat[i] = (int *)malloc ((n + 1 ) * sizeof (int *));
207
+
208
+ mat[0 ][0 ] = 1 ;
209
+ for (int i = 1 ; i <= n; i++)
210
+ {
211
+ mat[i][0 ] = 1 ;
212
+ mat[i][i] = 1 ;
213
+ for (int j = 1 ; j < i; j++)
214
+ mat[i][j] = mat[i - 1 ][j - 1 ] + mat[i - 1 ][j];
215
+ }
216
+
217
+ for (int i = 0 ; i <= n; i++)
218
+ {
219
+ for (int j = 0 ; j <= i; j++)
220
+ printf (" %i " , mat[i][j]);
221
+ printf (" \n " );
222
+ free (mat[i]);
223
+ }
224
+
225
+ free (mat);
226
+ }
227
+
228
+ // / <summary>
229
+ // / prints consecutive numbers that sum to n starting form k, if exists
230
+ // / </summary>
231
+ // / <param name="n">total sum</param>
232
+ // / <param name="k">first number</param>
233
+ void print_consecutives_starting_with (int n, int k)
234
+ {
235
+ int s = 0 , cnt = 0 ;
236
+ while (s < n)
237
+ {
238
+ s += k;
239
+ k++;
240
+ cnt++;
241
+ }
242
+ k -= cnt;
243
+ if (s == n)
244
+ {
245
+ for (int i = 0 ; i < cnt; i++)
246
+ printf (" %d " , k + i);
247
+ printf (" \n " );
248
+ }
249
+ }
250
+
251
+ // / <summary>
252
+ // / prints all consecutive numbers that add up to n
253
+ // / </summary>
254
+ // / <param name="n">target number</param>
255
+ void print_consecutives_sum (int n)
256
+ {
257
+ for (int i = 1 ; i <= n / 2 ; i++)
258
+ print_consecutives_starting_with (n, i);
259
+ }
0 commit comments