-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
846 lines (746 loc) · 30.9 KB
/
main.c
File metadata and controls
846 lines (746 loc) · 30.9 KB
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ModularArithmetic.h"
#include "Modular_Arithmetic/Utility/Matrix.h"
/**
* Generates a matrix - M: row x column.
*
* @param row the number of row.
* @param column the number of column.
* @return the matrix - M: row x column.
*/
matrix *generateMatrixNxM(long long int *row, long long int *column) {
//Matrix.
matrix *matrix = NULL;
//Return scanf value.
int returnScanf = 0;
printf("Enter the number of row: ");
returnScanf = scanf("%lld", row);
assert(returnScanf > 0);
printf("Enter the number of column: ");
returnScanf = scanf("%lld", column);
assert(returnScanf > 0);
matrix = createMatrix(*row, *column);
for (long long int i = 0; i < *row; ++i) {
for (long long int j = 0; j < *column; ++j) {
printf("Insert the value for position (%lld; %lld): ", i, j);
returnScanf = scanf("%lf", &matrix->matrix[i][j]);
assert(returnScanf > 0);
}
}
printf("\n");
return matrix;
}
/**
* Generates a matrix - M: row x row.
* @param row the matrix order.
* @return the matrix - M: row x row.
*/
matrix *generateMatrixNxN(long long int *row) {
//Matrix.
matrix *matrix = NULL;
//Return scanf value.
int returnScanf = 0;
printf("Enter the matrix order: ");
returnScanf = scanf("%lld", row);
assert(returnScanf > 0);
matrix = createMatrix(*row, *row);
for (long long int i = 0; i < *row; ++i) {
for (long long int j = 0; j < *row; ++j) {
printf("Insert the value for position (%lld; %lld): ", i, j);
returnScanf = scanf("%lf", &matrix->matrix[i][j]);
assert(returnScanf > 0);
}
}
printf("\n");
return matrix;
}
/**
* Prints all possible choices.
*/
void printChoice() {
printf("Choose which operation you want to perform from the following ones:\n"
" 1. compute the sum modulo m\n"
" 2. compute the difference modulo m\n"
" 3. compute the product modulo m\n"
" 4. compute the division modulo m\n"
" 5. compute the power elevation modulo m\n"
" 6. compute the square roots modulo p using the Tonelli-Shanks algorithm\n"
" 7. compute the square roots modulo n\n"
" 8. compute the discrete logarithm modulo n\n"
" 9. compute the solution of a system of modular linear equations\n"
"10. compute the solution of a linear diophantine equation\n"
"11. check if 2 numbers are congruent modulo m\n"
"12. check if 2 numbers are coprime\n"
"13. check if a number is a divisor of another one\n"
"14. check if a number is NOT prime using the Fermat's Pseudoprime to a assumption\n"
"15. check if a number is prime\n"
"16. check if a number admits the square root modulo n - quadratic residue\n"
"17. check if a number is a primitive root modulo n\n"
"18. compute the gcd - greatest common divisor using the Euclid's algorithm\n"
"19. compute the gcd - greatest common divisor using the Extended Euclid's algorithm\n"
"20. compute the module of 2 numbers\n"
"21. compute a number congruent with the one given\n"
"22. compute the modular reduction\n"
"23. compute the modular inversion\n"
"24. factorize an odd number using the Fermat Factorization method\n"
"25. factorize a number\n"
"26. compute the Euler function\n"
"27. compute the list of prime number up to the n-th\n"
"28. search for the n-th prime number\n"
"29. search the prime number following the given one\n"
"30. compute the list of primitive roots modulo n\n"
"31. compute the list of quadratic residual modulo n\n"
"32. compute the Legendre symbol\n"
"33. compute the Jacobi symbol\n"
"34. check if a matrix has all integer elements\n"
"35. compute matrix modulo n\n"
"36. compute the matrix inversion modulo n\n"
"37. compute the sum of 2 matrices modulo n\n"
"38. compute the difference of 2 matrices modulo n\n"
"39. compute the scalar product modulo n\n"
"40. compute the product of matrices modulo n\n"
"41. compute the power elevation of matrices modulo n\n"
"42. compute the Kronecker product of matrices modulo n\n"
"43. quit\n\n");
}
/**
* Handles the user's choice.
*
* @param choice the choice.
*/
void manageChoice(int *choice) {
//The first number.
long long int x;
//The second number.
long long int y;
//The third number.
long long int x1;
//The fourth number.
long long int y1;
//The module value.
long long int module;
//The list.
long long int *list = NULL;
//Matrices.
matrix *matrix1 = NULL, *matrix2 = NULL, *res = NULL;
//Return scanf value.
int returnScanf = 0;
switch (*choice) {
case 1:
printf("You chose to compute the sum modulo m\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
printf("%lld + %lld (mod %lld) = %lld (mod %lld)\n\n\n", x, y, module, sum(x, y, module), module);
break;
case 2:
printf("You chose to compute the difference modulo m\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
printf("%lld - %lld (mod %lld) = %lld (mod %lld)\n\n\n", x, y, module, sub(x, y, module), module);
break;
case 3:
printf("You chose to compute the product modulo m\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
printf("%lld * %lld (mod %lld) = %lld (mod %lld)\n\n\n", x, y, module, product(x, y, module), module);
break;
case 4:
printf("You chose to compute the division modulo m\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
printf("%lld / %lld (mod %lld) = %lld (mod %lld)\n\n\n", x, y, module, division(x, y, module), module);
break;
case 5:
printf("You chose to compute the power elevation modulo m\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the exponent: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
printf("%lld^%lld (mod %lld) = %lld (mod %lld)\n\n\n", x, y, module, power(x, y, module), module);
break;
case 6:
printf("You chose to compute the square roots modulo p using the Tonelli-Shanks algorithm\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the module value - prime number: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
list = TonelliShanksAlgorithm(x, module);
printf("sqrt(%lld) (mod %lld) = %lld (mod %lld) %lld (mod %lld)\n\n\n", x, module, list[0], module, list[1], module);
free(list);
break;
case 7:
printf("You chose to compute the square roots modulo n\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
//The 2 square roots.
list = squareRoot(x, module, &y);
printf("sqrt(%lld) (mod %lld) = ", x, module);
for (long long int i = 0; i < y; ++i) {
printf("%lld (mod %lld) ", list[i], module);
}
free(list);
printf("\n\n\n");
break;
case 8:
printf("You chose to compute the discrete logarithm modulo n\n\n"
"Insert the base: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
printf("%lld^x = %lld (mod %lld) --> x = %lld\n\n\n", x, y, module, discreteLogarithm(x, y, module));
break;
case 9:
printf("You chose to compute the solution of a system of modular linear equation\n\n"
"Insert the number of equation: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
//The list of parameters.
long long int *parameter = malloc(x * sizeof(int));
//The list of modules.
long long int *modules = malloc(x * sizeof(int));
for (long long int i = 0; i < x; ++i) {
printf("Insert the %lld-%s parameter: ", (i + 1),
((i + 1) == 1 ? "st" : ((i + 1) == 2 ? "nd" : ((i + 1) == 3 ? "rd" : "th"))));
returnScanf = scanf("%lld", (parameter + i));
assert(returnScanf > 0);
printf("Insert the %lld-%s module: ", (i + 1),
((i + 1) == 1 ? "st" : ((i + 1) == 2 ? "nd" : ((i + 1) == 3 ? "rd" : "th"))));
returnScanf = scanf("%lld", (modules + i));
assert(returnScanf > 0);
}
y = chineseReminderTheorem(x, parameter, modules);
for (long long int i = 0; i < x; ++i) {
printf("x = %lld (mod %lld)\n", parameter[i], modules[i]);
}
printf("--> x = %lld\n\n\n", y);
free(parameter);
free(modules);
break;
case 10:
printf("You chose to compute the solution of a linear diophantine equation\n\n"
"Insert the first parameter: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second parameter: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("Insert the third parameter: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
diophantineEquation(x, &x1, y, &y1, module);
printf("%lldx + %lldy = %lld --> x = %lld, y = %lld\n\n\n", x, y, module, x1, y1);
break;
case 11:
printf("You chose to check if 2 numbers are congruent modulo m\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
printf("%lld %c= %lld (mod %lld)\n\n\n", x, areCongruent(x, y, module) == 1 ? '=' : '!', y, module);
break;
case 12:
printf("You chose to check if 2 numbers are coprime\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("%lld %s coprime %lld\n\n\n", x, areCoPrime(x, y) == 1 ? "are" : "are not", y);
break;
case 13:
printf("You chose to check if a number is a divisor of another one\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("%lld %s %lld \n\n\n", x, isDivisor(x, y) == 1 ? "|" : "!|", y);
break;
case 14:
printf("You chose to check if a number is NOT prime using the Fermat's Pseudoprime assumption\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("%lld %s prime\n\n\n", x, isFermatPseudoPrime(2, x) == 1 ? "is" : "is NOT");
break;
case 15:
printf("You chose to check if a number is prime\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("%lld %s prime\n\n\n", x, isPrime(x) == 1 ? "is" : "is NOT");
break;
case 16:
printf("You chose to check if a number admits the square root modulo n\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
printf("%lld (mod %lld) %s\n\n\n", x, module, isSquareNumber(x, module) == 1 ? "admits the square root" : "does NOT admit the square root");
break;
case 17:
printf("You chose to check if a number is a primitive root modulo n\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
printf("%lld (mod %lld) %s\n\n\n", x, module, isPrimitiveRoot(x, module) == 1 ? "is a primitive root" : "is not a primitive root");
break;
case 18:
printf("You chose to compute the gcd\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("the gcd(%lld, %lld) = %lld\n\n\n", x, y, gcd(x, y));
break;
case 19:
printf("You chose to compute the gcd using the Extended Euclid's algorithm\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
module = extendedGCD(x, y, &x1, &y1);
printf("The gcd(%lld, %lld) = %lld --> %lld * %lld + %lld * %lld = %lld\n\n\n", x, y, module, x, x1, y, y1, module);
break;
case 20:
printf("You chose to compute the module of 2 numbers\n\n"
"Insert the first number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the second number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("%lld (mod %lld) = %lld\n\n\n", x, y, mod(x, y));
break;
case 21:
printf("You chose to compute a number congruent with the one given\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the modulo value: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("A number congruent with %lld is %lld\n\n\n", x, congruentNumber(x, y));
break;
case 22:
printf("You chose to compute the modular reduction of a number\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the modulo value: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("%lld = %lld (mod %lld)\n\n\n", x, modularReduction(x, y), y);
break;
case 23:
printf("You chose to compute the modular inversion of a number\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the modulo value: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("1/%lld = %lld (mod %lld)\n\n\n", x, modularInverse(x, y), y);
break;
case 24:
printf("You chose to factorize an odd number\n\n"
"Insert the odd number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
list = FermatFactorisation(x, &y);
printf("The factors of %lld %s: ", x, y == 1 ? "is" : "are");
for (long long int i = 0; i < y; ++i) {
printf("%lld ", list[i]);
}
printf("\n\n\n");
free(list);
break;
case 25:
printf("You chose to factorize a number\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
list = factorisation(x, &y);
printf("The factors of %lld %s: ", x, y == 1 ? "is" : "are");
for (long long int i = 0; i < y; ++i) {
printf("%lld ", list[i]);
}
free(list);
printf("\n\n\n");
break;
case 26:
printf("You chose to compute the Euler function\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Euler_function(%lld) = %lld\n\n\n", x, EulerFunction(x));
break;
case 27:
printf("You chose to compute the list of prime number up to the n-th\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
list = primeNumberList(x, &y);
printf("The first %lld prime number %s: ", x, x == 1 ? "is" : "are");
for (long long int i = 0; i < y; ++i) {
printf("%lld ", list[i]);
}
printf("\n\n\n");
free(list);
break;
case 28:
printf("You chose to compute the n-th prime number\n\n"
"Inset the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("The %lld-%s prime number is %lld\n\n\n", x,
(x == 1 ? "st" : (x == 2 ? "nd" : (x == 3 ? "rd" : "th"))), nthPrimeNumber(x));
break;
case 29:
printf("You chose to compute the next prime number from the one given\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("The next prime number from %lld is %lld\n\n\n", x, nextPrimeNumber(x));
break;
case 30:
printf("You chose to compute the list of primitive roots modulo n\n\n"
"Insert the modulo value: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
list = primitiveRoots(x, &y);
printf("The primitive roots modulo %lld %s: ", x, x == 1 ? "is" : "are");
for (long long int i = 0; i < y; ++i) {
printf("%lld ", list[i]);
}
printf("\n\n\n");
free(list);
break;
case 31:
printf("You chose to compute the list of quadratic residual modulo n\n\n"
"Insert the modulo value: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
list = quadraticResiduals(x, &y);
printf("The quadratic residual modulo %lld %s: ", x, x == 1 ? "is" : "are");
for (long long int i = 0; i < y; ++i) {
printf("%lld ", list[i]);
}
printf("\n\n\n");
free(list);
break;
case 32:
printf("You chose to compute the Legendre symbol\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the odd prime number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("The Legendre_symbol(%lld, %lld) = %lld\n\n\n", x, y, LegendreSymbol(x, y));
break;
case 33:
printf("You chose to compute the Jacobi symbol\n\n"
"Insert the number: ");
returnScanf = scanf("%lld", &x);
assert(returnScanf > 0);
printf("Insert the odd number: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("The Jacobi_symbol(%lld, %lld) = %lld\n\n\n", x, y, JacobiSymbol(x, y));
break;
case 34:
printf("You chose to check if a matrix has all integer elements\n\n"
"Insert the matrix to check: \n");
matrix1 = generateMatrixNxM(&x, &y);
assert(matrix1 != NULL);
printf("The matrix:\n");
printMatrix(matrix1);
printf("The matrix insert %s all integer elements", isIntegerMatrix(matrix1) == 1 ? "has" : "has NOT");
deleteMatrix(matrix1);
printf("\n\n\n");
break;
case 35:
printf("You chose to compute the matrix modulo n\n\n"
"Insert the matrix to compute the module: \n");
matrix1 = generateMatrixNxM(&x, &y);
assert(matrix1 != NULL);
printf("Insert the modulo value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
res = createMatrix(matrix1->n, matrix1->m);
assert(res != NULL);
modularMatrix(matrix1, res, module);
printf("The matrix:\n");
printMatrix(matrix1);
printf("The matrix modulo %lld:\n", module);
printMatrix(res);
deleteMatrix(matrix1);
deleteMatrix(res);
printf("\n\n\n");
break;
case 36:
printf("You chose to compute the inversion modulo n\n\n"
"Insert the matrix to invert: \n");
matrix1 = generateMatrixNxN(&x);
assert(matrix1 != NULL);
printf("Insert the modulo value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
res = createMatrix(x, x);
assert(res != NULL);
res = createMatrix(matrix1->n, matrix1->m);
assert(res != NULL);
inverseMatrixModulo(matrix1, res, module);
printf("The matrix:\n");
printMatrix(matrix1);
printf("The matrix inverse modulo %lld:\n", module);
printMatrix(res);
deleteMatrix(matrix1);
deleteMatrix(res);
printf("\n\n\n");
break;
case 37:
printf("You chose to compute the sum of 2 matrices modulo n\n\n"
"Insert the first matrix: \n");
matrix1 = generateMatrixNxM(&x, &y);
assert(matrix1 != NULL);
printf("Insert the second matrix: \n");
matrix2 = generateMatrixNxM(&x1, &y1);
assert(matrix2 != NULL);
assert(x == x1);
assert(y == y1);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
res = createMatrix(x, y);
assert(res != NULL);
sumMatrixModulo(matrix1, matrix2, res, module);
printMatrix(matrix1);
printf("+\n");
printMatrix(matrix2);
printf("(mod %lld)\n"
"=\n", module);
printMatrix(res);
deleteMatrix(matrix1);
deleteMatrix(matrix2);
deleteMatrix(res);
printf("\n\n\n");
break;
case 38:
printf("You chose to compute the difference of 2 matrices modulo n\n\n"
"Insert the first matrix: \n");
matrix1 = generateMatrixNxM(&x, &y);
assert(matrix1 != NULL);
printf("Insert the second matrix: \n");
matrix2 = generateMatrixNxM(&x1, &y1);
assert(matrix2 != NULL);
assert(x == x1);
assert(y == y1);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
res = createMatrix(x, y);
assert(res != NULL);
subMatrixModulo(matrix1, matrix2, res, module);
printMatrix(matrix1);
printf("-\n");
printMatrix(matrix2);
printf("(mod %lld) =\n", module);
printMatrix(res);
deleteMatrix(matrix1);
deleteMatrix(matrix2);
deleteMatrix(res);
printf("\n\n\n");
break;
case 39:
printf("You chose to compute the scalar product modulo n\n\n"
"Insert the K value: ");
returnScanf = scanf("%lld", &x1);
assert(returnScanf > 0);
printf("Insert the matrix: \n");
matrix1 = generateMatrixNxM(&x, &y);
assert(matrix1 != NULL);
printf("Insert the modulo value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
res = createMatrix(x, y);
assert(res != NULL);
scalarProductModulo(x1, matrix1, res, module);
printf("%lld\n"
"*\n", x1);
printMatrix(matrix1);
printf("(mod %lld) =\n", module);
printMatrix(res);
deleteMatrix(matrix1);
deleteMatrix(res);
printf("\n\n\n");
break;
case 40:
printf("You chose to compute the product of 2 matrices modulo n\n\n"
"Insert the first matrix: \n");
matrix1 = generateMatrixNxM(&x, &y);
assert(matrix1 != NULL);
printf("Insert the second matrix: \n");
matrix2 = generateMatrixNxM(&x1, &y1);
assert(matrix2 != NULL);
assert(y == x1);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
res = createMatrix(x, y1);
assert(res != NULL);
productMatrixModulo(matrix1, matrix2, res, module);
printMatrix(matrix1);
printf("*\n");
printMatrix(matrix2);
printf("(mod %lld)\n"
"=\n", module);
printMatrix(res);
deleteMatrix(matrix1);
deleteMatrix(matrix2);
deleteMatrix(res);
printf("\n\n\n");
break;
case 41:
printf("You chose to compute the power elevation modulo n\n\n"
"Insert the matrix: \n");
matrix1 = generateMatrixNxN(&x);
assert(matrix1 != NULL);
printf("Insert the exponent: ");
returnScanf = scanf("%lld", &y);
assert(returnScanf > 0);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
res = createMatrix(x, x);
assert(res != NULL);
printf("res\n");
printMatrix(res);
powerMatrixModulo(matrix1, y, res, module);
printMatrix(matrix1);
printf("^%lld\n"
"(mod %lld)\n"
"=\n", y, module);
printMatrix(res);
deleteMatrix(matrix1);
deleteMatrix(res);
printf("\n\n\n");
break;
case 42:
printf("You chose to compute the Kronecker product of 2 matrices modulo n\n\n"
"Insert the first matrix: \n");
matrix1 = generateMatrixNxM(&x, &y);
assert(matrix1 != NULL);
printf("Insert the second matrix: \n");
matrix2 = generateMatrixNxM(&x1, &y1);
assert(matrix2 != NULL);
printf("Insert the module value: ");
returnScanf = scanf("%lld", &module);
assert(returnScanf > 0);
res = createMatrix(x * x1, y * y1);
kroneckerProductMatrixModulo(matrix1, matrix2, res, module);
printMatrix(matrix1);
printf("(*)\n");
printMatrix(matrix2);
printf("(mod %lld)\n"
"=\n", module);
printMatrix(res);
deleteMatrix(matrix1);
deleteMatrix(matrix2);
deleteMatrix(res);
printf("\n\n\n");
break;
default:
printf("Exit\n");
*choice = 43;
printf("\n\n\n");
break;
}
}
/**
* Program Main.
*
* @return 0 if the program finished execution successfully.
*/
int main() {
//The user choice.
int choice = -1;
//Return scanf value.
int returScanf = 0;
do {
//print all the possible choice
printChoice();
//read the choice insert from the user
printf("Insert your choice: ");
returScanf = scanf("%d", &choice);
assert(returScanf > 0);
//manage the choice
manageChoice(&choice);
sleep(5);
} while (choice != 43);
return 0;
}