[Arrays]D. Liang 6.6 Revising Listing 4.11
Description:
Listing 4.11 determines whether a number n is prime by checking whether 2, 3, 4, 5, 6, …, n/2 is a divisor. If a divisor is found, n is not prime. A more efficient approach to determine whether n is prime is to check whether any of the prime numbers less than or equal sqrt(n) can divide n evenly. If not, n is prime. Rewrite 4.11 to display the first fifty prime numbers using this approach. You need to use an array to store the prime numbers and later use them to check whether they are possible divisors for n.
Input:
An integer n (1<=n<=10000).
Output:
The first n prime number with one blank to seperate them. Don’t output blank after the last prime number, output “\n” instead.
Sample Input:
12
Sample Output:
2 3 5 7 11 13 17 19 23 29 31 37
Programme:
//Date:2020/4/26
//Author:Kamenrider Justice
#include<stdio.h>
#include<math.h>
int isprime(int num,int a[],int size);
int main()
{
int n,i=3,count=1;//n是素数个数
scanf("%d",&n);
int primes[n];//储存素数的数组
primes[0]=2;//第一个素数是2,以此判断下一个数是不是素数
printf("%d",2);
while(count!=n)
{
if(isprime(i,primes,count))
{
primes[count]=i;
printf(" %d",i);
count++;
}
i++;
}
printf("\n");
return 0;
}
int isprime(int num,int a[],int size)//判断是否是素数
{
int i=0;
for(i=0;i<size;i++)//看是否能整除前面的素数
{
if(num%a[i]==0)
{
return 0;
}
}
return 1;
}
Python数据清洗实战入门