【发布时间】:2016-05-01 03:11:12
【问题描述】:
我试过了:
static void primeFactors(int n)
{
int t = 0;
List<int> lst = new List<int>();
for (int c = 2; c <= n; c = c + 1)
{
t = c;
if (t % 2 == 0 || t % 3 == 0 || t % 5 == 0)
{
Console.Write("{0} : ", t);
while (t % 2 == 0)
{
Console.Write("{0} ", 2);
t = t / 2;
}
for (int i = 3; i <= Math.Sqrt(t); i = i + 2)
{
while (t % i == 0)
{
Console.Write("{0} ", i);
t = t / i;
}
}
if (t >2)
Console.Write("{0}", t);
Console.WriteLine();
}
}
}
我的输出:
2 : 2
3 : 3
4 : 2 2
5 : 5
6 : 2 3
8 : 2 2 2
9 : 3 3
10 : 2 5
12 : 2 2 3
14 : 2 7 ** should not be list
15 : 3 5
16 : 2 2 2 2
18 : 2 3 3
20 : 2 2 5
21 : 3 7 ** should not be list
22 : 2 11 ** should not be list
24 : 2 2 2 3
25 : 5 5
26 : 2 13 ** should not be list
27 : 3 3 3
28 : 2 2 7 ** should not be list
30 : 2 3 5
所需的输出: 前 20 个数字是(分号后列出的因素):
2 : 2
3 : 3
4 : 2 2
5 : 5
6 : 2 3
8 : 2 2 2
9 : 3 3
10 : 2 5
12 : 2 2 3
15 : 3 5
16 : 2 2 2 2
18 : 2 3 3
20 : 2 2 5
24 : 2 2 2 3
25 : 5 5
27 : 3 3 3
30 : 2 3 5
32 : 2 2 2 2 2
36 : 2 2 3 3
40 : 2 2 2 5
注意:14(2*7),21 (3*7), 22 (2*11), 26 (2*13) 是不应出现在列表中的数字。因子只能是 2、3 或 5
【问题讨论】:
-
都是不同的语言,先决定你想要的代码是哪种语言?
-
我需要 vb /c#
-
只有 2、3、5 作为因数的自然数...是三个与其他一个或自身的倍数......请将标签更改为这些语言。
-
所以选择这两种语言中的一种,然后删除剩余的标签垃圾邮件。
标签: c#