最近在学c++,看到一种神奇的算法可以秒解10亿以内的所有素数,据说用了指针(还没学到,嘤),看不懂,姑且抄了下来,改吧改吧变成一个实现因式分解的程序
#include<stdio.h>
#include<memory.h>
#include<malloc.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#define max 10000
using namespace std;
#define IDX(i) ((i)/2/sizeof(unsigned int))
#define OFF(i) ((i)/2%sizeof(unsigned int))
#define ISPRIME(i) (isprimebits[IDX(i)]&(1UL<<OFF(i)))
#define CLEARPRIME(i) (isprimebits[IDX(i)]&=~(1UL<<OFF(i)))
int main(){
int n, i, j;
cin >> n;
unsigned int *isprimebits;
isprimebits = (unsigned int *)
malloc((IDX(n) + 1)*sizeof(unsigned int));
memset(isprimebits, -1, (IDX(n) + 1)*sizeof(unsigned int));
for (i = 3; i <= n / 2; i += 2){
if (ISPRIME(i)){
for (j = 3 * i; j <= n; j += 2 * i){
CLEARPRIME(j);
}
}
}
//以上就是求取n以内所有素数的方式,对于3到n以内的