123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /*
- * Copyright (C) 2020, 2019, 2018, 2017 Girish M
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- */
- /*-----------------------------------
- Name: Girish M
- Roll number: cs1713
- Date: 25 July 2017
- Program description: Take three positive integers x,n, and m from the user and compute (x power n) mod m.
- Acknowledgements:http://pubs.opengroup.org/onlinepubs/7908799/xsh/regcomp.html
- ------------------------------------*/
- #include <stdio.h>
- #include <regex.h>
- #include <stdlib.h>
- int match(const char *string, char *pattern)
- {
- int status;
- regex_t re;
- if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
- return(0); /* report error */
- }
- status = regexec(&re, string, (size_t) 0, NULL, 0);
- regfree(&re);
- if (status != 0) {
- return(0); /* report error */
- }
- return(1);
- }
- /* Function to calculate x raised to the power y in O(logn)*/
- long int power(long int x, long int y)
- {
- long int temp;
- if( y == 0)
- return 1;
- temp = power(x, y/2);
- if (y%2 == 0)
- return temp*temp;
- else
- {
- if(y > 0)
- return x*temp*temp;
- else
- return (temp*temp)/x;
- }
- }
- int main(int argc, char* argv[])
- {
- int x, n, m;
- long int prod = 1;
- char* pattern = "^[+-]?[0-9]+$";
- if(argc == 4)
- {
- if(match(argv[1], pattern) && match(argv[2], pattern) && match(argv[3], pattern))
- {
- x = atoi(argv[1]);
- n = atoi(argv[2]);
- m = atoi(argv[3]);
- if(n >= 0 && m > 0)
- {
- /* Iterative logic
- while(n != 0)
- {
- prod = prod * x;
- n--;
- }*/
- printf("\n (%s to the power of %s) mod %s is: %ld\n", argv[1], argv[2], argv[3], power(x,n)%m);
- }
- else
- printf("\nUsage: ./cs1713-day1-prog6.o int1 positiveInt1 positiveInt2\n");
- }
- else
- {
- printf("\nUsage: ./cs1713-day1-prog6.o int1 positiveInt1 positiveInt2\n");
- }
- }
- else
- {
- printf("\nUsage: ./cs1713-day1-prog6.o int1 positiveInt1 positiveInt2\n");
- }
- return 0;
- }
|