求质数

来源:互联网 发布:7.1声道耳机推荐 知乎 编辑:程序博客网 时间:2024/05/15 12:19

动态内存分配

  • 如果一个数不是质数,他必定能被比它小的质数整数。
  • 只能被1和它本身整除的数都是质数
  • 质数都是奇数
#define _STDC_WANT_LIB_EXT1_ 1#include <stdlib.h>#include <stdio.h>#include <stdbool.h>int main(){    unsigned long long *pPrimes = NULL;    unsigned  long long trial = 0;    bool found = false;    int count = 0;    int total = 0;    printf("How many primes would you like -you will get at least 4?");    scanf_s("%d",&total);    total = total < 4 ? 4 : total;    pPrimes = (unsigned long long*)malloc(total * sizeof(unsigned long long));    if (!pPrimes)    {        printf("Not enough memory.It's the end  I'm afrad.\n");        return 1;    }    *pPrimes = 2ULL;    *(pPrimes + 1) = 3ULL;    *(pPrimes + 2) = 5ULL;    count = 3;    trial = 5ULL;    while (count < total)    {        trial += 2ULL;//只要能被整除就就跳出循环        for (int i = 1; i < count; ++i)        {            if (!(found = (trial % *(pPrimes + i))))                break;        }        if (found)            *(pPrimes + count++) = trial;    }    for (int i = 0; i < total; ++i)    {        printf("%12llu",*(pPrimes + i));        if (!((i + 1) % 5))            printf("\n");    }    printf("\n");    free(pPrimes);    pPrimes = NULL;    return 0;}
  • 指针变量引用一块用于存储所计算的质数的内存区。
  • 运算符的前置还是后置?
  • 取模运算判断是否被整除
  • 检查请求的动态内存是否已经分配。
  • *(指针+i)表示引用一块连续的内存。
  • M % N == 0则表示能被整除
  • malloc()函数需指定要分配的内存的字节数。
  • calloc()把内存分配为给定大小的数组,calloc()函数初始化了多分配的内存,所有的位都是0;
  • *(p+n)是给p中的地址加上整数n,再对得到的地址取消引用。
  • p设置为multiple的地址,p+n 就等于multiple +n;d
  • multiple[]和*(multiple+n)相同

C语言的内存分配的方式是什么呢?

  • 静态存储区域分配。内存在程序编译的时候就已经分配好了,这块内存在程序的整个运行期间都存在
  • 在栈上创建。在执行函数时,函数内部变量的存储单元都可以在栈上创建,函数执行结束时,这些存储单元自动被释放。占内存分配运算内置于处理器的指令集中,效率很高。但是分配的内存容量有限。
  • 从堆上分配,亦称为动态内存分配,程序在运行的时候,用malloc()申请任意多少的内存,程序员自己负责在何时用free或delete释放内存,动态内存的生存期由程序员决定。

计算机的内存被划分为一个个存储单元,其中的每个存储单元是以字节为单位的,每个存储单元都有自己的编号,计算机通过编号可以访问相应的存储单元的内容,这个编号就是内存。

扩展动态分配的内存

用户给要计算的质数范围指定上限

#define _STDC_WANT_LIB_EXT1_ 1#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#define CAP_INCR 10int main(){    unsigned long long *pPrimes = NULL;    bool found = false;    unsigned long long limit = 0LL;    int count = 0;    printf("Enter the upper limit for primes you want to find: ");    scanf("%llu", &limit);    size_t capacity = 10;    //指定数组大小的    pPrimes = (unsigned long long *)calloc(capacity, sizeof(unsigned long long));    if (!pPrimes)    {        printf("Not enough memory.It's the end I'm afraid.\n");        return 1;    }    *pPrimes = 2ULL;    *(pPrimes + 1) = 3ULL;    *(pPrimes + 2) = 5ULL;    count = 3;    unsigned long long trial = *(pPrimes + 2) + 2ULL;    unsigned long long *pTemp = NULL;    while (trial <= limit)    {        for (int i = 1; i < count; ++i)        {            if (!(found = (trial % *(pPrimes))))                break;        }        if (found)        {            if (count == capacity)            {                capacity += CAP_INCR;                pTemp = (unsigned long long *)realloc(pPrimes, capacity * sizeof(unsigned long long));                if (!pTemp)                {                    printf("Unfortunately memory realloction failed.\n");                    free(pPrimes);                    pPrimes = NULL;                    return 2;                }                pPrimes = pTemp;            }            *(pPrimes + count++) = trial;        }        trial += 2ULL;    }    printf("%d primes found up to %llu: \n", count, limit);    for (int i = 0; i < count; ++i)    {        printf("%12llu", *(pPrimes + i));        if (!((i + 1) % 5))            printf("\n");    }    printf("\n");    free(pPrimes);    pPrimes = NULL;    return 0;}

输入一个数,判断这个数是不是素数?

#include <stdio.h>int main(){    int num, isPrime = 1;    scanf("%d",&num);    for (int i = 2; i < num; i++)    {        if (num %i == 0)            isPrime = 0;    }    if (isPrime)        printf("%d是素数\n", num);    else        printf("%d不是素数\n。",num);    return 0;}
0 0
原创粉丝点击