123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- /*
- * 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 a positive integer x from the user, and compute the value of x!.
- How long does it take with naive multiplications? Can you do any
- better? Can you compute for arbitrary sized inputs?
- Acknowledgements:http://pubs.opengroup.org/onlinepubs/7908799/xsh/regcomp.html
- ------------------------------------*/
- #include <stdio.h>
- /*
- Dynamic Programming Approach
- Funtion to calculate up to N Factorials
- */
- long int factorialDP(int n)
- {
- long int a[n+1],i,j; // factorials array
- a[0]=1;
-
- for(i=1;i<=n;i++)
- {
- a[i] = i * a[i-1];
- }
-
- return a[n];
- }
- int main(void)
- {
- int n, i;
- long int factorial = 1;
- printf("\nEnter a number\n");
- scanf("%d", &n);
- if(n >= 0 && n < 21)
- {
- /*Iterative logic
- for( i=1 ; i<=n; i++)
- {
- factorial = factorial * i;
- }*/
- printf("\nFactorial of %d is %ld.\n", n, factorialDP(n));
- }
- else
- {
- printf("\nFactorial can be computed for number from 0 to 20\n");
- }
- return 0;
- }
|