欧拉计划 第四十七题

The first two consecutive numbers to have two distinct prime factors are:

14 = 2 × 715 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

644 = 2² × 7 × 23645 = 3 × 5 × 43646 = 2 × 17 × 19.

Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?

前两个连续数字有两个不同的素数因子是:

14 = 2×7 15 = 3×5

前三个连续数字有三个不同的素因子:

644 = 2 2×7×23 645 = 3×5×43 646 = 2×17×19。

找到前四个连续的整数,每个整数有四个不同的素数因子。这些数字中的第一个是什么?

思路

1.求素因子个数:同欧拉计划 第十二题

扫描二维码关注公众号,回复: 2113205 查看本文章

素数筛:如果本身是素数,那只有一个素因子;当i*prime[j] 时,如果 i 能整除prime[j],那么素因子个数等于i的素因子个数;如果不能整除,加一即可

2.筛选出连续四个素因子个数为4的数字

#include <stdio.h>

#define max 1000000

int prime[max + 5] = {0};
int factor[max + 5] = {0};

int main(){
	for(int i = 2; i < max; i++){
		if(!prime[i]){prime[++prime[0]] = i; factor[i] = 1;}
		for(int j = 1; j <= prime[0]; j++){
			if(i * prime[j] > max) break;
			prime[i * prime[j]] = 1;
			factor[i * prime[j]] = factor[i] + (i % prime[j] != 0);
			if(i % prime[j] == 0) break;
		}
	}
	for(int i = 1; i < max - 3; i++){
		if(factor[i] != 4) continue;
		if(factor[i + 1] != 4) continue;
		if(factor[i + 2] != 4) continue;
		if(factor[i + 3] != 4) continue;
		printf("%d\n",i);
		break;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38362049/article/details/81005448