[英]C++ question on prime numbers
I am trying to make a program that determines if the number is prime or composite. 我正在尝试制作一个确定数字是素数还是复合数的程序。 I have gotten thus far.
我到目前为止。 Could you give me any ideas so that it will work?
您能给我些什么想法,以使其起作用吗? All primes will , however, because composites have values that are both r>0 and r==0, they will always be classified as prime.
但是,所有素数都将是因为复合材料的值都为r> 0且r == 0,因此它们将始终被归类为素数。 How can I fix this?
我怎样才能解决这个问题?
int main()
{
int pNumber, limit, x, r;
limit = 2;
x = 2;
cout << "Please enter any positive integer: " ;
cin >> pNumber;
if (pNumber < 0)
{
cout << "Invalid. Negative Number. " << endl;
return 0;
}
else if (pNumber == 0)
{
cout << "Invalid. Zero has an infinite number of divisors, and therefore neither composite nor prime." << endl;
return 0;
}
else if (pNumber == 1)
{
cout << "Valid. However, one is neither prime nor composite" << endl;
return 0;
}
else
{
while (limit < pNumber)
{
r = pNumber % x;
x++;
limit++;
if (r > 0)
cout << "Your number is prime" << endl;
else
{
cout << "Your number is composite" << endl;
return 0;
}
}
}
return 0;
}
Check out http://en.wikipedia.org/wiki/Prime_number and http://en.wikipedia.org/wiki/Primality_test 查看http://en.wikipedia.org/wiki/Prime_number和http://en.wikipedia.org/wiki/Primality_test
The simplest primality test is as follows: Given an input number n, check whether any integer m from 2 to n − 1 divides n.
最简单的素数测试如下:给定输入数n,检查从2到n − 1的整数m是否除以n。 If n is divisible by any m then n is composite, otherwise it is prime.
如果n可被任何m整除,则n为合成数,否则为质数。
#include <iostream>
#include <math.h>
// Checks primality of a given integer
bool IsPrime(int n)
{
if (n == 2) return true;
bool result = true;
int i = 2;
double sq = ceil(sqrt(double(n)));
for (; i <= sq; ++i)
{
if (n % i == 0)
result = false;
}
return result;
}
int main()
{
std::cout << "NUMBER" << "\t" << "PRIME" << std::endl;
for (unsigned int i = 2; i <= 20; ++i)
std::cout << i << "\t" << (IsPrime(i)?"YES":"NO") << std::endl;
std::cin.get();
return 0;
}
bool check_prime(unsigned val) {
if (val == 2)
return true;
// otherwise, if it's even, it's not prime.
if ((val & 1) == 0)
return false;
// it's not even -- only check for odd divisors.
for (int i=3; i*i<=val; i+=2)
if (val % i == 0)
return false;
return true;
}
with respect ur code u didn't check if i enter 2 what will be happened, and also u didn't return any thing if it is prime....thats why it is always returning prime in spite the number is composite . 关于您的代码,您没有检查我是否输入2将会发生什么,并且如果它是质数,您也没有返回任何东西。 here is the code below =>
这是下面的代码=>
#include<iostream>
using namespace std;
int main(){
int pNumber, limit, x, r;
limit = 2;
x = 2;
cout << "Please enter any positive integer: " ;
cin >> pNumber;
if (pNumber < 0){
cout << "Invalid. Negative Number. " << endl;
return 0;
}
else if (pNumber == 0){
cout << "Invalid. Zero has an infinite number of divisors, and therefore neither composite nor prime." << endl;
return 0;
}
else if (pNumber == 1){
cout << "Valid. However, one is neither prime nor composite" << endl;
return 0;
}
else if (pNumber == 2){
cout << " Your number is prime" << endl;
return 0;
}
else{
while (limit < pNumber){
r = pNumber % x;
x++;
limit++;
if (r > 0){
cout << "Your number is prime" << endl;
return 0;
}
else{
cout << "Your number is composite" << endl;
return 0;
}
}
}
return 0;
}
For one thing, you'll want to break out of your loop when you find some x
where pNumber % x == 0
. 一方面,当您找到一些
x
,其中pNumber % x == 0
时,您会希望跳出循环。 All you need to do is find one factor of pNumber
greater than 1 and less than pNumber
to prove it's not prime -- no point in searching further. 您需要做的就是找到
pNumber
大于1且小于pNumber
一个因子以证明它不是素数-进一步搜索毫无意义。 If you get all the way to x = pNumber
without finding one, then you know pNumber
is prime. 如果一路找到
x = pNumber
却没有找到一个,则说明pNumber
是质数。 Actually, even if you get to the square root of pNumber
without finding one, it's prime, since if it has a factor greater than that, it should have a factor less than that. 实际上,即使您未找到
pNumber
平方根,它也是质数,因为如果pNumber
的因数大于该因数,则它的因数应小于该因数。 Make sense? 说得通?
I don't know what you have been taught thus far, but my discrete mathematics teacher was a fan of the Miller-Rabin test . 我目前为止还不知道您所教的是什么,但是我的离散数学老师是Miller-Rabin考试的粉丝。 It is a pretty accurate test that is very easy to code, within a few base tests you have a very negligible chance that you have a Carmichael Number .
这是一个非常准确的测试,非常容易编写代码,在一些基本测试中,您拥有Carmichael Number的机会非常小。 If you haven't gotten that far in your studies I would just stick to some basic division rules for numbers.
如果您的学习还没到那儿,我会坚持一些基本的数字除法规则。
最简单的方法是对于给定的数n,如果它可以被2到sqrt(n)之间的任何数完全可整除,则它可以是复合数,也可以是素数
Hi i have done this that also without using math.h header file....Have used turboc compiler. 嗨,我做到了这一点,也没有使用math.h头文件。...已经使用了turboc编译器。 Following program checks whether the number is prime or composite.
以下程序检查数字是素数还是复合数。
#include<iostream.h>
#include<conio.h>
class prime
{
int a;
public:
void check();
};
void prime::check()
{
cout<<"Insert a number";
cin>>a;
int count=0;
for(int i=a;i>=1;i--)
{
if(a%i==0)
{
count++;
}
}
if(count==1)
{
cout<<"\nOne is neither prime nor composite";
}
if(count>2)
{
cout<<"\nThe number is composite " ;
}
if(count==2)
{
cout<<"\nThe numner is prime";
}
}
void main()
{
clrscr();
prime k;
k.check();
getch();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.