1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
| using System.Collections;
using System.Collections.Generic;
namespace sample1
{
using Application = javafx.application.Application;
//import javafx.fxml.FXMLLoader;
using Insets = javafx.geometry.Insets;
using Parent = javafx.scene.Parent;
using Scene = javafx.scene.Scene;
using javafx.scene.control;
using GridPane = javafx.scene.layout.GridPane;
using Stage = javafx.stage.Stage;
internal class tripsBig
{
internal tripsBig(System.Numerics.BigInteger one, System.Numerics.BigInteger two, System.Numerics.BigInteger three)
{
d = one;
x = two;
y = three;
}
internal tripsBig()
{
;
}
internal System.Numerics.BigInteger d;
internal System.Numerics.BigInteger x;
internal System.Numerics.BigInteger y;
}
public class Main : Application
{
public virtual int gcd(int a, int b)
{
while (b != 0)
{
int tmp = a % b;
a = b;
b = tmp;
}
return a;
}
public virtual int md(int a, int b)
{
int mod = 0;
for (int i = 1; i < b; i++)
{
int res = i * a;
if (res % b == 1)
{
mod = i;
}
}
return mod;
}
public virtual string epow(int a, int b, int n)
{
int nod = gcd(a, n);
if (nod == 1)
{
System.Numerics.BigInteger a1 = new BigInteger(a.ToString());
System.Numerics.BigInteger n1 = new BigInteger(n.ToString());
System.Numerics.BigInteger v = gcdWide(a1, n1).x;
int res_n = n1.intValue();
int res_v = v.intValue();
int mod;
if (res_v <= 0)
{
res_v = res_n + res_v;
}
mod = (res_v * b) % res_n;
ArrayList list = new List<int>();
for (int i = 0; i < 11; i++)
{
list.Add(mod + res_n * i);
}
return "x =" + mod + "\nResult: x = " + mod + "+ " + res_n + "t\nExample: " + list;
}
else
{
System.Numerics.BigInteger a1 = new BigInteger(a.ToString());
System.Numerics.BigInteger n1 = new BigInteger(n.ToString());
System.Numerics.BigInteger b1 = new BigInteger(b.ToString());
int res_n = n1.intValue();
int res_a = a1.intValue();
int res_b = b1.intValue();
int d = res_b % nod;
if (d == 0)
{
res_n = res_n / nod;
res_a = res_a / nod;
res_b = res_b / nod;
int r_nod = gcd(res_n, res_a);
System.Numerics.BigInteger v = gcdWide(a1, n1).x;
int res_v = v.intValue();
if (r_nod == 1)
{
int mod;
if (res_v <= 0)
{
res_v = res_n + res_v;
}
mod = (res_v * res_b) % res_n;
string s = "x = " + mod;
for (int i = 0; i < nod; i++)
{
s += "\nx" + (i + 1) + " = (" + mod + "+" + (res_n * i) + ")+" + n1 + "t";
}
return s;
}
else
{
return "Result Error";
}
}
else
{
return "Result Error";
}
}
}
public static int pow(int x, int n, int mod)
{
int res = 1;
for (long p = x; n > 0; n >>= 1, p = (p * p) % mod)
{
if ((n & 1) != 0)
{
res = (int)(res * p % mod);
}
}
return res;
}
internal virtual tripsBig gcdWide(System.Numerics.BigInteger a, System.Numerics.BigInteger b)
{
tripsBig temphere = new tripsBig(a,System.Numerics.BigInteger.One,System.Numerics.BigInteger.Zero);
tripsBig temphere2 = new tripsBig();
if (b == System.Numerics.BigInteger.Zero)
{
return temphere;
}
temphere2 = gcdWide(b,a % b);
temphere = new tripsBig();
temphere.d = temphere2.d;
temphere.x = temphere2.y;
temphere.y = temphere2.x - (a / b * temphere2.y);
return temphere;
}
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override public void start(javafx.stage.Stage primaryStage) throws Exception
public override void start(Stage primaryStage)
{
// Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
GridPane gridPane = new GridPane();
gridPane.Padding = new Insets(0,20,20,20);
gridPane.Vgap = 10;
gridPane.Hgap = 15;
primaryStage.Resizable = false;
TitledPane tpaneArray = new TitledPane();
GridPane gridArray = new GridPane();
gridArray.Padding = new Insets(10);
gridArray.Vgap = 5;
gridArray.Hgap = 10;
gridArray.Style = "-fx-background-color: darkgrey";
TextField textA = new TextField();
textA.PromptText = "Введите значение а:";
TextField textB = new TextField();
textB.PromptText = "Введите значение b:";
Label label = new Label("Result ");
Button button = new Button("Расчитать");
GridPane.setConstraints(textA,0,0);
gridArray.Children.add(textA);
GridPane.setConstraints(textB,0,1);
gridArray.Children.add(textB);
GridPane.setConstraints(label,0,3);
gridArray.Children.add(label);
GridPane.setConstraints(button,0,2);
gridArray.Children.add(button);
tpaneArray.Collapsible = false;
tpaneArray.Text = "Алгоритм Евклида";
tpaneArray.Content = gridArray;
TitledPane tpaneArray_r = new TitledPane();
GridPane gridArray_r = new GridPane();
gridArray_r.Padding = new Insets(10);
gridArray_r.Vgap = 5;
gridArray_r.Hgap = 10;
gridArray_r.Style = "-fx-background-color: darkgrey";
TextField textA_r = new TextField();
textA_r.PromptText = "Введите значение а:";
TextField textB_r = new TextField();
textB_r.PromptText = "Введите значение b:";
Label label_r = new Label("Result ");
Button button_r = new Button("Расчитать");
GridPane.setConstraints(textA_r,0,0);
gridArray_r.Children.add(textA_r);
GridPane.setConstraints(textB_r,0,1);
gridArray_r.Children.add(textB_r);
GridPane.setConstraints(label_r,0,3);
gridArray_r.Children.add(label_r);
GridPane.setConstraints(button_r,0,2);
gridArray_r.Children.add(button_r);
tpaneArray_r.Collapsible = false;
tpaneArray_r.Text = "Алгоритм Евклида(расширенный)";
tpaneArray_r.Content = gridArray_r;
TitledPane tpaneArray_e = new TitledPane();
GridPane gridArray_e = new GridPane();
gridArray_e.Padding = new Insets(10);
gridArray_e.Vgap = 5;
gridArray_e.Hgap = 10;
gridArray_e.Style = "-fx-background-color: darkgrey";
TextField textA_e = new TextField();
textA_e.PromptText = "Введите значение а:";
TextField textB_e = new TextField();
textB_e.PromptText = "Введите значение b:";
Label label_e = new Label("Result ");
Button button_e = new Button("Расчитать");
GridPane.setConstraints(textA_e,0,0);
gridArray_e.Children.add(textA_e);
GridPane.setConstraints(textB_e,0,1);
gridArray_e.Children.add(textB_e);
GridPane.setConstraints(label_e,0,3);
gridArray_e.Children.add(label_e);
GridPane.setConstraints(button_e,0,2);
gridArray_e.Children.add(button_e);
tpaneArray_e.Collapsible = false;
tpaneArray_e.Text = "Вычисление обратных величин";
tpaneArray_e.Content = gridArray_e;
TitledPane tpaneArray_s = new TitledPane();
GridPane gridArray_s = new GridPane();
gridArray_s.Padding = new Insets(10);
gridArray_s.Vgap = 5;
gridArray_s.Hgap = 10;
gridArray_s.Style = "-fx-background-color: darkgrey";
Label label1 = new Label("x^n(mod B)");
TextField textA_s = new TextField();
textA_s.PromptText = "Введите основание(x):";
TextField textN_s = new TextField();
textN_s.PromptText = "Введите значение степени(n):";
TextField textM_s = new TextField();
textM_s.PromptText = "Введите значение модуля(b):";
Button button1 = new Button("Расчитать");
Label label2 = new Label("Result ");
tpaneArray_s.Collapsible = false;
tpaneArray_s.Text = "Алгоритм быстрого возведения в степень";
tpaneArray_s.Content = gridArray_s;
GridPane.setConstraints(label1,0,0);
gridArray_s.Children.add(label1);
GridPane.setConstraints(label2,1,2);
gridArray_s.Children.add(label2);
GridPane.setConstraints(button1,1,1);
gridArray_s.Children.add(button1);
GridPane.setConstraints(textA_s,0,1);
GridPane.setConstraints(textN_s,0,2);
GridPane.setConstraints(textM_s,0,3);
gridArray_s.Children.addAll(textA_s,textN_s,textM_s);
TitledPane tpaneArray_l2 = new TitledPane();
GridPane gridArray_l2 = new GridPane();
gridArray_l2.Padding = new Insets(10);
gridArray_l2.Vgap = 5;
gridArray_l2.Hgap = 10;
gridArray_l2.Style = "-fx-background-color: darkgrey";
TextField textA_l2 = new TextField();
textA_l2.PromptText = "Введите основание(x):";
TextField textB_l2 = new TextField();
textB_l2.PromptText = "Введите значение(b):";
TextField textN_l2 = new TextField();
textN_l2.PromptText = "Введите значение модуля(n):";
Button button12 = new Button("Расчитать");
Label label22 = new Label("Result ");
tpaneArray_l2.Collapsible = false;
tpaneArray_l2.Text = "Сравнение первого степеня";
tpaneArray_l2.Content = gridArray_l2;
GridPane.setConstraints(label22,0,4);
GridPane.setRowSpan(label22,5);
gridArray_l2.Children.add(label22);
GridPane.setConstraints(button12,1,1);
gridArray_l2.Children.add(button12);
GridPane.setConstraints(textA_l2,0,1);
GridPane.setConstraints(textB_l2,0,2);
GridPane.setConstraints(textN_l2,0,3);
gridArray_l2.Children.addAll(textA_l2,textB_l2,textN_l2);
GridPane.setConstraints(tpaneArray,0,0);
GridPane.setColumnSpan(tpaneArray,10);
gridPane.Children.add(tpaneArray);
GridPane.setConstraints(tpaneArray_r,10,0);
GridPane.setColumnSpan(tpaneArray_r,10);
gridPane.Children.add(tpaneArray_r);
GridPane.setConstraints(tpaneArray_s,20,0);
GridPane.setColumnSpan(tpaneArray_s,10);
gridPane.Children.add(tpaneArray_s);
GridPane.setConstraints(tpaneArray_e,10,1);
GridPane.setColumnSpan(tpaneArray_e,10);
gridPane.Children.add(tpaneArray_e);
GridPane.setConstraints(tpaneArray_l2,0,1);
GridPane.setColumnSpan(tpaneArray_l2,10);
gridPane.Children.add(tpaneArray_l2);
Pattern p = Pattern.compile("(\\d+\\.?\\d*)?");
textA.textProperty().addListener((observable, oldValue, newValue) =>
{
if (!p.matcher(newValue).matches())
{
textA.Text = oldValue;
}
});
textB.textProperty().addListener((observable, oldValue, newValue) =>
{
if (!p.matcher(newValue).matches())
{
textA.Text = oldValue;
}
});
textA_r.textProperty().addListener((observable, oldValue, newValue) =>
{
if (!p.matcher(newValue).matches())
{
textA_r.Text = oldValue;
}
});
textB_r.textProperty().addListener((observable, oldValue, newValue) =>
{
if (!p.matcher(newValue).matches())
{
textA_r.Text = oldValue;
}
});
textA_s.textProperty().addListener((observable, oldValue, newValue) =>
{
if (!p.matcher(newValue).matches())
{
textA_s.Text = oldValue;
}
});
textN_s.textProperty().addListener((observable, oldValue, newValue) =>
{
if (!p.matcher(newValue).matches())
{
textN_s.Text = oldValue;
}
});
textM_s.textProperty().addListener((observable, oldValue, newValue) =>
{
if (!p.matcher(newValue).matches())
{
textM_s.Text = oldValue;
}
});
button.OnAction = @event =>
{
try
{
if (!textA.Text.Empty || !textB.Text.Empty)
{
int a = int.Parse(textA.Text);
int b = int.Parse(textB.Text);
if (a >= 0 || b >= 0)
{
label.Text = "Result " + gcd(a, b);
}
else
{
label.Text = "Result " + "Error";
}
}
else
{
label.Text = "Result " + "Error";
}
}
catch (Exception)
{
label.Text = "Result Error";
}
};
button1.OnAction = @event =>
{
try
{
if (!textA_s.Text.Empty || !textN_s.Text.Empty || !textM_s.Text.Empty)
{
int a = int.Parse(textA_s.Text);
int n = int.Parse(textN_s.Text);
int m = int.Parse(textM_s.Text);
if (a >= 0 || n >= 0 || m >= 0)
{
label2.Text = "Result " + pow(a, n, m);
}
else
{
label2.Text = "Result " + "Error";
}
}
else
{
label2.Text = "Result " + "Error";
}
}
catch (Exception)
{
label2.Text = "Result Error";
}
};
button_r.OnAction = @event =>
{
try
{
if (!textA_r.Text.Empty || !textB_r.Text.Empty)
{
int a = int.Parse(textA_r.Text);
int b = int.Parse(textB_r.Text);
if (a >= 0 || b >= 0)
{
BigInteger a1 = new BigInteger(a.ToString());
BigInteger b1 = new BigInteger(b.ToString());
label_r.Text = "Result НОД = " + gcdWide(a1, b1).d + ", U = " + gcdWide(a1, b1).x + ", V = " + gcdWide(a1, b1).y;
}
else
{
label_r.Text = "Result " + "Error";
}
}
else
{
label_r.Text = "Result " + "Error";
}
}
catch (Exception)
{
label_r.Text = "Result Error";
}
};
button_e.OnAction = @event =>
{
try
{
if (!textA_e.Text.Empty || !textB_e.Text.Empty)
{
int a = int.Parse(textA_e.Text);
int b = int.Parse(textB_e.Text);
if (a >= 0 || b >= 0)
{
label_e.Text = "Result: X = " + md(a,b);
}
else
{
label_e.Text = "Result " + "Error";
}
}
else
{
label_e.Text = "Result " + "Error";
}
}
catch (Exception)
{
label_e.Text = "Result Error";
}
};
button12.OnAction = @event =>
{
try
{
if (!textA_l2.Text.Empty || !textB_l2.Text.Empty || !textN_l2.Text.Empty)
{
int a = int.Parse(textA_l2.Text);
int b = int.Parse(textB_l2.Text);
int n = int.Parse(textN_l2.Text);
if (a >= 0 || b >= 0)
{
label22.Text = epow(a,b,n);
}
else
{
label22.Text = "Result " + "Error";
}
}
else
{
label22.Text = "Result " + "Error";
}
}
catch (Exception)
{
label22.Text = "Result Error";
}
};
primaryStage.Title = "НОД и т.д";
primaryStage.Scene = new Scene(gridPane, 800, 400);
primaryStage.show();
}
public static void Main(string[] args)
{
launch(args);
}
}
} |