factorial

Recursion Factorial terminating due to StackOverflowException

◇◆丶佛笑我妖孽 提交于 2019-12-20 08:06:15
问题 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

Factorial Code works but why

别来无恙 提交于 2019-12-20 07:48:01
问题 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

Factorial using Addition

僤鯓⒐⒋嵵緔 提交于 2019-12-20 05:53:35
问题 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); }

Calculating factorials with numbers bigger than ints and longs in java?

孤街浪徒 提交于 2019-12-20 02:31:33
问题 Been searching here and google for a couple of days as well as asking my programming friends. Unfortunately, i still don't understand how to change my code... My program calculates the factorial of a given number. It then provides a number which represents how many digits the factorials answer includes. Then it sums the values of those digits together to give a total. My program works for any number between 1! and 31!... If you put in anything over 31! (for example 50! or 100!) it doesn't

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

谁说我不能喝 提交于 2019-12-19 10:19:35
问题 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

Factorial Java Program

对着背影说爱祢 提交于 2019-12-19 04:12:54
问题 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(); } }

factorial of big numbers with strings in c++

…衆ロ難τιáo~ 提交于 2019-12-19 03:58:27
问题 I am doing a factorial program with strings because i need the factorial of Numbers greater than 250 I intent with: string factorial(int n){ string fact="1"; for(int i=2; i<=n; i++){ b=atoi(fact)*n; } } But the problem is that atoi not works. How can i convert my string in a integer. And The most important Do I want to know if the program of this way will work with the factorial of 400 for example? 回答1: There's a web site that will calculate factorials for you: http://www.nitrxgen.net

Prime factorization of a factorial

拟墨画扇 提交于 2019-12-19 03:12:40
问题 I need to write a program to input a number and output its factorial's prime factorization in the form: 4!=(2^3)*(3^1) 5!=(2^3)*(3^1)*(5^1) The problem is I still can't figure out how to get that result. Apparently each first number in brackets is for the ascending prime numbers up until the actual factorial. The second number in brackets is the amount of times the number occurs in the factorial. What I can't figure out is for example in 5!=(2^3)*(3^1)*(5^1) , how does 2 only occur 3 times, 3

Complexity of factorial recursive algorithm

白昼怎懂夜的黑 提交于 2019-12-18 11:06:23
问题 Today in class my teacher wrote on the blackboard this recursive factorial algorithm: int factorial(int n) { if (n == 1) return 1; else return n * factorial(n-1); } She said that it has a cost of T(n-1) + 1 . Then with iteration method she said that T(n-1) = T(n-2) + 2 = T(n-3) + 3 ... T(n-j) + j , so the algorithm stops when n - j = 1 , so j = n - 1 . After that, she substituted j in T(n-j) + j , and obtained T(1) + n-1 . She directly said that for that n-1 = 2 (log 2 n-1) , so the cost of

Find ith permutation in javascript

本秂侑毒 提交于 2019-12-17 18:37:26
问题 Given an array arr of size n , and and index 0<=i<n! I want to return the i'th permutation. I was able to write a method that gets all permutations: function permute (arr) { var permutations = []; if (arr.length === 1) { return [ arr ]; } for (var i = 0; i < arr.length; i++) { var subPerms = permute(arr.slice(0, i).concat(arr.slice(i + 1))); for (var j = 0; j < subPerms.length; j++) { subPerms[j].unshift(arr[i]); permutations.push(subPerms[j]); } } return permutations; } How do I trim it to