标签: factorial

Java:简单递归函数只返回1

这奇怪地是我的第一个Java应用程序,我想要实现一个任意精度阶乘函数,我做了递归一个很好,但我的迭代一个只输出"1"而没有别的..这对我来说太晚了我无法发现为什么,我不知道哪里出错了,这里有什么明显的东西吗?

public static BigInteger ifact(BigInteger n) {
    BigInteger ret = new BigInteger("1");
    BigInteger i = new BigInteger("1");
    for(i = new BigInteger("0"); i.compareTo(n) == 0; i.add(new BigInteger("1"))){
        ret.multiply(i);
    }
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

如果您没有注意到它使用了BigInteger软件包,请查看奇怪的文章.

另外,像C一样,你可以做类似于typedef的事情,所以我不需要每次都输入"BigInteger"吗?

编辑:我想我的意思是设定retn,可能是它,或者......可能不是.

java algorithm factorial

1
推荐指数
2
解决办法
321
查看次数

C中的递归因子程序在执行时挂起

我正在编写一个程序来显示计算给定数量200万次的阶乘所需的时间.我是在C/C++ Eclipse环境中使用Debian Linux编写的.当程序到达时int temp = n * rfact(n-1);,它会挂起并且不会执行任何其他操作.

这是我到目前为止所得到的:

#include <stdio.h>
#include <time.h>

//prototypes
int rfact(int n);

main()
{
    int n = 0;
    int i = 0;
    double result = 0.0;
    clock_t t;
    printf("Enter a value for n: ");
    scanf("%i", &n);

printf("n=%i\n", n);

    //get current time
    t = clock();

    //process factorial 2 million times
    for(i=0; i<2000000; i++)
    {
        rfact(n);
    }

    printf("n=%i\n", n);

    //get total time spent in the loop
    result = (clock() - t)/(double)CLOCKS_PER_SEC;

    //print result
    printf("runtime=%d\n", result); …
Run Code Online (Sandbox Code Playgroud)

c eclipse recursion factorial

1
推荐指数
1
解决办法
611
查看次数

可以被行中的素数整除为pascal三角形的数字的数量

如何找到一个pascal三角形的给定行号中的数字总数,该三角形可以被一个素数整除,其中给出行号和素数我在python中使用以下代码

def factorial(x):
    result = 1
    for i in xrange(1,x+1):
        result *= i
    return result

def combination(n,r):
    return factorial(n)/(factorial(n-r)*factorial(r))

p = input()
cnt = 0
for i in range(0,n+1):
    if((combination(n,i)%p)==0):
        cnt += 1
print cnt
Run Code Online (Sandbox Code Playgroud)

但是给定的代码需要很长时间才能获得大数字.你能告诉我一个更好的算法吗?

python algorithm primes factorial

1
推荐指数
1
解决办法
1215
查看次数

使用递归方法调用的因子计算

这段代码来自于书中的"C#5.0 in a Nutshell",是LinqPad上的一个示例.在第39页上它说:

"这个方法是递归的,这意味着它自己调用.每次输入方法时,都会在堆栈上分配一个新的int,每次方法退出时,int都会被释放."

使用文字5的产量和答案120(3产量6)

谁能解释一下这是如何工作的?我是一名VB程序员,正在尝试学习C#,并希望了解这样的结构.我已经多次单步执行它,无法理解之后会发生什么,x == 0并返回1.直到x等于零,执行流程很容易理解.之后,最后一个返回语句似乎重复执行(神奇地),直到x增加回原始值,然后返回到最初调用它的位置,并产生上述(神奇)数字结果.

static void Main()
{
    Console.WriteLine (Factorial(5));
}
static int Factorial (int x)
{
    if (x == 0) return 1;
    return x * Factorial (x-1);
}
Run Code Online (Sandbox Code Playgroud)

c# recursion factorial

1
推荐指数
1
解决办法
2614
查看次数

c 中阶乘中尾随零的数量

我是 C 编程的新手我想找出给定数字的阶乘中尾随零的数量

{
        long int n=0,facto=1,ln=0;
        int zcount=0,i=1;
        printf("Enter a number:");
        scanf("%ld",&n);
        if(n==0)
        {
          facto=1;
        }
        else
        {
           for(i=1;i<=n;i++)
           {
              facto=facto*i;
           }
        }
        printf("%ld",facto);
        while(facto>0)
        {
           ln=facto%10;
           facto/10;
           if(ln=!0)
           { 
               break;
           }
           else
           { 
               zcount+=1;
           }
        }
        printf("Tere are Total %d Trailing zeros in given factorial",zcount);
}
Run Code Online (Sandbox Code Playgroud)

我试图计算将返回给定数字的最后一位作为余数的数字的模数,然后n/10;将删除最后一个数字。

执行程序后,输出总是将尾随零的数量显示为“0”,if(ln =! 0)即使有零也总是满足条件。

c factorial

1
推荐指数
1
解决办法
8173
查看次数

C#计算阶乘

我有这段代码,它从用户那里获取输入并计算其阶乘和小于输入数字的阶乘,但我一直只获取第一个数字的阶乘,其余为 0。它应该是这样的:例如,如果输入是 5:

5!= 120

4!= 24

3!= 6

2!= 4

1!= 1

如何让循环抛出输入数字下方的所有数字?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace multiple_factorials
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, n;

            Console.WriteLine(".....................[Application 1].....................\n\n");
            Console.WriteLine("Please enter a number to get its factorial: ");
            num = Convert.ToInt32(Console.ReadLine());

            n = num; // Assign n to num

            while (num > 0)
            {
                for (int i = n - 1; i > 0; i--)
                {
                   n *= i; …
Run Code Online (Sandbox Code Playgroud)

c# factorial

1
推荐指数
3
解决办法
2万
查看次数

Oracle通过递归获得所有组合可能性

我真的需要你的SQL请求帮助.

我有这样一张桌子:

ID|LABEL|PRICE
1 |A    |10
2 |B    |15
3 |C    |20
4 |D    |30
5 |E    |35
Run Code Online (Sandbox Code Playgroud)

我想得到所有组合可能与sql请求(或pl/sql程序)喜欢这样:

A, AB, AC, AD, AE, ABC, ABD, ABE, AC, ABCD, ABCE, ABCDE... DE, BDE, CE...
Run Code Online (Sandbox Code Playgroud)

每个标签只能出现一次,例如,ABA是不可能的,我认为它就像一个因子数学函数?

我尝试"开始""连接",但我不明白如何正确使用它.

你有想法得到这个吗?

谢谢你的帮助.

查尔斯

sql oracle recursion combinations factorial

1
推荐指数
1
解决办法
1812
查看次数

如何计算张量流中的阶乘?

我是tensorflow的新手,我试图找到一个计算n!的函数。我看到可以使用theano函数使用theamma函数,但不适用于tensorflow。

factorial = theano.tensor.gamma(v)
Run Code Online (Sandbox Code Playgroud)

我正在使用for循环将数字从n乘以1,但是我认为有一种更简便快捷的方法。我看到了与伽马分布有关的函数,但无法弄清楚如何计算阶乘。如果能指出我一些文档,将不胜感激。

这是我现在做的方式

import tensorflow as tf

factorial = tf.Variable(1, "factorial")
recursion = tf.Variable(1, "recursion")

# calculate factorial
mult = tf.multiply(recursion, factorial)
assign_fact = tf.assign(factorial, mult)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init) 
    for i in range(2,10):
        counter = tf.assign(recursion, tf.constant(i))
        sess.run(counter)
        sess.run(assign_fact)

        print(i,"factorial is", sess.run(factorial))

    sess.close()
Run Code Online (Sandbox Code Playgroud)

输出是

2 factorial is 2
3 factorial is 6
4 factorial is 24
5 factorial is 120
6 factorial is 720
7 factorial is 5040
8 factorial is 40320 …
Run Code Online (Sandbox Code Playgroud)

python factorial tensorflow

1
推荐指数
1
解决办法
1135
查看次数

为什么我的阶乘函数返回NaN?

我使用递归和while循环编写了一个阶乘函数,但无论何时调用它,它的返回值都是NaN.我想知道为什么吗?以及如何解决它?

功能

function factorial(n) {
    while(n > 0)
        return factorial(n - 1) * n;
}
Run Code Online (Sandbox Code Playgroud)

javascript recursion nan factorial while-loop

1
推荐指数
1
解决办法
83
查看次数

比较 python 阶乘的性能(数学与 scipy)

为什么math.factorial比 快这么多scipy.special.factorial

import timeit

t = timeit.timeit("from math import factorial; factorial(20)"); print(t)
0.6399730000412092

t = timeit.timeit("from scipy.special import factorial; factorial(20)"); print(t)
5.339432950946502

t = timeit.timeit("from scipy.special import factorial; factorial(20, exact=True)"); print(t)
1.7984685270348564
Run Code Online (Sandbox Code Playgroud)

我使用的是 Python 3.7(scipy 版本是 1.1.0)

python factorial

1
推荐指数
1
解决办法
566
查看次数