使用因子计算找到贝尔数的C程序

时间:2018-03-10 19:12:19

标签: c

我需要帮助我的程序,我认为它几乎就在那里。我要离开等式{n k} = n! / k!(n - k)!我不确定我是否正确计算它。在此先感谢您的帮助。

#include <stdio.h>

我不确定是否将我的阶乘函数声明为浮点数会弄乱任何东西,但编译器一直给我一个警告,直到我将其更改为浮点数

float factorial(float num)
{
    float result = 1;

    for(int i = 1; i < num; i ++)
        result *= i;

    return result;    
}

在铃声函数中,我不确定我应该用等式左边的n超过k做什么。

void bells(int n) 
{

    float sum = 1;
    //printing bells first number
    printf("Bell Number [0] = 1\n");
    for(int i = 1; i <= n; i++)
    {
        printf("Bell Number [%d] = %.0lf\n", i, sum);
        sum += factorial(n) / (factorial(i)*factorial(n - i));
    }

}

主要工作正常

 int main()
{
    int sum = 0;
    int restart = 1;

    //while is used to restart program on users request
    while(restart == 1)
    {
        int userNum;
        printf("Enter a number :");
        scanf("%d",&userNum);

        bells(userNum);

        //Restarting program on users request 
        printf("\nRestart program? (1 = yes : 0 = no) ");
        scanf("%d", &restart);

    }//end of restart while

}//end of main

示例输出:

贝尔号[0] = 1

贝尔号[1] = 1

贝尔号[2] = 20

贝尔号[3] = 362

贝尔号[4] = 3269

贝尔号[5] = 18773

贝尔号[6] = 76913

贝尔号[7] = 239705

贝尔号[8] = 592421

贝尔号[9] = 1197077

贝尔号[10] = 2028479

3 个答案:

答案 0 :(得分:0)

您将二项式系数(n k)与斯特林数{n k}混淆。如果要使用二项式系数求和公式,请使用此处引用的第一个公式: https://en.m.wikipedia.org/wiki/Bell_number#Summation_formulas

如果您想使用斯特林求和公式,请使用第二个公式。

我建议使用整数而不是浮点数,因为这些数字被定义为整数。

另外,检查您的阶乘功能。 2! = 2.您的函数返回1.

答案 1 :(得分:0)

#include <stdio.h>

//If I were you, I would rather use the recursive way for factorials
//Moreover, factorials are always positive and are big numbers, which means it will be better to use unsigned integers, and long ones
unsigned long long factorial(unsigned long long num)
{
    if(num < 2)
        return 1;

    return num*factorial(num-1);
}

//More handy to have a binomial function ;)
unsigned long long binomial(unsigned long long n, unsigned long long k)
{
    return factorial(n) / (factorial(k) * factorial(n - k));
}

//From wikipedia : "The Bell numbers satisfy a RECCURRENCE relation involving binomial coefficients"
unsigned long long bell(int n)
{
    if(!n) return 1; // same as if(n == 0), this is the stop condition of the reccurrence

    unsigned long long sum = 0;

    for(int k = 0; k < n; k++)
        sum += binomial(n-1, k) * bell(k); //recursive function as you can see

    return sum;
}

int main(int argc, char **argv)
{
    int restart = 1, userNum;

    //Better not "restart == 1" when looping like this ;) In fact, it will keep looping while "restart" is different from 0
    while(restart)
    {
        printf("Enter a positive number :");
        scanf("%d",&userNum);

        printf("The %ith number is : %llu\n", userNum, bell(userNum-1));

        printf("Restart program? (1 = yes : 0 = no) ");
        scanf("%d", &restart);
    }

    return 0;
}

编辑:如果你厌倦了写“unsigned long long”,你可以使用以下两种方法之一创建一个新类型:

#define fat_int unsigned long long

typedef unsigned long long fat_int;

答案 2 :(得分:0)

这就是试图做的事情,我想我会发布它。感谢所有帮助,它完美地工作。

svc.CreateOrganization(<params>);