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

问题描述

我需要开发一个代码,找到介于2和

100000之间的正确数字。并打印一行文本,指示是否为int。是对的

prime。我正在开始编程这么复杂很复杂。我们在函数章节中是
。我找到了素数的平方根。

到目前为止我已经完成了这个但是我不知道如何将它除以

十,这样我才能找到合适的素数。我正在使用c。


#include< stdio.h>

#include< math.h>

int slowPrime(INT); //函数原型


int main()

{

int x; //循环计数器

int count = 0; //找到素数的总数

长素数;

printf_s(" 1到10000的素数是:\ n");

for(x = 2; x< = 2147483647; x ++)

{

if(Prime(x))

{

++ count; // count和print prime

// printf_s("%10d",x);


if(count%10 == 0)// 10个值后的新行显示

printf_s(" \ n");

} //结束

} //结束if



printf_s("%d prime numbers were\\\
,count);


返回0; //表示成功终止

} //结束主


int isRightPrime(int n)//如果n为素数,则慢素数返回1

{

int i; //循环计数器


for(i = 2; i< =(int)sqrt(n); i ++)

{

if(n%i == 0)

返回0;


} //结束


返回1;


} //结束函数素数


i知道这不会像我做的那么复杂。 />

i need to develop a code that finds a prime right number between 2 and
100000. and print one line of text that indicates if the int. is right
prime. i am in beginning programing so complex is complicated. we are
on a functions chapter. i am usung the square root to find the prime.
so far i have accomplished this but i cant fgure how to divide this by
ten so i can find the right prime. i am using c.

#include <stdio.h>
#include <math.h>
int slowPrime(int); //function prototype

int main()
{
int x; // loop counter
int count = 0; // total numbers of prime found
long prime;
printf_s("The prime numbers from 1 to 10000 are:\n" );
for ( x = 2; x <= 2147483647 ; x++ )
{
if ( Prime( x ) )
{
++count; // count and print prime
//printf_s("%10d", x );

if ( count % 10 == 0 ) // new line after 10 values diplayed
printf_s( "\n" );
} // end for
} // end if


printf_s("%d prime numbers were found\n", count);

return 0; // indicate successful termination
} // end main

int isRightPrime( int n ) // slow prime returns 1 if n is prime
{
int i; // loop counter

for ( i = 2; i <= (int)sqrt (n); i++ )
{
if ( n % i == 0 )
return 0;

} // end for

return 1;

} // end function prime

i know this can not be as complicated as i have made it.

推荐答案



是正确的素数不同于prime?

Is "right prime" different from "prime"?



您的缩进被新闻阅读器搞砸了;我已经在下面恢复了正确的

缩进。

Your indentation got messed up by your newsreader; I''ve restored proper
indentation below.



''printf_s''当然应该是''printf''。

''printf_s'' should be ''printf'', of course.



如果INT_MAX(

的最大可能值),此循环何时终止''int'')是32767?如果INT_MAX是2147483647怎么办?请记住,所有的整数

根据定义最多为INT_MAX ---没有大于

INT_MAX的整数。


另外,你可能会考虑甚至有多少素数,然后

相应地修改你的程序。

When will this loop terminate if INT_MAX (the maximum possible value of
an ''int'') is 32767? What if INT_MAX is 2147483647? Remember, all ints
are by definition at most INT_MAX --- there are no ints greater than
INT_MAX.

Also, you might consider how many even primes there are, and then
modify your program accordingly.



我还没有看到'Prime'的原型。您应该在

计划的顶部或至少在第一个之前的某个地方为所有功能(通常是主要除外)添加声明

使用。

I haven''t seen a prototype for ''Prime'' yet. You should put declarations
for all your functions (except, usually, ''main'') at the top of your
program, or at least somewhere prior to their first use.



和以前一样,考虑只有一个偶数素数。查看

Sierat of Eratosthenes,如果你还没有。


另请注意i< =(int) SQRT(N)"基本上测试的是i * i< = n的相同

的东西。您认为这两个表达式中的哪一个会在现实世界的计算机上执行得更快?

As before, consider that there is only one even prime. Look up the
"Sieve of Eratosthenes", if you haven''t yet.

Also notice that "i <= (int)sqrt(n)" is basically testing the same
thing as "i*i <= n". Which of those two expressions do you think would
execute faster on a real-world computer?



''Prime'的定义是什么?我还注意到函数

''isRightPrime''从未使用过。

Where''s the definition of ''Prime''? I also notice that the function
''isRightPrime'' was never used.



模数所有这些基本错误和错误,你已经得到了基本的

天真算法进行主要测试。使用它没有任何问题

的小数字(比如,数字小于2 ^ 32)。对于更大的数字,

有更快的算法。


-Arthur

Modulo all those elementary errors and bugs, you''ve got the basic
naive algorithm for prime testing. There''s nothing wrong with using it
for tiny numbers (say, numbers less than 2^32). For bigger numbers,
there are faster algorithms.

-Arthur




是的,我对你的问题还不太清楚。

但是我可以提供一些可能的东西很有用。

1:

for(x = 2; x< = 2147483647; x ++)

浪费周期检查偶数素数。

试试这个

for(x = 3; x< = 2147483647; x + = 2)

你已经知道2是素数在for循环开始之前你的其他

的东西也是如此


2:

这是正确的权利定义吗素数?

"如果N是素数且所有通过连续移除N的最右边数字获得的数字都是素数,那么它是一个右素数。基于这个应该这样做的



(未经测试,我的头顶,所以它可能是错误的)

Eric


const int true = 1;

const int false = 0;

int IsRightPrime( unsigned long long int Prim eN>)
{

char Prime [33]; //将此值设置为PrimeNumber中的最大数字+ 1

int i,lastchar;

unsigned long long int testNum;


sprintf(Prime,"%lld",PrimeNumber)

lastchar = strlen(Prime)-1;

for(i = lastchar; i> 0; i-- ){

Prime [i] = 0; //删除最后一位数

testNum = strtoull(Prime [i],NULL,10);

if(!Prime(testNum))返回false

}

返回true;

}

// ---------------- ----------------------------------------------

True, and I''m still a little unclear as to your question.
But I can offer a couple of things that might be usefull.
1:
for ( x = 2; x <= 2147483647 ; x++ )
is wasting cycles checking even numbers for primes.
try this
for ( x = 3; x <= 2147483647 ; x+=2 )
you already know 2 is prime so do your other
stuff on it before the for loop begins

2:
Is this the correct definition of a right prime?
"if N is prime and all numbers obtained by successively removing the
rightmost digits of N are prime then its a "right prime"."

based on that here''s a fn that should do it
(its untested, off the top of my head, so it might be buggy)
Eric

const int true = 1;
const int false = 0;
int IsRightPrime(unsigned long long int PrimeNumber)
{
char Prime[33]; // set this to max digits in PrimeNumber + 1
int i, lastchar;
unsigned long long int testNum;

sprintf(Prime, "%lld", PrimeNumber)
lastchar = strlen(Prime)-1;
for(i=lastchar; i>0; i--) {
Prime[i] = 0; // drop last digit
testNum = strtoull(Prime[i], NULL, 10);
if(!Prime(testNum)) return false
}
return true;
}
//--------------------------------------------------------------




是的,我对你的问题还不太清楚。

但是我可以提供一些可能的东西很有用。

1:

for(x = 2; x< = 2147483647; x ++)

浪费周期检查偶数素数。

试试这个

for(x = 3; x< = 2147483647; x + = 2)

你已经知道2是素数在for循环开始之前你的其他

的东西也是如此


2:

这是正确的权利定义吗素数?

"如果N是素数且所有通过连续移除N的最右边数字获得的数字都是素数,那么它是一个右素数。基于这个应该这样做的



(未经测试,我的头顶,所以它可能是错误的)

Eric


const int true = 1;

const int false = 0;

int IsRightPrime( unsigned long long int Prim eN>)
{

char Prime [33]; //将此值设置为PrimeNumber中的最大数字+ 1

int i,lastchar;

unsigned long long int testNum;


sprintf(Prime,"%lld",PrimeNumber)

lastchar = strlen(Prime)-1;

for(i = lastchar; i> 0; i-- ){

Prime [i] = 0; //删除最后一位数

testNum = strtoull(Prime [i],NULL,10);

if(!Prime(testNum))返回false

}

返回true;

}

// ---------------- ----------------------------------------------


True, and I''m still a little unclear as to your question.
But I can offer a couple of things that might be usefull.
1:
for ( x = 2; x <= 2147483647 ; x++ )
is wasting cycles checking even numbers for primes.
try this
for ( x = 3; x <= 2147483647 ; x+=2 )
you already know 2 is prime so do your other
stuff on it before the for loop begins

2:
Is this the correct definition of a right prime?
"if N is prime and all numbers obtained by successively removing the
rightmost digits of N are prime then its a "right prime"."

based on that here''s a fn that should do it
(its untested, off the top of my head, so it might be buggy)
Eric

const int true = 1;
const int false = 0;
int IsRightPrime(unsigned long long int PrimeNumber)
{
char Prime[33]; // set this to max digits in PrimeNumber + 1
int i, lastchar;
unsigned long long int testNum;

sprintf(Prime, "%lld", PrimeNumber)
lastchar = strlen(Prime)-1;
for(i=lastchar; i>0; i--) {
Prime[i] = 0; // drop last digit
testNum = strtoull(Prime[i], NULL, 10);
if(!Prime(testNum)) return false
}
return true;
}
//--------------------------------------------------------------



ahem :(在发布后稍微盯着它...)


char Prime [33]; //将此值设置为PrimeNumber中的最大数字+ 1

int i,lastchar;

unsigned long long int testNum;


sprintf(Prime,"%lld",PrimeNumber);

lastchar = strlen(Prime)-1;

for(i = lastchar; i> 0; i- - ){

Prime [i] = 0; //删除最后一位数

testNum = strtoull(Prime,NULL,10);

if(!IsPrime(testNum))返回false;

}

返回true;

}

ahem: (after staring at it a bit after posting...)

char Prime[33]; // set this to max digits in PrimeNumber + 1
int i, lastchar;
unsigned long long int testNum;

sprintf(Prime, "%lld", PrimeNumber);
lastchar = strlen(Prime)-1;
for(i=lastchar; i>0; i--) {
Prime[i] = 0; // drop last digit
testNum = strtoull(Prime, NULL, 10);
if(!IsPrime(testNum)) return false;
}
return true;
}


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

10-13 19:16