欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

玩转C语言:从1到100的素数挑战

最编程 2024-08-08 21:52:23
...

素数又称质数,有无限个。质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数的数称为质数。
接下来我们直接上代码:

#define _CRT_SECURE_NO_WARNINGS 10
#include<stdio.h>  
#include<stdlib.h>  
#include<assert.h>  
#include<math.h>  

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