zoukankan      html  css  js  c++  java
  • Project Euler Problem 47 Distinct primes factors

    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?


    C++(Faster):

    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <vector>
    
    using namespace std;
    
    const int MAXN = 4000;
    const int N = 1000000;
    
    int prime[MAXN+2] = {2, 3, 5};
    
    bool isprime(int n)
    {
        int q=sqrt(n), i=1;
    
        while(prime[i] <= q)
            if(n % prime[i] == 0)
                return false;
            else
                i++;
    
        return true;
    }
    
    void maketable(int n)
    {
        int val1 = 1, val5 = 5;
    
        for(int i=3; i<=n;) {
            val1 += 6;
            if(isprime(val1))
                prime[i++] = val1;
    
            val5 += 6;
            if(isprime(val5))
                prime[i++] = val5;
        }
    }
    
    int factorcount[N+1];
    
    void setval(int start, int k, int count)
    {
        for(int i=start; i<=N; i*=k) {
            if(i < 0)
                break;
            factorcount[i] = count;
        }
    }
    
    int next(int start)
    {
        for(;;) {
            while(factorcount[start])
                start++;
    
            if(factorcount[start+1] == 0 && factorcount[start+2] == 0 && factorcount[start+3] == 0)
                break;
    
            while(factorcount[start] == 0)
                start++;
        }
    
        return start;
    }
    
    int fact(int n, int fact[])
    {
        int i=0, j=0;
        while(n > 1) {
            if(i > MAXN) {
                fact[j++] = n;
                break;
            }
            while(n % prime[i] == 0) {
                fact[j] = prime[i];
                n /= prime[i];
            }
            if(fact[j])
                j++;
            i++;
        }
        return j;
    }
    
    int main()
    {
        vector<int> v;
        int factor[64];
    
        maketable(MAXN);
    
        memset(factorcount, 0, sizeof(factorcount));
    
        for(int i=0; i<=MAXN; i++)
            setval(prime[i], prime[i], 1);
    
        int i = 2, count;
        i = next(i);
        for(; i<N-4;) {
            memset(factor, 0, sizeof(factor));
    
            count = fact(i, factor);
            if(count != 4) {
                for(int j=0; j<count; j++)
                    setval(i, factor[j], count);
                i = next(i + 1);
            } else {
                v.push_back(i++);
                if(v.size() >= 4) {
                    if(v[0]+1 == v[1] && v[1]+1 == v[2] && v[2]+1 == v[3]) {
                        cout << v[0] << endl;
                        break;
                    } else
                        v.erase(v.begin());
                }
            }
        }
    
        return 0;
    }

    C++:
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <vector>
    
    using namespace std;
    
    const int MAXN = 4000;
    const int N = 1000000;
    
    int prime[MAXN+2] = {2, 3, 5};
    
    bool isprime(int n)
    {
        int q=sqrt(n), i=1;
    
        while(prime[i] <= q)
            if(n % prime[i] == 0)
                return false;
            else
                i++;
    
        return true;
    }
    
    void maketable(int n)
    {
        int val1 = 1, val5 = 5;
    
        for(int i=3; i<=n;) {
            val1 += 6;
            if(isprime(val1))
                prime[i++] = val1;
    
            val5 += 6;
            if(isprime(val5))
                prime[i++] = val5;
        }
    }
    
    int fact(int n)
    {
        int i=0, j=0;
        while(n > 1) {
            if(i > MAXN) {
                j = 1;
                break;
            }
            if(n % prime[i] == 0) {
                j++;
                while(n % prime[i] == 0)
                    n /= prime[i];
            }
            i++;
        }
        return j;
    }
    
    int main()
    {
        vector<int> v;
    
        maketable(MAXN);
    
        for(int i=1; i<=N; i++) {
            if(fact(i) == 4) {
                v.push_back(i);
                if(v.size() >= 4) {
                    if(v[0]+1 == v[1] && v[1]+1 == v[2] && v[2]+1 == v[3]) {
                        cout << v[0] << endl;
                        break;
                    } else
                        v.erase(v.begin());
                }
            }
        }
    
        return 0;
    }



  • 相关阅读:
    [Vue + TS] Using Route events inside Vue
    [Vue + TS] Write a Vue Component as a Class in TypeScript
    [Mobx] Use MobX actions to change and guard state
    [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5
    一年四个P(Project)
    android之IntentFilter的用法_Intent.ACTION_TIME_TICK在manifest.xml不起作用
    (step7.2.3)hdu 2554(N对数的排列问题——简单数论)
    hdu 1528 Card Game Cheater ( 二分图匹配 )
    SilkTest天龙八部系列5-类的属性
    SilkTest天龙八部系列6-用open agent进行测试
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564016.html
Copyright © 2011-2022 走看看