cs1713-day1-prog6.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (C) 2020, 2019, 2018, 2017 Girish M
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. *
  18. */
  19. /*-----------------------------------
  20. Name: Girish M
  21. Roll number: cs1713
  22. Date: 25 July 2017
  23. Program description: Take three positive integers x,n, and m from the user and compute (x power n) mod m.
  24. Acknowledgements:http://pubs.opengroup.org/onlinepubs/7908799/xsh/regcomp.html
  25. ------------------------------------*/
  26. #include <stdio.h>
  27. #include <regex.h>
  28. #include <stdlib.h>
  29. int match(const char *string, char *pattern)
  30. {
  31. int status;
  32. regex_t re;
  33. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
  34. return(0); /* report error */
  35. }
  36. status = regexec(&re, string, (size_t) 0, NULL, 0);
  37. regfree(&re);
  38. if (status != 0) {
  39. return(0); /* report error */
  40. }
  41. return(1);
  42. }
  43. /* Function to calculate x raised to the power y in O(logn)*/
  44. long int power(long int x, long int y)
  45. {
  46. long int temp;
  47. if( y == 0)
  48. return 1;
  49. temp = power(x, y/2);
  50. if (y%2 == 0)
  51. return temp*temp;
  52. else
  53. {
  54. if(y > 0)
  55. return x*temp*temp;
  56. else
  57. return (temp*temp)/x;
  58. }
  59. }
  60. int main(int argc, char* argv[])
  61. {
  62. int x, n, m;
  63. long int prod = 1;
  64. char* pattern = "^[+-]?[0-9]+$";
  65. if(argc == 4)
  66. {
  67. if(match(argv[1], pattern) && match(argv[2], pattern) && match(argv[3], pattern))
  68. {
  69. x = atoi(argv[1]);
  70. n = atoi(argv[2]);
  71. m = atoi(argv[3]);
  72. if(n >= 0 && m > 0)
  73. {
  74. /* Iterative logic
  75. while(n != 0)
  76. {
  77. prod = prod * x;
  78. n--;
  79. }*/
  80. printf("\n (%s to the power of %s) mod %s is: %ld\n", argv[1], argv[2], argv[3], power(x,n)%m);
  81. }
  82. else
  83. printf("\nUsage: ./cs1713-day1-prog6.o int1 positiveInt1 positiveInt2\n");
  84. }
  85. else
  86. {
  87. printf("\nUsage: ./cs1713-day1-prog6.o int1 positiveInt1 positiveInt2\n");
  88. }
  89. }
  90. else
  91. {
  92. printf("\nUsage: ./cs1713-day1-prog6.o int1 positiveInt1 positiveInt2\n");
  93. }
  94. return 0;
  95. }