不熟悉这个, 给你一个链接

来源: 秋月冬雪 2014-12-04 08:19:30 [] [博客] [旧帖] [给我悄悄话] 本文已被阅读: 0 次 (19754 bytes)
本文内容已被 [ 秋月冬雪 ] 在 2014-12-04 08:23:01 编辑过。如有问题,请报告版主或论坛管理删除.
回答: scilabSo_Be_It2014-12-04 08:02:55
给你找到这个

www.programmingsimplified.com/c/source-code/c-program-for-prime-number

Prime number program in c: c program for prime number, this code prints prime numbers using c programming language. To check whether a number is prime or not see another code below. Prime number logic: a number is prime if it is divisible only by one and itself. Remember two is the only even and also the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13, 17....etc. Prime numbers have many applications in computer science and mathematics. A number greater than one can be factorized into prime numbers, For example 540 = 22*33*51

Prime number program in c language

#include<stdio.h>
 
int main()
{
   int n, i = 3, count, c;
 
   printf("Enter the number of prime numbers required\n");
   scanf("%d",&n);
 
   if ( n >= 1 )
   {
      printf("First %d prime numbers are :\n",n);
      printf("2\n");
   }
 
   for ( count = 2 ; count <= n ;  )
   {
      for ( c = 2 ; c <= i - 1 ; c++ )
      {
         if ( i%c == 0 )
            break;
      }
      if ( c == i )
      {
         printf("%d\n",i);
         count++;
      }
      i++;
   }
 
   return 0;
}

Download Prime number program.

Output of program:
Prime number c program

C program for prime number or not

#include<stdio.h>
 
main()
{
   int n, c = 2;
 
   printf("Enter a number to check if it is prime\n");
   scanf("%d",&n);
 
   for ( c = 2 ; c <= n - 1 ; c++ )
   {
      if ( n%c == 0 )
      {
         printf("%d is not prime.\n", n);
	 break;
      }
   }
   if ( c == n )
      printf("%d is prime.\n", n);
 
   return 0;
}

C program for prime number using function

#include<stdio.h>
 
int check_prime(int);
 
main()
{
   int n, result;
 
   printf("Enter an integer to check whether it is prime or not.\n");
   scanf("%d",&n);
 
   result = check_prime(n);
 
   if ( result == 1 )
      printf("%d is prime.\n", n);
   else
      printf("%d is not prime.\n", n);
 
   return 0;
}
 
int check_prime(int a)
{
   int c;
 
   for ( c = 2 ; c <= a - 1 ; c++ )
   { 
      if ( a%c == 0 )
	 return 0;
   }
   if ( c == a )
      return 1;
}

There are many logic to check prime numbers, one given below is more efficient then above method.
for ( c = 2 ; c <= (int)sqrt(n) ; c++ )
Only checking from 2 to square root of number is sufficient.
There are many more efficient logic available.

所有跟帖: 

谢谢!我是一窍不通的,等他回来我给他看你的建议。 -So_Be_It- 给 So_Be_It 发送悄悄话 So_Be_It 的博客首页 (0 bytes) () 12/04/2014 postreply 08:22:06

我找到一个网上的例子了。别看我的了。修改了内容。 -秋月冬雪- 给 秋月冬雪 发送悄悄话 秋月冬雪 的博客首页 (33 bytes) () 12/04/2014 postreply 08:23:42

從數學來說好像可以優化一點 -violinpiano- 给 violinpiano 发送悄悄话 (132 bytes) () 12/04/2014 postreply 08:30:31

是的,这样优化得多。 -秋月冬雪- 给 秋月冬雪 发送悄悄话 秋月冬雪 的博客首页 (6 bytes) () 12/04/2014 postreply 08:31:21

算法可以优化: -heure- 给 heure 发送悄悄话 (304 bytes) () 12/04/2014 postreply 08:31:33

请您先登陆,再发跟帖!

发现Adblock插件

如要继续浏览
请支持本站 请务必在本站关闭/移除任何Adblock

关闭Adblock后 请点击

请参考如何关闭Adblock/Adblock plus

安装Adblock plus用户请点击浏览器图标
选择“Disable on www.wenxuecity.com”

安装Adblock用户请点击图标
选择“don't run on pages on this domain”