[英]Determine whether a number contains specific prime factors
以下是我的代碼,但無法使其正常工作。
我必須找到素數(這很好)。 然后,如果素數是7和3
( 63 = 7 * 3 * 3
或7 = 7
)這個數字是神奇的,如果包含其他數字( 98 = 7 * 7 * 2
或42 = 7 * 3 * 2
)則不是。
我有點卡在這里:
if (b != 7 && b != 3)
Console.WriteLine(k);
else
Console.WriteLine(j);
我不知道該如何解決。 這是完整的代碼:
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);
}
您正在為每個因素打印"isnt magical"
或"is magical"
。 您的代碼應如下所示:
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.