factorial

How to find factorial without using Recursion or loop in java?

删除回忆录丶 提交于 2019-12-24 01:14:46
问题 I need to find the factorial in java without using loop or recursion ? So if there is any way then please help . Thanks 回答1: Slightly impractical but no explicit loop anywhere. import javax.swing.Timer; import java.awt.event.*; import java.util.concurrent.ArrayBlockingQueue; public class Fac { public static int fac(final int _n) { final ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1); final Timer timer = new Timer(0, null); timer.addActionListener(new ActionListener() {

In SQL, How can I generate every possible unique combination of 5!56?

删除回忆录丶 提交于 2019-12-23 13:34:11
问题 I have a TABLE "elements" with one COLUMN "number", type SMALLINT that contains numbers 1 thru 56. How can I generate unique sets of 5 numbers of every possible combination from 1 to 56, using an SQL statement? In APL (programming language) a simple dyadic function 5!56 does the trick! EDIT: In good ole MS-DOS QBASIC, I accomplished it like this: 10 OPEN "C:\5NUMBERS.OUT" FOR OUTPUT ACCESS READ WRITE AS #1 12 LET SER = 0 15 LET E = 56 30 FOR B5 = 5 TO E 40 FOR B4 = 4 TO E 50 FOR B3 = 3 TO E

Dynamic Java integer/long overflow checking versus performance

拟墨画扇 提交于 2019-12-23 12:57:32
问题 This is a rather theoretical question, so while the language is specifically Java, any general solution will suffice. Suppose I wanted to write a trivial factorial function: long factorial(int n) { //handle special cases like negatives, etc. long p = 1; for(int i = 1; i <= n; i++) { p = p * n; } return p; } But now, I also want to check if the factorial overflows (without simply hard coding a MAX_FACTORIAL_PARAMETER or something of the like). In general checking for overflow during

Calculating the factorial without trailing zeros efficiently?

大憨熊 提交于 2019-12-23 12:54:00
问题 I'm trying to improve the running time of the factorial calculation of the large number. The first Code which simply loop over and multiplies. def calculate_factorial_multi(number): ''' This function takes one agruments and returns the factorials of that number This function uses the approach successive multiplication like 8! = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 ''' ''' If 0 or 1 retrun immediately ''' if number == 1 or number == 0: return 1 result = 1 # variable to hold the result for x in xrange

Finding the factorial using recursion with the BigInteger Class

好久不见. 提交于 2019-12-23 12:16:43
问题 So consider the following program-segment! I've tried to use the basic recursion function to determine the factorial of a number, but now using the BigInteger class. public static BigInteger fact(int a) { BigInteger factorial = BigInteger.ONE; BigInteger factz = BigInteger.ONE; if(a == 1) { return factorial; } else { return factz.multiply(fact(a-1)); } } So when I try implementing this in a program, it returns the output as 1. Is it because BigInteger objects are immutable? Or am I missing

How does memoizing a recursive factorial function make it more efficient?

老子叫甜甜 提交于 2019-12-22 08:37:42
问题 var lookup = {}; function memoized(n) { if(n <= 1) { return 1; } if(lookup[n]) { return lookup[n]; } lookup[n] = n * memoized(n - 1); return lookup[n]; } vs. function fact(n) { if(n <= 1) { return 1; } return n * fact(n-1); } If we call fact(3) With the second method we get --> 3 * (2 * (1)) What is the efficiency gain of storing the result in a hash. Is it only for subsequent calls to the same function? I can't see how you would gain anything if you are only calling the function once. With

How to check whether a no is factorial or not?

旧时模样 提交于 2019-12-22 08:17:14
问题 I have a problem, then given some input number n, we have to check whether the no is factorial of some other no or not. INPUT 24, OUTPUT true INPUT 25, OUTPUT false I have written the following program for it:- int factorial(int num1) { if(num1 > 1) { return num1* factorial(num1-1) ; } else { return 1 ; } } int is_factorial(int num2) { int fact = 0 ; int i = 0 ; while(fact < num2) { fact = factorial(i) ; i++ ; } if(fact == num2) { return 0 ; } else { return -1; } } Both these functions, seem

Recursive function calculating factorials leads to stack overflow

▼魔方 西西 提交于 2019-12-22 03:50:31
问题 I tried a recursive factorial algorithm in Rust. I use this version of the compiler: rustc 1.12.0 (3191fbae9 2016-09-23) cargo 0.13.0-nightly (109cb7c 2016-08-19) Code: extern crate num_bigint; extern crate num_traits; use num_bigint::{BigUint, ToBigUint}; use num_traits::One; fn factorial(num: u64) -> BigUint { let current: BigUint = num.to_biguint().unwrap(); if num <= 1 { return One::one(); } return current * factorial(num - 1); } fn main() { let num: u64 = 100000; println!("Factorial {}!

Efficient Matlab implementation of Multinomial Coefficient

风流意气都作罢 提交于 2019-12-21 06:16:40
问题 I want to calculate the multinomial coefficient: where it is satisifed n=n0+n1+n2 The Matlab implementation of this operator can be easily done in the function: function N = nchooseks(k1,k2,k3) N = factorial(k1+k2+k3)/(factorial(k1)*factorial(k2)*factorial(k3)); end However, when the index is larger than 170, the factorial would be infinite which would generate NaN in some cases, e.g. 180!/(175! 3! 2!) -> Inf/Inf-> NaN . In other posts, they have solved this overflow issue for C and Python.

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

你离开我真会死。 提交于 2019-12-21 03:35:10
问题 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? 回答1: The fact that your Scala code creates 50,000 BigInt objects is unlikely to be making much of a