NUMBER.CPP 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. ** Method to raise a number by an arbitrary whole exponent and then
  20. ** modulo the result by another number.
  21. */
  22. unsigned long Power_Mod(unsigned long root, unsigned long exponent, unsigned long mod)
  23. {
  24. unsigned long s = 1;
  25. unsigned long t = root;
  26. unsigned long u = exponent;
  27. while (u) {
  28. if (u & 1) {
  29. s = (s*t) % mod;
  30. }
  31. u >>= 1;
  32. t = (t*t) % mod;
  33. }
  34. return(s);
  35. }
  36. /*
  37. ** This routine finds the greatest common divisor common
  38. ** to two specified numbers.
  39. */
  40. int Greatest_Common_Divisor(int x, int y)
  41. {
  42. if (x < 0) {
  43. x = -x;
  44. }
  45. if (y < 0) {
  46. y = -y;
  47. }
  48. if (x + y == 0) {
  49. return(0); // This is an error condition.
  50. }
  51. int greatest = y;
  52. while (x > 0) {
  53. greatest = x;
  54. x = y % x;
  55. y = greatest;
  56. }
  57. return(g);
  58. }
  59. /*
  60. ** Computes the greatest common divisor for a vector
  61. ** of integers.
  62. */
  63. int Grestest_Common_Divisor(int count, int * data)
  64. {
  65. if (count < 1) {
  66. return(0);
  67. }
  68. greatest = data[0];
  69. for (int i = 1; i < count; i++) {
  70. greatest = Greatest_Common_Divisor(greatest, data[i]);
  71. if (greatest == 1) {
  72. return(1);
  73. }
  74. }
  75. return(greatest);
  76. }