“因素”方法 [英] "factors of" method
问题描述
.NET是否提供了因素。方法?
我需要确定一个整数的因子。
Does .NET provide a "factors of" method ?
I need to determine the factors of an integer.
推荐答案
<" John A Grandy < johnagrandy-at-gmail-dot-com>写道:
<"John A Grandy" <johnagrandy-at-gmail-dot-com>wrote:
.NET是否提供了因素。方法 ?
Does .NET provide a "factors of" method ?
No.
No.
我需要确定整数的因子。
I need to determine the factors of an integer.
你需要使用标准算法来找到它们,我很害怕。
这些整数有多大可能得到?
-
Jon Skeet - < sk *** @ pobox.com>
网站: http://www.pobox.com/~skeet
博客: http://www.msmvps.com/jon.skeet
C#深度: http://csharpindepth.com
You''ll need to use the standard algorithms to find them, I''m afraid.
How big might these integers get?
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
试试这样的事情
public List< intFactorsOf(int value)
{
List< intresult = new List< int>();
List< intprimeNumbers = new List< int {2,3,5,7,11,etc}};
而(值1)
{
foreach(在primeNumbers中为int prime)
if(value%prime == 0)
{
结果t.Add(prime);
value = value / prime;
}
}
返回结果;
}
可能无法工作甚至编译:-)
Pete
>
Try something like this
public List<intFactorsOf(int value)
{
List<intresult = new List<int>();
List<intprimeNumbers = new List<int{ 2, 3, 5, 7, 11, etc };
while (value 1)
{
foreach (int prime in primeNumbers)
if (value % prime == 0)
{
result.Add(prime);
value = value / prime;
}
}
return result;
}
Might not work or even compile :-)
Pete
Peter Morris写道:
Peter Morris wrote:
尝试这样的事情
public List< intFactorsOf(int value)
{
List< intresult = new List< int>();
List< intprimeNumbers =新列表< int {2,3,5,7,11等};
而(值1)
{
foreach (在primeNumbers中的int prime)
if(value%prime == 0)
{
result.Add(prime);
value = value / prime;
}
}
返回结果;
}
可能无法工作甚至编译:-)
Try something like this
public List<intFactorsOf(int value)
{
List<intresult = new List<int>();
List<intprimeNumbers = new List<int{ 2, 3, 5, 7, 11, etc };
while (value 1)
{
foreach (int prime in primeNumbers)
if (value % prime == 0)
{
result.Add(prime);
value = value / prime;
}
}
return result;
}
Might not work or even compile :-)
它会起作用(在感觉上)它会产生独特的素因子,如果你已经正确地列出了这个列表,那么对于大多数
数字而言,这是非常低效的。尝试值== 4看看我的意思。但是很容易修复;
你需要用另一个关键字替换一个关键字。结果列表将稍微不同。
如果你想采用蛮力方法,那么就没有特别的/>
使用素数列表的原因。只需将每个数字除以2以上;
只有素数才能真正作为因素出现。
-
J 。
It''ll work (in the sense that it produces unique prime factors, if you''ve
typed out the list correctly), but it''s preposterously inefficient for most
numbers. Try value == 4 to see what I mean. It''s easy enough to fix, though;
you need to replace one keyword with another. The resulting list will be
slightly different.
If you''re going for the brute force approach, though, there''s no particular
reason to use a list of primes. Just divide by every number from 2 upwards;
only the primes will actually come out as factors.
--
J.
这篇关于“因素”方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!