标签: factorial

无法在C中使用阶乘函数

我无法使用以下代码.

#include <stdio.h>

// I am not sure whethere I should void here or not.
int main() {
    // when the first bug is solved, I put here arg[0]. It should be
    // similar command line parameter as args[0] in Java.
    int a=3;                  
    int b; 
    b = factorial(a);

    // bug seems to be here, since the %1i seems to work only in fprintf
    printf("%1i", b);
    return 0;      
}  

int factorial(int x) {
    int i; 
    for(i=1; i<x; i++) 
        x *= i; …
Run Code Online (Sandbox Code Playgroud)

c factorial

0
推荐指数
2
解决办法
3482
查看次数

用于查找数字阶乘的 8085 汇编语言程序

我想找到一个我首先占用内存的数字的阶乘。(intel 8085)

编辑:我是初学者。我不知道如何编写它的汇编代码。

伪代码:

input n
fact = 1
loop:
..multiply fact by n
..decrement n
..test n
..jump if not zero to loop
output fact
Run Code Online (Sandbox Code Playgroud)

assembly factorial 8085

0
推荐指数
1
解决办法
4万
查看次数

在n中找到最后的零个数!

有没有有效的方法来计算n末尾的零数!没有明确需要计算n!?

c c++ factorial

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

javascript程序中的"意外的输入结束"

<html>
<head>
<title>
</title>

<script>
var n = 2;
var days = 365;

function factorial() {
        var result = 1;
        for (var i = 1; i <= n; i++) {
            result *= i
      }
       return result;
    }  

var theNumber = function baseExponent (base, exponent) {
    var number = 1;
    for (i=0; i < exponent; i++) {
        var number = base * number;
    }
    return number
}


document.write(factorial()*182)/(theNumber(days, n)
//i used the (formula n! * combination 365 2)/365^n
//i cheated a …
Run Code Online (Sandbox Code Playgroud)

javascript factorial

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

当输入高于 12 时阶乘计算失败

我的代码对数字进行阶乘,但由于某种原因,每当我输入 13 或更高的数字时,它要么给出错误的数字,要么以某种方式得到负数。有什么建议么?

        List<int> myList = new List<int>();
        Console.WriteLine("My Job is to take the factorial of the number you give");
        Console.WriteLine("What is the number?");
        string A = Console.ReadLine();
        int C = Convert.ToInt32(A);
        int k = C;
        int B = C;
        int U = C - 1;
        Console.Write("{0} ", B);
        while (U != 0)
        {
           k *= U;
           Console.Write("* {0} ", U);
            U--;
        }
        Console.WriteLine(" = {0}", k);
        Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

c# factorial

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

为什么使用int64_t给出错误的结果,而double工作正如预期的简单整数乘法

这是我的代码:

using integer = int64_t;

integer factorial(integer number) {
    return number <= 0 ? 1 : number * factorial(number - 1);
}

integer binomial_coefficent(integer n, integer r) {
    return factorial(n) / (factorial(r) * factorial(n - r));
}

int main()
{
    using namespace std;
    cout << binomial_coefficent(40, 20) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这打印

0
Run Code Online (Sandbox Code Playgroud)

这是错误的答案,但如果我将整数类型更改为将打印1.37847e+11 哪个是正确答案,我的问题是为什么使用int64_t给我错误的答案

c++ double int factorial

0
推荐指数
3
解决办法
111
查看次数

C中的因子递归(分段错误)

嘿大家我是初学程序员,我的递归代码有问题来计算数字的阶乘.

我得到分段错误,我不知道为什么会这样.

任何帮助将非常感谢:)

(在我的代码中,我试图计算例如4的阶乘)

#include <stdio.h>

int factorial(int i) {
    int result = i * factorial(i - 1);
    return result;
}

int main()
{   
    int result = factorial(4);
    printf("result is %d", result);
}   
Run Code Online (Sandbox Code Playgroud)

c recursion factorial segmentation-fault

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

阶乘的模除以阶乘

如何计算a!/(b1! b2! ... bm!) modulo pp素数在哪里?a和的阶乘b可能非常大(long long int不够),所以我需要传递到模。

c++ algorithm factorial modulo number-theory

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

C 中数字的阶乘

所以我正在编写一个程序来打印 C 中数字的阶乘。我的代码 -

#include <stdio.h>
int main(void)
{
    int a,i ;
    printf("Enter the number = ");
    scanf("%d", &a);

    for(i=1; i<a; i++)
    {
        a = a*i;
    }
    printf("The factorial of the given number is = %d\n",a);
}
Run Code Online (Sandbox Code Playgroud)

现在这个程序正在打印一些垃圾值。我问了一些朋友,他们说为阶乘添加另一个变量,并使用该阶乘变量的循环,但他们都不知道为什么这段代码是错误的。

我的问题是为什么这段代码是错误的?这个 For 循环在这里做什么?为什么它不打印数字的阶乘,而是打印一些垃圾值?

预期产出-

Enter the number = 5
The factorial of the given number is = 120
Run Code Online (Sandbox Code Playgroud)

我得到的输出是-

Enter the number = 5
The factorial of the given number is = -1899959296
Run Code Online (Sandbox Code Playgroud)

c variables factorial

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

对于大于 12 的数字,阶乘给出错误答案

用户输入一个数字,该数字将传递给 calcFactorial 函数。只要数字大于零,循环就会重复。该数字将乘以结果中存储的任何内容。每次重复循环时,x 都会减一。

当我测试它时,程序只能运行到 12。之后我会得到错误的数字。数字太大了吗?我不确定问题是什么。

#include <stdio.h>

long calcFactorial(int x){
    long result = 1;
    while(x > 0){
        result *= x;
        x--;
    }
    return result;
}
int main(){
    int x;
    printf("Enter a number: ");
    scanf(" %d",&x);
    printf("%d",calcFactorial(x));
}
Run Code Online (Sandbox Code Playgroud)

c factorial

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