factorial

Calculate the sum of digits in 100 factorial

為{幸葍}努か 提交于 2019-12-01 23:42:14
问题 Edit - Changed title to match the actual problem statement. I'm programming a function that calculates the sum of digits in 100! but I seem to be having two big issues. The actual result of 100! is only accurate to the first few numbers (actual result is 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000) My method for adding up the digits of the resulting number doesn't output the

Is there a package or technique availabe for calculating large factorials in R?

吃可爱长大的小学妹 提交于 2019-12-01 21:11:57
If I calculate factorial(100) then I get an answer of [1] 9.332622e+157 but when I try to calculate a larger factorial, say factorial(1000) I get an answer of [1] Inf Is there a way to use arbitrary precision when calculating factorials such that I can calculate say factorial(1000000) ? For arbitrary precision you can use either gmp or Rmpfr . For specifically factorial gmp offers factorialZ and Rmpfr has factorialMpfr . So you can run something like below > Rmpfr::factorialMpfr(200) 1 'mpfr' number of precision 1246 bits [1]

Recursive factorial method returning some negative numbers

社会主义新天地 提交于 2019-12-01 17:50:16
This is my factorial method: public static long factorial(int num1) { if (num1 <= 1) return 1; else return num1 * factorial(num1 - 1); } And this is what calls this recursive factorial method: for (i = 0; i <= 25; i++) System.out.printf ("%d != %,d\n", i, factorial (i)); So far so good, the output seems correct at first but some factorials are negative instead of positive: OUTPUT: 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,000 16 != 20

Why vector in C++ doesn't resize automatically

↘锁芯ラ 提交于 2019-12-01 14:14:12
I've a very long factorial program which needs to find factorial up to 100. It works well up to 33 factorial but not from 34. Can someone help in identifying the problem. #include <iostream> #include <vector> #include <utility> using namespace std; void bigFactorials(int n) { vector<int> v;//If I specify the size as v(1000) it works fine but I don't //want to specify the size beforehand. v.push_back(1); // Complete this function for(int i=2;i<=n;i++) { int carry = 0, mul=0; for(auto j=v.rbegin();j!=v.rend();j++) { mul=i**j + carry; carry=mul/10; *j=mul%10; } if(carry) v.insert(v.begin(),carry)

Calculate Nth multiset combination (with repetition) based only on index

断了今生、忘了曾经 提交于 2019-12-01 10:59:09
How can i calculate the Nth combo based only on it's index. There should be (n+k-1)!/(k!(n-1)!) combinations with repetitions. with n=2, k=5 you get: 0|{0,0,0,0,0} 1|{0,0,0,0,1} 2|{0,0,0,1,1} 3|{0,0,1,1,1} 4|{0,1,1,1,1} 5|{1,1,1,1,1} So black_magic_function(3) should produce {0,0,1,1,1}. This will be going into a GPU shader, so i want each work-group/thread to be able to figure out their subset of permutations without having to store the sequence globally. with n=3, k=5 you get: i=0, {0,0,0,0,0} i=1, {0,0,0,0,1} i=2, {0,0,0,0,2} i=3, {0,0,0,1,1} i=4, {0,0,0,1,2} i=5, {0,0,0,2,2} i=6, {0,0,1,1

Difficulty with BigInteger

北城余情 提交于 2019-12-01 09:31:19
I am trying to do Factorial with Recursion and BigIntegers but eclipse is complaining about the BigInteger. I know the program is supposed to be simple but it is giving me headache. Here is the code. import java.util.Scanner; import java.math.BigInteger; public class Factorial { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter integer"); BigInteger n = input.nextBigInteger(); System.out.println("Factorial of " + n + " is " + fact(n)); } public static int fact(BigInteger n) { if(n ==0) { return 1; } else { return n * fact(n-1); } } }

Prolog factorial recursion

梦想的初衷 提交于 2019-12-01 03:28:33
I'm having trouble understanding the following factorial program fact1(0,Result) :- Result is 1. fact1(N,Result) :- N > 0, N1 is N-1, fact1(N1,Result1), Result is Result1*N. When fact1 is called nested within the second fact1 , doesn't that mean that the the last line, Result is Result1*N. , is never called? Or in Prolog does the last line get executed before the recursive call? Carl Norum No, the recursive call happens first! It has to, or else that last clause is meaningless. The algorithm breaks down to: factorial(0) => 1 factorial(n) => factorial(n-1) * n; As you can see, you need to

maximum recursion depth exceeded in comparison

百般思念 提交于 2019-12-01 00:49:52
I wrote this piece of code to compute the number of combinations: def fact(n): return 1 if(n == 1) else n * fact(n - 1) def combinations(n,k): return fact(n)/((fact(n - k) * fact(k))) while(True): print(combinations(int(input()), int(input()))) The factorial function seems to work fine. But why does it give me a maximum recursion depth exceeded in comparison error when I try to find the combinations of two numbers? Is there something wrong with the factorial function, since that's where the error seems to be originating from? This was the error I got: builtins.RuntimeError: maximum recursion

Factorial Java Program

允我心安 提交于 2019-12-01 00:24:38
I want to do a factorial program in java using a for loop. For example I want to take the user input, lets say 10 , and then multiply 10*9*8*7*6*5*4*3*2*1 . I need help constructing the for loop. The code below is all I have so far as I'm not sure where to go after. import java.util.Scanner; import java.lang.Math; public class factorial { public static void main(String[] args) { int num; Scanner input = new Scanner(System.in); System.out.println("Enter a number: "); num = input.nextInt(); } } Try public static void main(String[] args) { int num; int fact=1; Scanner input = new Scanner(System

How can I calculate a factorial in C# using a library call?

雨燕双飞 提交于 2019-11-30 20:53:19
I need to calculate the factorial of numbers up to around 100! in order to determine if a series of coin flip-style data is random, as per this Wikipedia entry on Bayesian probability. As you can see there, the necessary formula involves 3 factorial calculations (but, interestingly, two of those factorial calculations are calculated along the way to the third). I saw this question here , but I'd think that integer is going to get blown out pretty quickly. I could also make a function that is more intelligent about the factorial calculation (ie, if I have 11!/(7!3!), as per the wiki example, I