factorial

Why is factorial calculation much faster in Haskell than in Java

主宰稳场 提交于 2019-12-03 04:45:26
One of the programming problems I have come across involves calculation of factorials of large numbers (of numbers up to 10^5). I have seen a simple Haskell code which goes like this factorial :: (Eq x, Num x) => x -> x factorial 0 = 1 factorial a = a * factorial (a - 1) which implicitly handles the huge numbers and somehow runs faster even without any caching that is involved in the code. When I tried to solve the problem using Java, I had to use BigInteger to hold the huge numbers and also use iterative version of factorial public static BigInteger factorialIterative(int n) { if(n == 0 || n

recursive factorial function

拜拜、爱过 提交于 2019-12-03 02:54:20
how can I combine these two functions in to one recursive function to have this result: factorial(6) 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 these are the codes def factorial( n ): if n <1: # base case return 1 else: return n * factorial( n - 1 ) # recursive call def fact(n): for i in range(1, n+1 ): print "%2d! = %d" % ( i, factorial( i ) ) fact(6) 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 as you see execution of these two gives a correct answer, I just want to make it to one recursive function. def factorial( n ): if n <1: # base case return 1 else: returnNumber = n * factorial(

Creating Python Factorial

99封情书 提交于 2019-12-02 17:19:41
问题 Evening, I'm an intro to python student having some trouble. I'm trying to make a python factorial program. It should prompt the user for n and then calculate the factorial of n UNLESS the user enters -1. I'm so stuck, and the prof suggested we use the while loop. I know I didn't even get to the 'if -1' case yet. Don't know how to get python to calc a factorial with out just blatantly using the math.factorial function. import math num = 1 n = int(input("Enter n: ")) while n >= 1: num *= n

print factorial calculation process in java

自古美人都是妖i 提交于 2019-12-02 17:12:58
问题 Hi I need to print process of factorial calculation. E.g. If the user's input is 5, the system should print out "5 * 4 * 3 * 2 * 1 = 120" I have this code: public static void factorial() { Scanner sc = new Scanner(System.in); int factorial = 1; int count; System.out.println(me+", This is option 2: Calculate a factorial"); System.out.print("Enter your number: "); int number = sc.nextInt(); System.out.println(); if (number>0) { for (count=1; count<=number; count++) factorial = factorial*count;

Which function grows faster, exponential or factorial?

橙三吉。 提交于 2019-12-02 16:31:16
Which function grows faster, exponential (like 2^n, n^n, e^n etc) or factorial (n!)? Ps: I just read somewhere, n! grows faster than 2^n. n! eventually grows faster than an exponential with a constant base (2^n and e^n), but n^n grows faster than n! since the base grows as n increases. n! = n * (n-1) * (n-2) * ... n^n = n * n * n * ... Every term after the first one in n^n is larger, so n^n will grow faster. n^n grows larger than n! -- for an excellent explanation, see the answer by @AlexQueue. For the other cases, read on: Factorial functions do asymptotically grow larger than exponential

Recursion Factorial terminating due to StackOverflowException

穿精又带淫゛_ 提交于 2019-12-02 14:52:21
I am trying to learn recursion and I am attempting to do factorial's via recursion instead of loops, but my program is causing "Process is terminated due to StackOverflowException class RecursionFactorial { public static int n; static void Main(string[] args) { string input; Console.WriteLine("Please enter a number to work out the factorial"); input = Console.ReadLine(); bool test = int.TryParse(input, out n); fact(n); } public static int fact(int y) { int count = n; if (y <= 1) { Console.WriteLine(y); } else { count = (count * y); Console.WriteLine(count); fact(y - 1); } } } fixed with this

print factorial calculation process in java

我的梦境 提交于 2019-12-02 13:33:21
Hi I need to print process of factorial calculation. E.g. If the user's input is 5, the system should print out "5 * 4 * 3 * 2 * 1 = 120" I have this code: public static void factorial() { Scanner sc = new Scanner(System.in); int factorial = 1; int count; System.out.println(me+", This is option 2: Calculate a factorial"); System.out.print("Enter your number: "); int number = sc.nextInt(); System.out.println(); if (number>0) { for (count=1; count<=number; count++) factorial = factorial*count; System.out.println(" = "+factorial); System.out.println(); } else { System.out.println("Enter a

Factorial Code works but why

Deadly 提交于 2019-12-02 12:59:20
I'm solving a factorial problem where the function takes a number and returns the factorial of that number. The problem I'm running into is that the code works but I don't know why. There are no loops to call it back after the code is executed and I'm not even sure where the current value is being stored.If I am correct the I assume the function is re-running every time it hit the return and it is running with a value of n-1 so one number less than the previous time it ran, however, I still do not see how the value is getting stored to multiple each number by the current value. Even if I log

Math behind “compute n! under modulo p”?

拥有回忆 提交于 2019-12-02 09:23:08
long long x; for (int i = 1; i <= n; i++) { x = (x * i) % m; } cout << x; This is the trick to calculate (n!) mod m (assume m > n). However, I don't know why it's true. Can you explain the math mechanism behind this? The basic idea here is that you can take the modulus before, during, or after multiplication and get the same value after taking the modulus of the final result. As @Peter points out, (a * b) % m == ((a % m) * (b % m)) % m For the factorial, n! = 1 * 2 * 3 * ... * (n-1) * n so we have n! % m = (((((((1 * 2) % m) * 3) % m) * ... * n-1) % m) * n) % m taking the modulus after each

Factorial using Addition

允我心安 提交于 2019-12-02 08:43:53
I am attempting to create a C code that finds the factorial of a integer so that I may convert my code to assembly language. My code seems to 'multiply' the second integer twice. i.e. 5*4*4*3... I cannot seem to find out why. Help please! #define N 5 int main() { int j = 0; int i = 0; int num1 = N; int num2 = N - 1; int sum = 0; while (num2 != 0) { while (j < num2) { sum += num1; j++; } j = 0; printf("%d\n", sum); printf("--------------\n"); --num2; num1 = sum; } printf("--->%d", sum); } Erroneous Output: 20 -------------- 80 -------------- 240 -------------- 480 -------------- 480 Here's the