project euler 47 Distinct primes factors
2018-01-17 14:26
323 查看
题目:
https://projecteuler.net/problem=47题意:
Distinct primes factorsProblem 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; }
相关文章推荐
- Project Euler:Problem 47 Distinct primes factors
- Project Euler:Problem 47 Distinct primes factors
- Project Euler Problem 47 Distinct primes factors
- Problem 47—Distinct primes factors
- Problem 47 Distinct primes factors (分解因子)
- 欧拉工程第47题:Distinct primes factors
- 10 Summation of primes - Project Euler
- hust Distinct Primes
- 【Project Euler】【Problem 10】Summation of primes
- Project Euler:Problem 29 Distinct powers
- project euler 29 Distinct powers
- ACM/ICPC 2011 Asia-Amritapuri Site / E Distinct Primes(求数的素因子)
- Distinct Primes 打表
- Project Euler 47
- SPOJ 10232. Distinct Primes
- Project Euler 010 Summation of primes
- live achive 5987 - Distinct Primes
- PE 47(Distinct primes factors-Python数组)
- Project Euler 10 Summation of primes
- project euler 10 Summation of primes