factorial

“Recursive on All Control Paths” error when implementing factorial function

我的未来我决定 提交于 2019-12-04 10:25:16
For class I have an assignment: Write a C++ program that will output the number of distinct ways in which you can pick k objects out of a set of n objects (both n and k should be positive integers). This number is given by the following formula: C(n, k) = n!/(k! * (n - k)!) Your program should use two value-returning functions. The first one should be called factorial and should return n! . The second function should be called combinations and should return n!/(k! * (n - k)!). Test your program for different values of n and k five times (count-controlled loop). I came up with a solution:

C++ algorithm for N! orderings

假如想象 提交于 2019-12-04 07:57:20
I have a list of N items and I am wondering how I can loop through the list to get every combination. There are no doubles, so I need to get all N! orderings. Extra memory is no problem, I'm trying to think of the simplest algorithm but I'm having trouble. See std::next_permutation Expanding on others' answers, here's an example of std::next_permutation adapted from cplusplus.com #include <iostream> #include <algorithm> using namespace std; void outputArray(int* array, int size) { for (int i = 0; i < size; ++i) { cout << array[i] << " "; } } int main () { int myints[] = { 1, 2, 3, 4, 5 };

Factorial java return 0

泄露秘密 提交于 2019-12-04 05:30:26
问题 I made a simple function to calculate the factorial of a number but from number 34 returns 0 . It should be from number 51 . public class Métodos { public int factorial (int numero ){ if ((numero <0)||(numero>50)){ return 0; } else if ((numero == 0)||(numero == 1)){ return 1; } else{ return numero * factorial(numero -1); } } } Thanks ! Edit: Ok,how can i check it ? because it says int cannot be converted to bigInteger. public static void main(String[] args) { // TODO code application logic

Prolog factorial recursion

寵の児 提交于 2019-12-04 00:11:06
问题 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? 回答1: No, the recursive call happens first! It has to, or else that last clause is meaningless. The algorithm breaks

Parallel calculation of BigInteger factorial

不羁的心 提交于 2019-12-03 17:26:21
As a part of my BigDecimal library, I need to calculate the factorial of any given non negative integer. So I'm using .Net 4.0 's System.Numerics.BigInteger to be able to store huge numbers. Here is the function I'm using: private BigInteger Factorial(BigInteger x) { BigInteger res = x; x--; while (x > 1) { res *= x; x--; } return res; } It's working but not optimized. Now I want to use parallel computing, so here is what I've tried: (I have no experience with parallel programming) public BigInteger Factorial(long x) { BigInteger res = 1; ParallelLoopResult r = Parallel.For(2L, (x + 1), i =>

Sum of factorials for large numbers

牧云@^-^@ 提交于 2019-12-03 17:20:03
I want to calculate the sum of digits of N!. I want to do this for really large values of N, say N(1500). I am not using .NET 4.0. I cannot use the BigInteger class to solve this. Can this be solved by some other algorithm or procedure? Please help. I want to do some thing like this Calculate the factorial of an arbitrarily large number, showing all the digits but in C#. However I am unable to solve. There is no special magic that allows you to calculate the sum of the digits, as far as I am concerned. It shouldn't be that hard to create your own BigInteger class anyway - you only need to

Tail recursion with Groovy

谁说我不能喝 提交于 2019-12-03 16:05:30
I coded 3 factorial algorithms: First, I expect to fail by Stack Overflow. No problem. Second, I try tail recusive call , convert previous algorithm from recursive to iterative. It doesn't work but I don't understand why . Third, I use trampoline() method and works fine as I expect. def factorial factorial = { BigInteger n -> if (n == 1) return 1 n * factorial(n - 1) } factorial(1000) // Stack Overflow factorial = { Integer n, BigInteger acc = 1 -> if (n == 1) return acc factorial(n - 1, n * acc) } factorial(1000) // Stack Overflow, why??? factorial = { Integer n, BigInteger acc = 1 -> if (n =

Why is factorial calculation much faster in Haskell than in Java

旧街凉风 提交于 2019-12-03 14:59:19
问题 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

How to make a function that computes the factorial for numbers with decimals?

帅比萌擦擦* 提交于 2019-12-03 12:40:58
How can I make a function that calculates the factorial (or the gamma function ) of decimal numbers in JavaScript? For example, how could I calculate 2.33! ? I might have found an existing solution... It's an implementation of Lanczos method, I found it at the swedish wikipedia ( http://sv.wikipedia.org/wiki/Gammafunktionen ). It was written in python and says to be correct up to 15 decimals. I ported it to js, cross checked some random values against ( http://www.efunda.com/math/gamma/findgamma.cfm ). http://jsfiddle.net/Fzy9C/ var g = 7; var C = [0.99999999999980993, 676.5203681218851, -1259

How to optimize this short factorial function in scala? (Creating 50000 BigInts)

时光怂恿深爱的人放手 提交于 2019-12-03 11:41:14
I've compaired the scala version (BigInt(1) to BigInt(50000)).reduce(_ * _) to the python version reduce(lambda x,y: x*y, range(1,50000)) and it turns out, that the scala version took about 10 times longer than the python version. I'm guessing, a big difference is that python can use its native long type instead of creating new BigInt-objects for each number. But is there a workaround in scala? Travis Brown The fact that your Scala code creates 50,000 BigInt objects is unlikely to be making much of a difference here. A bigger issue is the multiplication algorithm— Python's long uses Karatsuba