Calculate sum of 1+(1/2!)+…+(1/n!) n number in C language

后端 未结 4 1357
情歌与酒
情歌与酒 2021-01-24 08:44

Like the title say, how I calculate the sum of n number of the form: 1+(1/2!)+⋯(1/n!)? I already got the code for the harmonic series:

#include 

         


        
4条回答
  •  不思量自难忘°
    2021-01-24 09:24

    If you're just looking for computing the first n factorials, I would suggest just computing them recursively, e.g.

    factorial[0] = 1;
    for (i = 1; i < n; i++) factorial[i] = factorial[i-1] * i;
    

    However, unless you store them as floating point numbers, the large factorials are going to overflow really quickly.

提交回复
热议问题