Исправить функцию для подсчета большого факториала - C#
Формулировка задачи:
Помогите написать функцию для подсчета большого факториала,
вот код программы
А вот проверка максимального значения факториала max=170.
Я смог только до 170 дальше бесконечность но из за этого не работает ряд Тейлора как надо.
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication40
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Clear();
- int c = 0;
- double y = 0;
- double x1 = 0;
- double x2 = 0;
- double dx = 0;
- double e = 0;
- double d = 0;
- double f = 0;
- int l = 0;
- bool u = true;
- double bufy = 0;
- bool o = true;
- while (o)
- {
- try
- {
- o = false;
- Console.Write("Введите х начальное ");
- x1 = Convert.ToDouble(Console.ReadLine());
- Console.Write("Введите х конечное ");
- x2 = Convert.ToDouble(Console.ReadLine());
- Console.Write("Введите шаг ");
- dx = Convert.ToDouble(Console.ReadLine());
- Console.Write("Введите точность ");
- e = Convert.ToDouble(Console.ReadLine());
- }
- catch
- {
- o = true;
- Console.Clear();
- Console.WriteLine("Ошибка ввода");
- }
- }
- Console.WriteLine("Таблица значений");
- Console.WriteLine("---------------------------");
- Console.WriteLine(" x | y | k ");
- Console.WriteLine("---------------------------");
- for (int i = 1; x1 <= x2; i++)
- {
- c = 0;
- l = 1;
- y = 0;
- d = 0;
- f = 0;
- u = true;
- for (int n = 0; u; n++)
- {
- f = fac(n);
- bufy = (Math.Pow((-1), n) * (Math.Pow(x1, 2 * n))) / f;
- y = y + bufy;
- if (Math.Abs(bufy) < e) u = false;
- d = bufy;
- c++;
- //f = f * c;
- //l = l * -1;
- y = Math.Round(y, 7);
- // Console.WriteLine(" n={0}, fac={1}",n, fac(n));
- }
- Console.Write("|{0,5} ", x1);
- Console.Write("|{0,13}|", y);
- Console.WriteLine(" {0,3}", c);
- x1 = x1 + dx;
- }
- Console.ReadLine();
- }
- private static double fac(int n)
- {
- if (n <= 1) return (1);
- else return (n * fac(n - 1));
- }
- }
- }
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication51
- {
- class Program
- {
- private static double fac(int n)
- {
- if (n <= 1) return (1);
- else return (n * fac(n - 1));
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Введите n=");
- double p = double.Parse(Console.ReadLine());
- for (int n = 0; n < p; n++)
- {
- // if (n = false)
- // break;
- Console.WriteLine("n={0},fac={1}", n, fac(n));
- }
- Console.ReadLine();
- }
- }
- }
Решение задачи: «Исправить функцию для подсчета большого факториала»
textual
Листинг программы
- double current = 1;
- int n = 0;
- double sum = 1;
- int sign = 1;
- for (/*условие цикла*/)
- {
- n++;
- sign = -1 * sign; //меняем знак
- current = current * x * x; //домножаем на x^2
- current = current / n; //делим на номер счетчика(вот и весь факториал)
- current = current * sign;
- sum = sum + current;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д