确定一个数字是否包含特定的素数因子

本文关键字:包含特 是否 数字 一个 | 更新日期: 2023-09-27 18:14:14

下面是我的代码,我不能让它按它应该的方式工作。

我必须找到质数(这很好)。那么,如果质数是7和3
(63 = 7 * 3 * 37 = 7)的数字是神奇的,如果它包含任何其他(98 = 7 * 7 * 242 = 7 * 3 * 2),它不是。

I'm kind stuck here:

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);