C# 中的质因数
我想用 C# 2005 创建一个程序来计算给定输入的素因数。我想使用基本和最简单的东西,不需要为其创建方法,也不需要数组等,只是简单的模数。有没有任何代码可以满足我的愿望?
这是查找简单因子的代码,我需要修改此代码来计算素因子
class Program
{
static void Main(string[] args)
{
int a, b;
Console.WriteLine("Please enter your integer: ");
a = int.Parse(Console.ReadLine());
for (b = 1; b <= a; b++)
{
if (a % b == 0)
{
Console.WriteLine(b + " is a factor of " + a);
}
}
Console.ReadLine();
}
}
I want to create a program in C# 2005 which calculates prime factors of a given input. i want to use the basic and simplest things, no need to create a method for it nor array things etc. just simple modulus. is there any code which fulfills what i desire?
here is the code for finding simple factors, i need this code to be modified to calculate prime factors
class Program
{
static void Main(string[] args)
{
int a, b;
Console.WriteLine("Please enter your integer: ");
a = int.Parse(Console.ReadLine());
for (b = 1; b <= a; b++)
{
if (a % b == 0)
{
Console.WriteLine(b + " is a factor of " + a);
}
}
Console.ReadLine();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您想了解开发步骤,可以在此处观看视频< /a>.
If you want to learn steps of development you can watch video here.
您可以做得更好,因为除数永远不会大于数字的平方根。
You can go one better as the divisor can never be larger than the square root of the number.
尝试这段代码(我在此代码中合并了各种解决方案)。虽然,插值不是在 2005 年(我认为是这样......)
所以,无论如何,试试这个:
Try this code (I incorporated various solutions in this code). Although, interpolation is not in 2005 (I think so....)
So, anyways, try this:
最有效的方法是使用轮分解的优化实现。下面的示例演示了
2, 3, 5
轮,只需考虑(30k ± {1, 7, 11, 13})
形式的因素。The most efficient way is to use an optimized implementation of wheel factorization. Here is an example that demonstrates a
2, 3, 5
wheel that only needs to consider factors of the form(30k ± {1, 7, 11, 13})
.该版本以明确的公式列出了所有因素:
This version lists all the factors as an explicit formula: