[英]Determine whether a number contains specific prime factors
Below is my code, and I can't get it to work the way it should. 以下是我的代码,但无法使其正常工作。
I have to find prime numbers (this works fine). 我必须找到素数(这很好)。 Then, if the prime numbers are 7 and 3
然后,如果素数是7和3
( 63 = 7 * 3 * 3
or 7 = 7
) the number is magical, and if it contains any others ( 98 = 7 * 7 * 2
or 42 = 7 * 3 * 2
) it's not. (
63 = 7 * 3 * 3
或7 = 7
)这个数字是神奇的,如果包含其他数字( 98 = 7 * 7 * 2
或42 = 7 * 3 * 2
)则不是。
I'm kind of stuck here: 我有点卡在这里:
if (b != 7 && b != 3)
Console.WriteLine(k);
else
Console.WriteLine(j);
I don't know how to fix it. 我不知道该如何解决。 Here is the whole code:
这是完整的代码:
string k="isnt magical";
string j = "is magical";
int a, b;
Console.WriteLine("Vnesite svoje stevilo: ");
a = Convert.ToInt32(Console.ReadLine());
for (b = 2; a > 1; b++)/
if (a % b == 0)
{
while (a % b == 0)
{
a /= b;
}
if (b != 7 && b != 3)
Console.WriteLine(k);
else
Console.WriteLine(j);
}
You are printing "isnt magical"
or "is magical"
for every factor. 您正在为每个因素打印
"isnt magical"
或"is magical"
。 Your code should look like this: 您的代码应如下所示:
string k = "isnt magical";
string j = "is magical";
int a, b;
Console.WriteLine("Vnesite svoje stevilo: ");
a = Convert.ToInt32(Console.ReadLine());
var allMagical = true;
for(b = 2; a > 1; b++) if(a % b == 0)
{
while(a % b == 0) a /= b;
if(b != 7 && b != 3) allMagical = false;
}
Console.WriteLine(allMagical ? j : k);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.