给定一个正整数n,编写一个函数来计算1/1的和! + 1/2! + .. + 1 / n!
一个简单的解决方案是将sum初始化为0,然后运行一个循环并在循环内调用阶乘函数。
以下是简单解决方案的实现。
C++
// A simple C++ program to compute sum of series 1/1! + 1/2! + .. + 1/n!
#include
using namespace std;
// Utility function to find
int factorial(int n)
{
int res = 1;
for (int i=2; i<=n; i++)
res *= i;
return res;
}
// A Simple Function to return value of 1/1! + 1/2! + .. + 1/n!
double sum(int n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
sum += 1.0/factorial(i);
return sum;
}
// Driver program to test above functions
int main()
{
int n = 5;
cout << sum(n);
return 0;
}
Java
// A simple Java program to compute
// sum of series 1/1! + 1/2! + .. + 1/n!
import java.io.*;
class GFG {
// Utility function to find
static int factorial(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res *= i;
return res;
}
// A Simple Function to return value
// of 1/1! + 1/2! + .. + 1/n!
static double sum(int n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
sum += 1.0/factorial(i);
return sum;
}
// Driver program
public static void main (String[] args)
{
int n = 5;
System.out.println(sum(n));
}
}
// This code is contributed by Ajit.
Python3
# Python3 program to compute sum of series
# 1/1! + 1/2! + .. + 1/n!
# Function to find factorail of a number
def factorial(n):
res = 1
for i in range(2, n + 1):
res *= i
return res
# A Simple Function to return value
# of 1/1! + 1/2! + .. + 1/n!
def sum(n):
s = 0.0
for i in range(1, n + 1):
s += 1.0 / factorial(i)
print(s)
# Driver program to test above functions
n = 5
sum(n)
# This code is contributed by Danish Raza
C#
// A simple C# program to compute sum
// of series 1/1! + 1/2! + .. + 1/n!
using System;
class GFG {
// Utility function to find
static int factorial(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res *= i;
return res;
}
// A Simple Function to return value
// of 1/1! + 1/2! + .. + 1/n!
static double sum(int n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
sum += 1.0/factorial(i);
return sum;
}
// Driver program
public static void Main ()
{
int n = 5;
Console.WriteLine(sum(n));
}
}
// This code is contributed by Sam007.
PHP
Javascript
C++
// A simple C++ program to compute sum of series 1/1! + 1/2! + .. + 1/n!
#include
using namespace std;
// An Efficient Function to return value of 1/1! + 1/2! + .. + 1/n!
double sum(int n)
{
double sum = 0;
int fact = 1;
for (int i = 1; i <= n; i++)
{
fact *= i; // Update factorial
sum += 1.0/fact; // Update series sum
}
return sum;
}
// Driver program to test above functions
int main()
{
int n = 5;
cout << sum(n);
return 0;
}
Java
// A simple Java program to compute
// sum of series 1/1! + 1/2! + .. + 1/n!
import java.io.*;
class GFG {
// An Efficient Function to return
// value of 1/1! + 1/2! + .. + 1/n!
static double sum(int n)
{
double sum = 0;
int fact = 1;
for (int i = 1; i <= n; i++)
{
// Update factorial
fact *= i;
// Update series sum
sum += 1.0/fact;
}
return sum;
}
// Driver program
public static void main (String[] args)
{
int n = 5;
System.out.println(sum(n));
}
}
// This code is contributed by Ajit.
Python3
# Python3 program to compute sum of series
# 1/1! + 1/2! + .. + 1/n!
# Function to return value of
# 1/1! + 1/2! + .. + 1/n!
def sum(n):
sum = 0
fact = 1
for i in range(1, n + 1):
# Update factorial
fact *= i
# Update series sum
sum += 1.0/fact
print(sum)
# Driver program to test above functions
n = 5
sum(n)
# This code is contributed by Danish Raza
C#
// A simple C# program to compute sum
// of series 1/1! + 1/2! + .. + 1/n!
using System;
class GFG {
// An Efficient Function to return
// value of 1/1! + 1/2! + .. + 1/n!
static double sum(int n)
{
double sum = 0;
int fact = 1;
for (int i = 1; i <= n; i++)
{
// Update factorial
fact *= i;
// Update series sum
sum += 1.0 / fact;
}
return sum;
}
// Driver program
public static void Main ()
{
int n = 5;
Console.WriteLine(sum(n));
}
}
// This code is contributed by Sam007.
PHP
输出:
1.71667
上述解决方案的时间复杂度是O(n * n!),这是巨大的。
一个有效的解决方案可以找到O(n)时间的总和。这个想法是在与求和相同的循环中计算阶乘。以下是该想法的实现。
C++
// A simple C++ program to compute sum of series 1/1! + 1/2! + .. + 1/n!
#include
using namespace std;
// An Efficient Function to return value of 1/1! + 1/2! + .. + 1/n!
double sum(int n)
{
double sum = 0;
int fact = 1;
for (int i = 1; i <= n; i++)
{
fact *= i; // Update factorial
sum += 1.0/fact; // Update series sum
}
return sum;
}
// Driver program to test above functions
int main()
{
int n = 5;
cout << sum(n);
return 0;
}
Java
// A simple Java program to compute
// sum of series 1/1! + 1/2! + .. + 1/n!
import java.io.*;
class GFG {
// An Efficient Function to return
// value of 1/1! + 1/2! + .. + 1/n!
static double sum(int n)
{
double sum = 0;
int fact = 1;
for (int i = 1; i <= n; i++)
{
// Update factorial
fact *= i;
// Update series sum
sum += 1.0/fact;
}
return sum;
}
// Driver program
public static void main (String[] args)
{
int n = 5;
System.out.println(sum(n));
}
}
// This code is contributed by Ajit.
Python3
# Python3 program to compute sum of series
# 1/1! + 1/2! + .. + 1/n!
# Function to return value of
# 1/1! + 1/2! + .. + 1/n!
def sum(n):
sum = 0
fact = 1
for i in range(1, n + 1):
# Update factorial
fact *= i
# Update series sum
sum += 1.0/fact
print(sum)
# Driver program to test above functions
n = 5
sum(n)
# This code is contributed by Danish Raza
C#
// A simple C# program to compute sum
// of series 1/1! + 1/2! + .. + 1/n!
using System;
class GFG {
// An Efficient Function to return
// value of 1/1! + 1/2! + .. + 1/n!
static double sum(int n)
{
double sum = 0;
int fact = 1;
for (int i = 1; i <= n; i++)
{
// Update factorial
fact *= i;
// Update series sum
sum += 1.0 / fact;
}
return sum;
}
// Driver program
public static void Main ()
{
int n = 5;
Console.WriteLine(sum(n));
}
}
// This code is contributed by Sam007.
的PHP
输出:
1.71667