Factor of a number chosen by the user

Asked

Viewed 676 times

1

I’m not getting to be factorial expression, which is:

1 + (1/1! ) + (1/2! ) + (1/3! ) + ... + (1/n!)

Whereas the number n is entered by the user.

My code:

Console.WriteLine("A expreção 1 + (1/1!) + (1/2!) + (1/3!) + ... + (1/n!)");
double fatorial = 1, s;
Console.WriteLine("Digite a quantidade de fatoação");
s = double.Parse(Console.ReadLine());
for (double n = 1; n <= s; n++)
{
    fatorial = fatorial * n;

    Console.WriteLine("############################################################################");
    Console.WriteLine("A fatoração de " + n + " é: " + fatorial);
    Console.WriteLine("");
    Console.WriteLine("############################################################################");
    Console.WriteLine("1 ÷ pela fatoração de " + 1 / fatorial);
    Console.WriteLine("");
}
double conta = (1 / fatorial + fatorial);
Console.WriteLine("1 ÷ pela fatoração de " + conta);
Console.ReadKey();

3 answers

3

You can use a separate function to calculate the factorial, as already suggested in the other answers. But you can also do everything in one loop.

The account starts with 1, and then adds up to 1/1! , 1/2! , 1/3! etc. But since 3! = 3 x 2! and the value of 2! has already been calculated in the previous term, there is no need to calculate everything again. Just multiply 3 by the previous value (2!). And in the next iteration, when you have to calculate 4!, you don’t have to do one! loop to calculate everything (1 * 2 * 3 * 4), just do 4 x 3! (3! has already been calculated in the previous iteration). And so on...

So stay like this:

Console.WriteLine("Digite n: ");
int n = int.Parse(Console.ReadLine());
double total = 1;
double denominador = 1;
for (int i = 1; i <= n; i++)
{
    denominador *= i;
    total += 1.0 / denominador;
}
Console.WriteLine(total);

In the first iteration, denominador is 1, and is multiplied by 1 (then its value equals 1!), so we have 1/1! , which is added to the total.

In the second iteration, denominador is multiplied by 2 (i.e., its value is the same as 2!), and 1/2! is added to the total.

In the third iteration, denominador is multiplied by 3. As its previous value was 2!, then it becomes 3 x 2! which is the same as 3! - then 1/3! is added to the total.

And so on and so forth...


Not that it’s wrong to do a function or loop separate only to calculate factorial. But you will be doing loops which are not necessary in that case.

2

In a quick search on the internet, there are 3 ways to calculate a factorial, which are:

Using Loop

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

namespace factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, number, fact;
            Console.WriteLine("Enter the Number");
            number = int.Parse(Console.ReadLine());
            fact = number;
            for (i = number - 1; i >= 1; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine("\nFactorial of Given Number is: "+fact);
            Console.ReadLine();

        }
    }
}

Using Recursiveness

public double factorial_Recursion(int number)
{
    if (number == 1)
        return 1;
    else
        return number * factorial_recursion(number - 1);
}

While using

public double factorial_WhileLoop(int number)
{
    double result = 1;
    while (number != 1)
    {
        result = result * number;
        number = number - 1;
    }
    return result;
}

Examples taken from https://www.csharpstar.com/csharp-program-to-calculate-factorial/

In your case you were not making the factorial for each number.

Example of your problem using recursiveness

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("A expreção 1 + (1/1!) + (1/2!) + (1/3!) + ... + (1/n!)");
        Console.WriteLine("Digite a quantidade de fatoração");

        var qtde = double.Parse(Console.ReadLine());
        double fatorial = 0;
        double resultado = 1;

        for (var item = 1; item <= qtde; item++)
        {
            fatorial = factorial_Recursion(item);
            resultado += (1 / fatorial);

            Console.WriteLine("############################################################################");
            Console.WriteLine("A fatoração de " + item + " é: " + fatorial);
            Console.WriteLine("");
            Console.WriteLine("############################################################################");
            Console.WriteLine("1 ÷ pela fatoração de " + 1 / fatorial);
            Console.WriteLine("");
        }
        Console.WriteLine("1 ÷ pela fatoração de " + resultado);
    }

    private static double factorial_Recursion(int number)
    {
        if (number == 1)
            return 1;
        else
            return number * factorial_Recursion(number - 1);
    }

}

Url https://dotnetfiddle.net/8nZ9ep

1

You need two cycles, one to calculate the factorial, and the other to accumulate the value.

Console.WriteLine("A expreção 1 + (1/1!) + (1/2!) + (1/3!) + ... + (1/n!)");
Console.WriteLine("Digite a quantidade de fatoação");
int s = int.Parse(Console.ReadLine());

double acum = 1;
for (int n = 1; n <= s; n++)
{
    int fatorial = 1;
    for (int i = n; i > 0; i--) fatorial *= i;

    acum += (1 / (double)fatorial);

    Console.WriteLine("A fatoração de " + n + " é: " + fatorial);
    Console.WriteLine("1 ÷ pela fatoração de " + acum);
    Console.WriteLine("######################################");
    Console.WriteLine("");
}
Console.WriteLine("1 ÷ pela fatoração de " + acum);

Console.ReadKey();

See working in dotnetfiddle

Linq version:

Console.WriteLine("Digite a quantidade de fatoação");
int s = int.Parse(Console.ReadLine());

double result = 1 + Enumerable.Range(1, s).Select(n => 1 / (double)Enumerable.Range(1, n).Aggregate((a, b) => a * b)).Sum();

Console.WriteLine(result);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.