Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int* fillRing(int n) {
  5. int* ring = new int[n];
  6. for(int i = 0; i < n; i++) {
  7. ring[i] = i;
  8. }
  9.  
  10. return ring;
  11. }
  12.  
  13. int add(int left, int right, int n) {
  14. if(left > n || right > n) {
  15. return -1;
  16. }
  17. return (left + right) % n;
  18. }
  19.  
  20. int subract(int left, int right, int n) {
  21. if(left > n || right > n) {
  22. return -1;
  23. }
  24.  
  25. int res = left - right;
  26. if(res < 0) {
  27. res = n + res;
  28. }
  29.  
  30. return res;
  31. }
  32.  
  33. int multiply(int left, int right, int n) {
  34. if(left > n || right > n) {
  35. return -1;
  36. }
  37.  
  38. return (left * right) % n;
  39. }
  40.  
  41. int** fillPairs(int n) {
  42. int** result = new int*[2];
  43. result[0] = new int[n];
  44. result[1] = new int[n];
  45. int c = 0;
  46.  
  47. for(int i = 1; i < n; i++) {
  48. for(int j = 1; j < n; j++) {
  49. unsigned int mult = j*i;
  50. if((mult % n) == 1) {
  51. result[0][c] = i;
  52. result[1][c] = j;
  53. c++;
  54. }
  55. }
  56. }
  57.  
  58. result[0][c] = -1; // Last element;
  59. result[1][c] = -1; // Last element;
  60.  
  61. return result;
  62. }
  63. int main() {
  64. int n = 7;
  65. int* ring = fillRing(n);
  66. cout << add(0, 4, n) << endl;
  67. cout << add(3, 4, n) << endl;
  68. cout << add(2, 6, n) << endl;
  69. cout << add(4, 5, n) << endl;
  70. cout << subract(2, 5, n) << endl;
  71. cout << subract(1, 3, n) << endl;
  72. cout << subract(4, 5, n) << endl;
  73. cout << multiply(2, 6, n) << endl;
  74. cout << multiply(4, 4, n) << endl;
  75. cout << multiply(5, 5, n) << endl;
  76.  
  77. int **pairs = fillPairs(n);
  78.  
  79. int i = 0;
  80. while(pairs[0][i] != -1) {
  81. cout << pairs[0][i] << " * " << pairs[1][i] << " % " << n << " = 1" << endl;
  82. i++;
  83. }
  84.  
  85.  
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement