C# 比较两个阶乘而不进行计算
有没有办法不用计算就比较两个数中哪个阶乘数更大?C# 比较两个阶乘而不进行计算,c#,.net,math,decimal,factorial,C#,.net,Math,Decimal,Factorial,有没有办法不用计算就比较两个数中哪个阶乘数更大? 场景是我正在创建一个c#控制台应用程序,它接受两个阶乘输入,如 123!!!!!! 456!!! 我所要做的就是比较哪个阶乘值大于其他阶乘值,我所做的代码就是 try { string st = Console.ReadLine(); Int64 factCount = 0; while (st.Contains('!')) { factCount = st.Where(w => w ==
场景是我正在创建一个c#控制台应用程序,它接受两个阶乘输入,如
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();
}
试试看
{
字符串st=Console.ReadLine();
Int64 factCount=0;
而(st.Contains(“!”))
{
factCount=st.Where(w=>w='!').Count();
st=st.替换(“!”,“);
};
小数结果=1;
对于(Int64 j=0;j0;x--)
{
结果=结果*x;
}
}
如果(factCount==0)
{
结果=Convert.ToUInt64(st.Trim());
}
字符串st2=Console.ReadLine();
Int64 factCount2=0;
而(st2.Contains(“!”)
{
factCount2=st2.Where(w=>w='!').Count();
st2=st2。替换(“!”,“);
};
小数结果2=1;
对于(Int64 j=0;j0;x--)
{
结果2=结果2*x;
}
}
如果(factCount2==0)
{
result2=Convert.ToUInt64(st2.Trim());
}
如果(结果==result2)
{
控制台写入线(“x=y”);
}
否则如果(结果<结果2)
{
控制台写入线(“xy”);
}
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
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");
}
}
string st=Console.ReadLine();
内部因素计数=0;
而(st.Contains(“!”))
{
factCount=st.Where(w=>w='!').Count();
st=st.替换(“!”,“);
};
字符串st2=Console.ReadLine();
int factCount2=0;
而(st2.Contains(“!”)
{
factCount2=st2.Where(w=>w='!').Count();
st2=st2。替换(“!”,“);
};
int resultFactCount=factCount-factCount2;
小数结果=1;
小数结果2=1;
如果(resultFactCount>0)
{
对于(Int64 j=0;j0;x--)
{
结果=结果*x;
}
}
如果(factCount==0)
{
结果=Convert.ToUInt64(st.Trim());
}
UInt64 num1=Convert.ToUInt64(st.Trim());
如果(结果==num1)
{
控制台写入线(“x=y”);
}
否则如果(结果0;x--)
{
结果2=结果2*x;
}
}
如果(factCount2==0)
{
result2=Convert.ToUInt64(st2.Trim());
}
UInt64 num1=Convert.ToUInt64(st.Trim());
如果(结果2==num1)
{
控制台写入线(“x=y”);
}
否则如果(结果2
很抱歉,但还是123!!!太大了,我也会犯同样的错误
传统的
m代码>带有n
s表示m(m-n)(m-2n)…
但此处被视为(…((m!)!)代码>
Alec注意到,是的,我知道,这是一个不幸的符号,但你看,传统的定义(在组合数学中,阶乘的来源)比OP想要的要有用得多。
我会在评论中提到这一点,但它会被其他人所掩盖,这一点非常重要
这里,a代码>定义为(a!)代码>
123代码>绝对是巨大的。我想如果你用墨水写下来的话,你需要比宇宙中更多的粒子
因此,您无法直接比较这些数字。我猜想没有一个数字类可以做到这一点
<>你能做的,就是考虑商<代码> 123!456!!!代码>。许多倍数将是相似的,因此您可以取消它们。还要注意,后面的代码>将取消。这是因为x>y意味着,并且由x!>Y其中x和y是正整数
最终,您将达到一个点,您可以将其评估为小于或大于1,从而得出您的答案
我可以在检查时告诉你123代码>更大,因为123
大于456
该类型可以处理大整数。但是对于你的例子来说还不够大
小因子可以分解成它们的素因子,而不必首先计算因子本身,相同的因子可以被取消
您还可以取消尾随的代码>,如建议的,从123开始!!!大于456,(123!!!)!!!也将大于(456) 对于给定的数字,假设456代码>的意思是((456!)!)代码>我们有
123!!!!!! == (123!!!)!!!
及
甚至123
(它是1.2e205
)远大于仅456
为了估计阶乘的实际值,让我们使用斯特林近似
i、 e
所以((456!)!)代码>是关于
lg(456!) == 1014
lg((456!)!) == 1e1014 * 1014- 1e1014/ln(10) == 1e1017
lg(((456!)!)!) == 1e(1e1017)
((456!)!)! == 1e(1e(1e1017))
这是非常重要的
ln(n!) == n * ln(n) - n
lg(n!) == ln(n!)/ln(10) == n * ln(n) / ln(10) - n / ln(10) == n * lg(n) - n / ln(10)
n! == n ** n / exp(n)
lg(456!) == 1014
lg((456!)!) == 1e1014 * 1014- 1e1014/ln(10) == 1e1017
lg(((456!)!)!) == 1e(1e1017)
((456!)!)! == 1e(1e(1e1017))
123 !!!!!! > 456 !!!
123 !!!!! > 456 !!
123 !!!! > 456 !
123 !!! > 456
public class Program
{
static bool LeftIsGreaterThanRightSide(UInt64 leftSide, int leftSidefactCount, UInt64 rightSide)
{
try
{
checked // for the OverflowException
{
UInt64 input2 = leftSide;
int factCount = leftSidefactCount;
UInt64 result = 1;
for (Int64 j = 0; j < factCount; j++)
{
UInt64 num = input2;
for (UInt64 x = num; x > 0; x--)
{
result = result * x;
}
}
// None of the operand are great or equal than UInt64.MaxValue
// So let's compare the result normaly
return result > rightSide;
}
}
catch (OverflowException)
{
// leftSide overflowed, rightSide is a representable UInt64 so leftSide > rightSide ;
return true;
}
}
static void Main()
{
String input1 = Console.ReadLine();
String input2 = Console.ReadLine();
int fact1Count = input1.Count(c => c == '!');
int fact2Count = input2.Count(c => c == '!');
UInt64 x = Convert.ToUInt64(input1.Replace("!", String.Empty).Trim());
UInt64 y = Convert.ToUInt64(input2.Replace("!", String.Empty).Trim());
x = x == 0 ? 1 : x ; // Handling 0 !
y = y == 0 ? 1 : y;
if (fact1Count > fact2Count)
{
fact1Count = fact1Count - fact2Count;
Console.WriteLine(LeftIsGreaterThanRightSide(x, fact1Count, y) ? "x > y" : "x <= y");
}
else
{
fact2Count = fact2Count - fact1Count;
Console.WriteLine(LeftIsGreaterThanRightSide(y, fact2Count, x) ? "y > x" : "y <= x");
}
Console.ReadLine();
}
}
// While string parsing check if one number equals 0 and has at least
// one "!" - if yes set its value to 1 ( because 0! = 1! = 1 )
int x = 123;
int y = 456;
int numberOfFactorials = 3;
try
{
for( int i = 0; i < numberOfFactorials; ++i )
{
for ( int j = x-1; j > 0; --j )
{
x *= j;
// This quick exit will return after one iteration
// because 123*122 > 456
if ( x > y ) return "x is bigger than y";
}
}
return x == y ? "gosh they are the same!"
: "x is smaller than y";
}
catch( OverflowException e )
{
return "x Overflowed so it is bigger than y!";
}
1!! = 1! = 1
2!! = 2! = 2
3!! = 6! = 720
4!! = 24! = 620,448,401,733,239,439,360,000
5!! = 120! = about 6.6895 * 10^198
6!! = 720! = about 2.6012 * 10^1746
public struct RepeatedFactorial
{
private readonly int _baseNumber;
private readonly int _repeats;
public int BaseNumber
{
get { return _baseNumber; }
}
public int Repeats {
get { return _repeats; }
}
public RepeatedFactorial(int baseNumber, int repeats)
{
if (baseNumber < 0 || repeats < 0) throw new ArgumentOutOfRangeException();
_baseNumber = baseNumber;
_repeats = repeats;
}
}
public int CompareTo(RepeatedFactorial other)
{
// ?
}
public int CompareTo(RepeatedFactorial other)
{
if (BaseNumber == 0)
{
// If Repeats is zero the value of this is zero, otherwise
// this is the same as a value with BaseNumber == 1 and no factorials.
// So delegate to the handling of that case.
if (Repeats == 0) return other.BaseNumber == 0 && other.Repeats == 0 ? 0 : -1;
return new RepeatedFactorial(1, 0).CompareTo(other);
}
if (other.BaseNumber == 0)
// Likewise
return other.Repeats == 0 ? 1 : CompareTo(new RepeatedFactorial (1, 0));
if (Repeats == other.Repeats)
// X < Y == X! < Y!. X > Y == X! > Y! And so on.
return BaseNumber.CompareTo(other.BaseNumber);
???
}
public int CompareTo(RepeatedFactorial other)
{
if (BaseNumber == 0)
{
// If Repeats is zero the value of this is zero, otherwise
// this is the same as a value with BaseNumber == 1 and no factorials.
// So delegate to the handling of that case.
if (Repeats == 0) return other.BaseNumber == 0 && other.Repeats == 0 ? 0 : -1;
return new RepeatedFactorial(1, 0).CompareTo(other);
}
if (other.BaseNumber == 0)
// Likewise
return other.Repeats == 0 ? 1 : CompareTo(new RepeatedFactorial (1, 0));
if (Repeats == other.Repeats)
// X < Y == X! < Y!. X > Y == X! > Y! And so on.
return BaseNumber.CompareTo(other.BaseNumber);
if (Repeats > other.Repeats)
return -other.CompareTo(this);
???
}
public int CompareTo(RepeatedFactorial other)
{
if (BaseNumber == 0)
{
// If Repeats is zero the value of this is zero, otherwise
// this is the same as a value with BaseNumber == 1 and no factorials.
// So delegate to the handling of that case.
if (Repeats == 0) return other.BaseNumber == 0 && other.Repeats == 0 ? 0 : -1;
return new RepeatedFactorial(1, 0).CompareTo(other);
}
if (other.BaseNumber == 0)
// Likewise
return other.Repeats == 0 ? 1 : CompareTo(new RepeatedFactorial (1, 0));
if (Repeats == other.Repeats)
// X < Y == X! < Y!. X > Y == X! > Y! And so on.
return BaseNumber.CompareTo(other.BaseNumber);
if (Repeats > other.Repeats)
return -other.CompareTo(this);
if (Repeats != 0)
return new RepeatedFactorial(BaseNumber, 0).CompareTo(new RepeatedFactorial(other.BaseNumber, other.Repeats - Repeats);
???
}
public int CompareTo(RepeatedFactorial other)
{
if (BaseNumber == 0)
{
// If Repeats is zero the value of this is zero, otherwise
// this is the same as a value with BaseNumber == 1 and no factorials.
// So delegate to the handling of that case.
if (Repeats == 0) return other.BaseNumber == 0 && other.Repeats == 0 ? 0 : -1;
return new RepeatedFactorial(1, 0).CompareTo(other);
}
if (other.BaseNumber == 0)
// Likewise
return other.Repeats == 0 ? 1 : CompareTo(new RepeatedFactorial (1, 0));
if (Repeats == other.Repeats)
// X < Y == X! < Y!. X > Y == X! > Y! And so on.
return BaseNumber.CompareTo(other.BaseNumber);
if (Repeats > other.Repeats)
return -other.CompareTo(this);
if (Repeats != 0)
return new RepeatedFactorial(BaseNumber, 0).CompareTo(new RepeatedFactorial(other.BaseNumber, other.Repeats - Repeats);
if (other.BaseNumber > 12)
return -1; // this is less than other
???
}
public int CompareTo(RepeatedFactorial other)
{
if (BaseNumber == 0)
{
// If Repeats is zero the value of this is zero, otherwise
// this is the same as a value with BaseNumber == 1 and no factorials.
// So delegate to the handling of that case.
if (Repeats == 0) return other.BaseNumber == 0 && other.Repeats == 0 ? 0 : -1;
return new RepeatedFactorial(1, 0).CompareTo(other);
}
if (other.BaseNumber == 0)
// Likewise
return other.Repeats == 0 ? 1 : CompareTo(new RepeatedFactorial (1, 0));
if (Repeats == other.Repeats)
// X < Y == X! < Y!. X > Y == X! > Y! And so on.
return BaseNumber.CompareTo(other.BaseNumber);
if (Repeats > other.Repeats)
return -other.CompareTo(this);
if (Repeats != 0)
return new RepeatedFactorial(BaseNumber, 0).CompareTo(new RepeatedFactorial(other.BaseNumber, other.Repeats - Repeats);
int accum = other.BaseNumber;
for (int rep = 0; rep != other.Repeats; ++rep)
{
if (accum > 12 || accum > BaseNumber) return -1;
for (int mult = accum - 1; mult > 1; --mult)
accum *= mult;
}
return BaseNumber.CompareTo(accum);
}
public struct RepeatedFactorial : IComparable<RepeatedFactorial>
{
private readonly int _baseNumber;
private readonly int _repeats;
public int BaseNumber
{
get { return _baseNumber; }
}
public int Repeats {
get { return _repeats; }
}
public RepeatedFactorial(int baseNumber, int repeats)
{
if (baseNumber < 0 || repeats < 0) throw new ArgumentOutOfRangeException();
_baseNumber = baseNumber;
_repeats = repeats;
}
public int CompareTo(RepeatedFactorial other)
{
if (BaseNumber == 0)
{
// If Repeats is zero the value of this is zero, otherwise
// this is the same as a value with BaseNumber == 1 and no factorials.
// So delegate to the handling of that case.
if (Repeats == 0) return other.BaseNumber == 0 && other.Repeats == 0 ? 0 : -1;
return new RepeatedFactorial(1, 0).CompareTo(other);
}
if (other.BaseNumber == 0)
// Likewise
return other.Repeats == 0 ? 1 : CompareTo(new RepeatedFactorial (1, 0));
if (Repeats == other.Repeats)
// X < Y == X! < Y!. X > Y == X! > Y! And so on.
return BaseNumber.CompareTo(other.BaseNumber);
if (Repeats > other.Repeats)
return -other.CompareTo(this);
if (Repeats != 0)
return new RepeatedFactorial(BaseNumber, 0).CompareTo(new RepeatedFactorial(other.BaseNumber, other.Repeats - Repeats));
int accum = other.BaseNumber;
for (int rep = 0; rep != other.Repeats; ++rep)
{
if (accum > 12 || accum > BaseNumber) return -1;
for (int mult = accum - 1; mult > 1; --mult)
accum *= mult;
}
return BaseNumber.CompareTo(accum);
}
}
public struct RepeatedFactorial : IComparable<RepeatedFactorial>
{
private readonly ulong _baseNumber;
private readonly long _repeats;
public ulong BaseNumber
{
get { return _baseNumber; }
}
public long Repeats {
get { return _repeats; }
}
public RepeatedFactorial(ulong baseNumber, long repeats)
{
if (baseNumber < 0 || repeats < 0) throw new ArgumentOutOfRangeException();
_baseNumber = baseNumber;
_repeats = repeats;
}
public int CompareTo(RepeatedFactorial other)
{
if (BaseNumber == 0)
// This is the same as a value with BaseNumber == 1 and no factorials.
// So delegate to the handling of that case.
return new RepeatedFactorial(1, 0).CompareTo(other);
if (other.BaseNumber == 0)
// Likewise
return CompareTo(new RepeatedFactorial (1, 0));
if (Repeats == other.Repeats)
// X < Y == X! < Y!. X > Y == X! > Y! And so on.
return BaseNumber.CompareTo(other.BaseNumber);
if (Repeats > other.Repeats)
return -other.CompareTo(this);
if (Repeats != 0)
return new RepeatedFactorial(BaseNumber, 0).CompareTo(new RepeatedFactorial(other.BaseNumber, other.Repeats - Repeats));
ulong accum = other.BaseNumber;
for (long rep = 0; rep != other.Repeats; ++rep)
{
if (accum > 20 || accum > BaseNumber) return -1;
for (ulong mult = accum - 1; mult > 1; --mult)
accum *= mult;
}
return BaseNumber.CompareTo(accum);
}
}
123!!!!
456!!!!
456 > 123
456!!!! > 123!!!!
123!!!!!!
456!!!
(123!!!)!!!
(456!!!)
123!!!
456
int max_factorial (int x, int x_fact, int y, int y_fact)
{
int A=1,B=1,F=0,product=1,sum=0;
if (x_fact == y_fact) return (x>y?x:y);
if (x_fact > y_fact)
{
A = x; B = y; F = x_fact-y_fact;
}
else
{
A = y; B = x; F = y_fact-x_fact;
}
for (int k=0; k<F; k++)
{
try
{
for (int i=1; i<A; i++)
{
// multiplication in terms of addition
// P * i = P + P + .. P } i times
sum = 0; for (int p=0; p<i; p++) sum += product;
product = product + sum;
if (product > B) return A;
}
}
catch (OverflowException e)
{
return A;
}
}
return B;
}