检查给定的数字是否为素数 [英] checking given number is prime or not

查看:93
本文介绍了检查给定的数字是否为素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生!

我编写的代码用于检查给定的数字是否为素数。



这是代码:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 prime
{
class Program
{
静态 void Main( string [] args)
{
int i,m;

for (m = 0 ; m< = 4 ; m ++)
{
Console.WriteLine( 输入数字\ n);
i = Convert.ToInt32(Console.ReadLine());

if ((i / 2 )!= 0
{
Console.WriteLine(i + \t是素数);

继续;
}
else
{
Console.WriteLine(i + \t不是素数);

Console.ReadKey();
}
}
Console.ReadKey();
}
}
}

但这并没有产生所需的输出

当我输入4时它显示4是素数。






第二版

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 prime
{
class Program
{
静态 void Main( string [] args)
{
int i,m;

for (m = 2 ; m< = i-1; m ++ )
{
Console.WriteLine( 输入数字\ n );
i = Convert.ToInt32(Console.ReadLine());

if ((i%m)== 0
{
Console.WriteLine(i + {0}不是素数\ n);
Console.WriteLine();
}
其他 如果(m == i)
{
Console.WriteLine(i + \t是prime);
Console.ReadKey( true );
}
}

Console.Clear();
Console.ReadKey();
}

}
}





但它说的是变量我不是已分配

解决方案

除了您的算法完全错误之外...

4/2!= 0(4/2 = 2 )所以4是素数(根据你的代码)!!!


4/2是2,它不是0所以你的代码说它是素数。你的代码似乎正在检查奇数而不是素数。如果数字是素数,有数学家一直致力于解决问题,并且(据我所知)尚未找到任何解决方案。



http://www.wikihow.com/Check-if-a-Number-Is-Prime [ ^ ]


首先,让我们回顾一下

 如果((i /  2 )!=  0 
{
Console.WriteLine(i + \t是prime);

继续;


}





如果是4,你把它除以2,得到2不是0,因此你进入如果部分条件。



其次,如果你需要知道你的号码是否是素数,你的逻辑需要不同。

你不能除以2并找出答案。例如9不能被2整除但仍不是素数。

素数 - https://en.wikipedia .org / wiki / Prime_number [ ^ ]。


Dear sir !
I've written code for checking given number whether it is prime or not.

Here 's the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prime
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, m;
          
            for (m = 0; m<=4;m++)
            {
                Console.WriteLine("Enter the number\n");
                i = Convert.ToInt32(Console.ReadLine());

                if ((i / 2)!= 0)
                {
                    Console.WriteLine(i + "\t is  prime");
                    
                    continue;
                }
                else
                {
                    Console.WriteLine(i+"\t is not prime");

                    Console.ReadKey();
                }
            }
            Console.ReadKey();
        }
    }
}

But this is not generating desired output
when I enter 4 it shows that 4 is prime .



Second version

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prime
{
class Program
{
static void Main(string[] args)
{
int i , m;

for (m = 2; m<=i-1; m++)
    {
    Console.WriteLine("Enter the number\n");
    i = Convert.ToInt32(Console.ReadLine());

    if ((i %m ) == 0)
        {
        Console.WriteLine(i + "{0} is not prime \n");
        Console.WriteLine();
        }
    else if(m==i)
        {
        Console.WriteLine(i + "\t is prime");
        Console.ReadKey(true);
        }
    }

Console.Clear();
Console.ReadKey();
}

}
}



but it says tat variable i is not assigned

解决方案

Except the fact that your algorithm is totally wrong...
4 / 2 != 0 (4 / 2 = 2) so 4 is a prime (according to your code)!!!


4 / 2 is 2 which is not 0 so your code is saying it is prime. Your code seems to be checking for odd\even rather than prime. There are mathematicians who have dedicated their lives to working out if numbers are prime, and (as for as I know) no solution has yet been found.

http://www.wikihow.com/Check-if-a-Number-Is-Prime[^]


First, lets review

if ((i / 2)!= 0)
{
Console.WriteLine(i + "\t is prime");

continue;


}



If i is 4 and you divide it by 2, you get 2 which is not 0 and hence you get into the if part of the condition.

Second, if you need to know whether your number is a prime number or not, your logic needs to be different.
You cannot divide by just 2 and find out. 9 for e.g. is not divisible by 2 but still not a prime number.
Prime numbers - https://en.wikipedia.org/wiki/Prime_number[^].


这篇关于检查给定的数字是否为素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆