Есть ли способ сравнить, какое факториальное число больше среди двух чисел без вычисления?
Сценарий: я создаю консольное приложение С#, которое принимает два факториала, например
123!!!!!!
456!!!
все, что я хочу сделать, это сравнить, какое факториальное значение больше, чем другое, кусок кода, который я сделал,
try
{
string st = Console.ReadLine();
Int64 factCount = 0;
while (st.Contains('!'))
{
factCount = st.Where(w => w == '!').Count();
st = st.Replace('!', ' ');
};
decimal result = 1 ;
for (Int64 j = 0; j < factCount; j++)
{
UInt64 num = Convert.ToUInt64(st.Trim());
for (UInt64 x = num; x > 0; x--)
{
result = result * x;
}
}
if (factCount == 0)
{
result = Convert.ToUInt64(st.Trim());
}
string st2 = Console.ReadLine();
Int64 factCount2 = 0;
while (st2.Contains('!'))
{
factCount2 = st2.Where(w => w == '!').Count();
st2 = st2.Replace('!', ' ');
};
decimal result2 = 1;
for (Int64 j = 0; j < factCount2; j++)
{
UInt64 num = Convert.ToUInt64(st.Trim());
for (UInt64 x = num; x > 0; x--)
{
result2 = result2 * x;
}
}
if (factCount2 == 0)
{
result2 = Convert.ToUInt64(st2.Trim());
}
if (result == result2)
{
Console.WriteLine("x=y");
}
else if (result < result2)
{
Console.WriteLine("x<y");
}
else if (result > result2)
{
Console.WriteLine("x>y");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
но ошибка, которую я получаю, - это
значение слишком велико или слишком мало для десятичного числа
Я понял ошибку, но есть ли способ сделать это
Просьба указать, есть ли какой-либо другой тип данных, который вмещает значение больше десятичного или есть ли другой способ сравнения этих факториалов
После реализации предложения @Bathsheba я немного изменил свой код
string st = Console.ReadLine();
int factCount = 0;
while (st.Contains('!'))
{
factCount = st.Where(w => w == '!').Count();
st = st.Replace('!', ' ');
};
string st2 = Console.ReadLine();
int factCount2 = 0;
while (st2.Contains('!'))
{
factCount2 = st2.Where(w => w == '!').Count();
st2 = st2.Replace('!', ' ');
};
int resultFactCount = factCount - factCount2;
decimal result = 1;
decimal result2 = 1;
if (resultFactCount > 0)
{
for (Int64 j = 0; j < resultFactCount; j++)
{
UInt64 num = Convert.ToUInt64(st.Trim());
for (UInt64 x = num; x > 0; x--)
{
result = result * x;
}
}
if (factCount == 0)
{
result = Convert.ToUInt64(st.Trim());
}
UInt64 num1 = Convert.ToUInt64(st.Trim());
if (result == num1)
{
Console.WriteLine("x=y");
}
else if (result < num1)
{
Console.WriteLine("x<y");
}
else if (result > num1)
{
Console.WriteLine("x>y");
}
}
else
{
int resultFactCount1 = System.Math.Abs(resultFactCount);
for (Int64 j = 0; j < resultFactCount1; j++)
{
UInt64 num = Convert.ToUInt64(st.Trim());
for (UInt64 x = num; x > 0; x--)
{
result2 = result2 * x;
}
}
if (factCount2 == 0)
{
result2 = Convert.ToUInt64(st2.Trim());
}
UInt64 num1 = Convert.ToUInt64(st.Trim());
if (result2 == num1)
{
Console.WriteLine("x=y");
}
else if (result2 < num1)
{
Console.WriteLine("x<y");
}
else if (result2 > num1)
{
Console.WriteLine("x>y");
}
}
Извините, но еще 123!!! настолько огромна, что я получаю ту же ошибку
Традиционно
m!!...!
сn
!
означаетm(m-n)(m-2n)....
, но здесь берется как(...((m!)!)!...)!
Замечание от Алека, да, я знаю, это печальная нотация, но вы видите, что обычное определение гораздо более полезно (в комбинатонике, место, где происходят факториалы), чем тот, который нужен OP.
Я бы поставил это в комментарии, но это было бы затмевано другими, и это очень важно.