素数例程 [英] prime number routine

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

问题描述

好的,这是一个家庭作业,但是你能不能帮帮我......我需要一个例程来判断用户输入的数字是否为素数

数字与否......所有我要求的不是确切的代码(也许是一个

一点)但逻辑或算法判断是否或者不是一个数字是

prime ....

解决方案

" don" < do*@panix.com>在消息中写道

新闻:c0 ********** @ reader2.panix.com ...

好的,这是一个家庭作业,但是你能不能帮助我
......我需要一个例程来确定用户输入的数字是否是
a素数还是不是......所有我都是要求是不是确切的代码(好吧
可能有点)但是判断数字
是否为素数的逻辑或算法....



以下是两个相关链接:

http://www.cse.iitk.ac.in/news/primality.html
http://www.parashift.com/c++-faq-lite/

HTH。


Jonathan


嗯,这实在是效率低下,但是


int i =输入的数字

int j = i - 1;

int prime = 1; // 0表示假,1表示真实

表示(j> 1; j--)

{

if((i / j == 0)

{

//不是素数

prime = 0;

break;

}

}

if(prime == 1)prime;


else {not prime }

" don"< do*@panix.com>写在消息中

news:c0 ********** @ reader2.panix .com ...

好的,这是一个家庭作业,但你能不能帮帮我......我需要一个例行程序来确定输入的数字是否是用户是一个
素数还是不......所有我要求的不是确切的代码(也许是
a少)但是逻辑或算法来判断是否数字是
黄金....



这不是最好的方法,但这里是一个想法:


取输入的数字除以所有数字除以及

包括该数字。


每次出现这个数字时,如果数量增加1,那么当它完成时如果

count大于2那么这个数字不是素数!


或者如果你不想这样做你可以修改它并包括

那个数字,每次mod为0然后加1到计数和如果计数

是> 2然后你知道它不是素数!!!


Shawn

don < do*@panix.com>在消息中写道

新闻:c0 ********** @ reader2.panix.com ...

好的,这是一个家庭作业,但是你能不能帮帮我......我需要一个例程来判断用户输入的数字是否是一个
的素数......所有我都是m要求的不是确切的代码(也许是
a少)但是用于判断一个数字是否为素数的逻辑或算法....


Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime
number or not...... all I''m asking for is Not the exact code ( well maybe a
little) but the logic or algorithm to tell whether or not a number is
prime....

解决方案

"don" <do*@panix.com> wrote in message
news:c0**********@reader2.panix.com...

Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I''m asking for is Not the exact code ( well maybe a little) but the logic or algorithm to tell whether or not a number is prime....



Here are two relevant links:

http://www.cse.iitk.ac.in/news/primality.html
http://www.parashift.com/c++-faq-lite/

HTH.

Jonathan


Well, this is really inefficient, but

int i = inputted number
int j = i - 1;
int prime = 1; //0 for false, 1 for true
for (j > 1; j--)
{
if ( (i/ j == 0)
{
//is not prime
prime = 0;
break;
}
}
if (prime == 1) prime;

else { not prime}
"don" <do*@panix.com> wrote in message
news:c0**********@reader2.panix.com...

Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime number or not...... all I''m asking for is Not the exact code ( well maybe a little) but the logic or algorithm to tell whether or not a number is
prime....



Well this isn''t the best way to do it but here is an idea:

Take the number that was input and divide it by all numbers up to and
including that number.

Each time the number comes out even add 1 to count and when it is done if
count is greater than 2 then the number is not prime!!!

Or if you do not want to do it that way you can mod it up to and including
that number and each time that the mod is 0 then add 1 to count and if count
is > 2 then you know it is not prime!!!

Shawn
"don" <do*@panix.com> wrote in message
news:c0**********@reader2.panix.com...

Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime number or not...... all I''m asking for is Not the exact code ( well maybe a little) but the logic or algorithm to tell whether or not a number is
prime....



这篇关于素数例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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