您的位置:首页 > 其它

project euler 47 Distinct primes factors

2018-01-17 14:26 323 查看

题目:

https://projecteuler.net/problem=47

题意:

Distinct primes factors

Problem 47

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

14=2×7

15=3×5

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

644=2²×7×23

645=3×5×43

646=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=22×7×23

645=3×5×43

646=2×17×19

首次出现连续四个数均有四个不同的质因数时,其中的第一个数是多少?

思路:

暴力枚举+分解质因子

代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 1000000 + 10;

bool is_prime
;
int prime
;

int get_prime_table(int n)
{
is_prime[0] = is_prime[1] = false;
for(int i = 2; i <= n; ++i)
is_prime[i] = true;
int k = 0;
for(int i = 2; i <= n; ++i)
{
if(is_prime[i])
{
prime[k++] = i;
for(int j = i*2; j <= n; j += i)
is_prime[j] = false;
}
}
return k;
}
bool check(int val)
{
int cnt = 0;
for(int i = 0; prime[i] <= val; ++i)
{
if(val % prime[i] == 0)
{
++cnt;
while(val % prime[i] == 0)
val /= prime[i];
}
}
return cnt == 4;
}
int main()
{
int n = 1000000;
get_prime_table(n);
int ans = 0, cnt = 0;
for(int i = 2; i <= n; ++i)
{
if(check(i))
{
++cnt;
if(cnt == 1)
ans = i;
else if(cnt == 4)
break;
}
else
cnt = 0;
}
printf("%d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: