factorial

How to work out how many bits the result of a factorial should take up as a number?

£可爱£侵袭症+ 提交于 2019-12-12 21:19:04
问题 The factorial function could return a very large number as a result. How could I work out the size of the data which must return as a result of the factorial? Is there a function which can give me the size of the data quickly based upon the number n for which we are computing the factorial? For example, factorial (5) = 5 * 4 * 3 * 2 = 120 The number 120 will be 120 = 0b1111000 where 0b indicates this is a binary number. At least, I need 7 bits to represent the result and probability I would

Permutation of factorial in php

流过昼夜 提交于 2019-12-12 17:17:53
问题 Don't know how to explain. But maybe example below will be make you understandable what my problem is. Example : I have an array with 3 elements. $elements = array( 'A', 'B', 'C' ); The permutation will be 3 in 3. so the result are : A-B-C ; A-C-B ; B-A-C ; B-C-A ; C-A-B; C-B-A I don't want any permutation 2 in 3 or 1 in 3, just 3 in 3 as you can see in example. So if I have 4 elements in an array, the permutation is 4 in 4. and so on... (I think the number of permutations is 3! = 1*2*3 = 6

Factorial function produces wrong result for 21! and above

时光怂恿深爱的人放手 提交于 2019-12-12 12:06:54
问题 for (int i = 0; i <= 25; i++) System.out.printf("%d! = %,d\n", i, factorial(i)); The code above initializes the factorial method below: public static long factorial(int num1) { if (num1 == 0) return 1; else return Math.abs(num1 * factorial(num1 - 1)); } As so the following output is created: 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5,040 8! = 40,320 9! = 362,880 10! = 3,628,800 11! = 39,916,800 12! = 479,001,600 13! = 6,227,020,800 14! = 87,178,291,200 15! = 1,307,674,368

C++ function to calculate factorial returns negative value

扶醉桌前 提交于 2019-12-12 04:16:33
问题 I've written a C++ function to calculate factorial and used it to calculate 22 C 11 (Combination). I have declared a variable ans and set it to 0. I tried to calculate 22C11 = fact(2*n)/(fact(n)*fact(n)) where i sent n as 11. For some reason, i'm getting a negative value stored in answer. How can i fix this? long int fact(long int n) { if(n==1||n==0) return 1; long int x=1; if(n>1) x=n*fact(n-1); return x; } The following lines are included in the main function: long int ans=0; ans=ans+(fact

Factorial - Array - C++

a 夏天 提交于 2019-12-12 02:27:24
问题 The problem is to compute factorial of a number. I've debugged my code, and it works fine for any input and produces correct output for all the given test cases. But still, I'm getting Wrong Answer on SPOJ. Problem: An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100. Any insight would be helpful. Am I missing some critical test cases? My Code: #include <iostream> using namespace std; int main() { long T; cin>>T; while

finding cosine using python

橙三吉。 提交于 2019-12-12 01:06:09
问题 I must write a function that computes and returns the cosine of an angle using the first 10 terms of the following series: cosx = 1 - (x**2)/2! + (x**4)/4! - (x**6)/6!.... I can't use the factorial function, but i can use the fact that if the previous denominator was n! , the current denominator would be n!(n+1)(n+2) . I'm trying to use an accumulator loop, but i'm having a hard time with the fact that it alternates from positive to negative and also having trouble with the denominator. This

Overflow exception?

巧了我就是萌 提交于 2019-12-11 23:49:08
问题 I have the following code for finding factorials: Private Shared Function Factorial(ByVal Number As Long) As Long If Number = 0 Then Return 1 Else Return Number * Factorial(Number - 1) End If End Function It usually results in an overflow. It only works if I start with something small like 4. I have to work with starting numbers such as 30-60. Any ideas? I thought changing the value type to LONG would prevent this problem. This is VB.net just for reference. 回答1: Factorials get very large,

Having difficulty in computing factorial(n) mod m, when n gets large

▼魔方 西西 提交于 2019-12-11 18:13:42
问题 I am trying to compute factorial of large numbers mod a large prime number. arr[] stores the value of the factorials for various numbers If I calculate fact(32570) first and then print arr[1] and arr[2] , then that works. If I calculate fact(32571) first and then print arr[1] and arr[2] , then that does not work. However, calculating only fact(32571) works. I really cannot debug this code. Also, calculating fact(32572) independently does not work. But, if I calculate fact(32572) after finding

Writing a factorial-like function (Prolog)

痴心易碎 提交于 2019-12-11 11:06:36
问题 I have to write a Prolog program to compute: f(0) = 2, f(1) = 0, f(2) = 3, f(n) = 3f(n-3) + 2f(n-2) - f(n-1) for n≥3. I need to make an iterative version, as well as a recursive version. I was able to write that recursive version in SML: fun myFunc 0 = 2 | myFunc 1 = 0 | myFunc 2 = 3 | myFunc x = 3* myFunc(x-3) + 2* myFunc(x-2) - myFunc(x-1) But am having trouble transferring it to Prolog as i'm very new to the language. Also, I was never able to figure out how to do an iterative approach.

Permutation for numbers in C

不问归期 提交于 2019-12-11 08:41:19
问题 I'm trying to write a C function to list all permutations of a set of numbers, in groups of five, including repeat numbers: 15-11-49-43-5 2-30-34-6-11 So it's easy enough to write a function to grab all permutations of a number set and throw them out, but mapped to a certain group size, i'm somewhat stuck.. 回答1: void visit(int *Value, int N, int k) { static level = -1; level = level+1; Value[k] = level; if (level == N) print(Value, N); else for (int i = 0; i < N; i++) if (Value[i] == 0) visit