一、素数打表
/*
* 素数筛选,查找出小于等于MAXN的素数并连续存到prime[1...n]中
* prime[0]存素数的个数,初始为0
*/
#include<stdio.h>
#include<string.h>
const int MAXN = ;
int prime[MAXN + ];
int vis[MAXN+];
int cnt=;
void getPrime() {
memset(prime, , sizeof(prime));
for (int i = ; i <= MAXN; i++) {
if (!prime[i]) {
prime[++prime[]] = i;
}
for (int j = ; j <= prime[] && prime[j] <= MAXN / i; j++) {
prime[prime[j] * i] = ;
if (i % prime[j] == ) {
break;
}
}
}
}
//这两个素数打表函数任选一个即可
void getPrime() { //借助vis数组标记
memset(prime,,sizeof(prime));
memset(vis,,sizeof(vis));
for(long long i=; i<=MAXN; i++) {
if(!vis[i]) {
prime[++cnt]=i;
}
for(long long j=i*i; j<=MAXN; j+=i) {
vis[j]=;
}
}
}
int main() {
//相关操作......
return ;
}
二、合数分解
#include<stdio.h>
#include<string.h>
const int MAXN = ;
int prime[MAXN + ];
long long factor[][];
int fatCnt;
// 获取素数
void getPrime() {
memset(prime, , sizeof(prime));
for (int i = ; i <= MAXN; i++) {
if (!prime[i]) {
prime[++prime[]] = i;
}
for (int j = ; j <= prime[] && prime[j] <= MAXN / i; j++) {
prime[prime[j] * i] = ;
if (i % prime[j] == ) {
break;
}
}
}
return ;
}
// 合数分解
int getFactors(long long x) {
fatCnt = ;
long long tmp = x;
for (int i = ; prime[i] <= tmp / prime[i]; i++) {
factor[fatCnt][] = ;
if (tmp % prime[i] == ) {
factor[fatCnt][] = prime[i];
while (tmp % prime[i] == ) {
factor[fatCnt][]++;
tmp /= prime[i];
}
fatCnt++;
}
}
if (tmp != ) {
factor[fatCnt][] = tmp;
factor[fatCnt++][] = ;
}
return fatCnt;
}
int main() {
//相关操作......
return ;
}