using System;
namespace kaldata_task_1_console
{
// Напишете програма за изчисляване на сумата:
// 1 - 1 / 2 + 1 / 3 - 1 / 4 + ...+1 / 99999 - 1 / 100000
// Изчисленията да се проведат по следните 4 начина:
// 1) членовете се сумират отляво надясно;
// 2) членовете се сумират отдясно наляво;
// 3) отделно положителните, отделно отрицателните отляво надясно;
// 4) както в 3), но сумирането да става отдясно наляво.
// Сравнете получените резултати и обяснете различията.
class Program
{
private static int getSign(int num)
{
return num % 2 == 0 ? -1 : 1;
}
private static void calcWithDouble()
{
double sum_left_to_right = 0;
for (int i = 1; i <= 100000; i++)
{
sum_left_to_right += 1.0 / i * getSign(i);
}
Console.WriteLine("Left to right");
Console.WriteLine(sum_left_to_right.ToString());
double sum_right_to_left = 0;
for (int i = 100000; i >= 1; i--)
{
sum_right_to_left += 1.0 / i * getSign(i);
}
Console.WriteLine("Right to left");
Console.WriteLine(sum_right_to_left.ToString());
double sum_left_to_right_sep = 0;
for (int i = 1; i <= 100000; i += 2)
{
sum_left_to_right_sep += 1.0 / i;
}
for (int i = 2; i <= 100000; i += 2)
{
sum_left_to_right_sep += -1.0 / i;
}
Console.WriteLine("Left to right , first positives, then negatives");
Console.WriteLine(sum_left_to_right_sep.ToString());
double sum_right_to_left_sep = 0;
for (int i = 99999; i >= 1; i -= 2)
{
sum_right_to_left_sep += 1.0 / i;
}
for (int i = 100000; i >= 1; i -= 2)
{
sum_right_to_left_sep += -1.0 / i;
}
Console.WriteLine("Right to left , first positives, then negatives");
Console.WriteLine(sum_right_to_left_sep.ToString());
}
private static void calcWithDecimal()
{
Decimal sum_left_to_right = 0;
for (int i = 1; i <= 100000; i++)
{
sum_left_to_right += (Decimal)(1.0 / i * getSign(i));
}
Console.WriteLine("Left to right");
Console.WriteLine(sum_left_to_right.ToString());
Decimal sum_right_to_left = 0;
for (int i = 100000; i >= 1; i--)
{
sum_right_to_left += (Decimal)(1.0 / i * getSign(i));
}
Console.WriteLine("Right to left");
Console.WriteLine(sum_right_to_left.ToString());
Decimal sum_left_to_right_sep = 0;
for (int i = 1; i <= 100000; i += 2)
{
sum_left_to_right_sep += (Decimal)(1.0 / i);
}
for (int i = 2; i <= 100000; i += 2)
{
sum_left_to_right_sep += (Decimal)(-1.0 / i);
}
Console.WriteLine("Left to right , first positives, then negatives");
Console.WriteLine(sum_left_to_right_sep.ToString());
Decimal sum_right_to_left_sep = 0;
for (int i = 99999; i >= 1; i -= 2)
{
sum_right_to_left_sep += (Decimal)(1.0 / i);
}
for (int i = 100000; i >= 1; i -= 2)
{
sum_right_to_left_sep += (Decimal)(-1.0 / i);
}
Console.WriteLine("Right to left , first positives, then negatives");
Console.WriteLine(sum_right_to_left_sep.ToString());
}
static void Main(string[] args)
{
Console.WriteLine("Using doubles :");
calcWithDouble();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Using decimals :");
calcWithDecimal();
Console.WriteLine("The difference in the sums when using doubles is because of the way doubles are represented in binary. Press any key to exit...");
Console.ReadKey();
}
}
}