Project Euler #47: Distinct primes factors

Sort by

recency

|

29 Discussions

|

  • + 0 comments

    Python

    In python 0 is false and 1 is true

    def sieve(n):
        arr = [0] * n
        
        for i in range(2, n):
            if arr[i] : continue
            
            for j in range(i, n, i):
                arr[j] += 1
        
        return arr
    
    N, K = map(int, input().split())
    sieve = sieve(N + K)
    ret = []
    
    for i in range(N + 1):
        curr = sieve[i]
        
        if curr == K:
            if all(j == K for j in sieve[i + 1: i + K]):
                ret.append(i)
                
    [print(i) for i in ret]
    
  • + 0 comments

    JAva Code

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
      private static boolean[] prime = null;
        
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int k = in.nextInt();
            int limit = 2500000;
            int root = (int) Math.ceil(Math.sqrt(limit));
            prime = generatePrimes(root, limit);
    
            for(int i=2;i<=n;i++)
            {
                ArrayList<TreeSet> list = new ArrayList<TreeSet>();
                if(!prime[i])
                {
                    for(int j=0;j<k;j++)
                    {
                        if(!prime[i+j])
                        {
                            TreeSet s = primeFactorize(i+j);
                            if(s.size()==k)
                                list.add(s);
                            else break;
                        }
                        else break;
                    }
                }
                if(list.size()==k)
                    System.out.println(i);
            }
        }
        
        public static TreeSet<Integer> primeFactorize(Integer n)
        {
            try
            {
                TreeSet<Integer> set = new TreeSet<Integer>();
                for(int i=2;i<=n/i;i++)
                {
                    while(n%i==0)
                    {
                        set.add(i);
                        n/=i;
                    }
                }
                if(n>1) set.add(n);
                return set;
            }
            catch(Exception e)
            {
                throw e;
            }
        }
        
        public static boolean[] generatePrimes(int root, int limit)
        {
            boolean[] prime = new boolean[limit+1];
            prime[2] = true;
            prime[3] = true;
    
            //Sieve of Atkin for prime number generation
            for (int x = 1; x < root; x++)
            {
                for (int y = 1; y < root; y++)
                {
                    int n = 4 * x * x + y * y;
                    if (n <= limit && (n % 12 == 1 || n % 12 == 5))
                        prime[n] = !prime[n];
    
                    n = 3 * x * x + y * y;
                    if (n <= limit && n % 12 == 7)
                        prime[n] = !prime[n];
    
                    n = 3 * x * x - y * y;
                    if ((x > y) && (n <= limit) && (n % 12 == 11))
                        prime[n] = !prime[n];
                }
            }
    
            for (int i = 5; i <= root; i++)
            {
                if (prime[i])
                {
                    for (int j = i * i; j < limit; j += i * i)
                    {
                        prime[j] = false;
                    }
                }
            }
    
            return prime;
            }
    }
    
  • + 0 comments

    After a day of googling, I had realized this:

    If you use common codes to find prime factors like here: https://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/ , you cannot pass all test cases coz its time complexity = O(n^0.5) (I though it was the fastest algorithm ever but I was wrong)

    If you use a more efficient algorithm like here: https://www.geeksforgeeks.org/efficient-program-print-number-factors-n-numbers/?ref=rp , you can pass coz it's only O(logn)

  • + 0 comments

    C++ Solution :

    #include <bits/stdc++.h>
    using namespace std;
    #define all(v) (v).begin(), (v).end()
    #define debug(x) cout << #x << " = " << x << endl
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    inline ll Mod(ll x, ll mod) { return x % mod >= 0 ? x % mod : x % mod + mod; }
    
    int is_prime[2000 * 1000 + 1] = {0};
    void sieve(int N)
    {
        for (int i = 2; i <= N; ++i)
        {
            if (!is_prime[i])
            {
                for (int j = i; j <= N; j += i)
                    is_prime[j]++;
            }
        }
    }
    
    int main()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        sieve(2000 * 1000);
        int n, k;
        cin >> n >> k;
        for (int i = 2; i <= n; i++)
        {
            bool flag = 0;
            for (int j = 0; j < k; j++)
            {
                if (is_prime[i + j] != k)
                {
                    flag = 1;
                    break;
                }
            }
            if (!flag)
                cout << i << '\n';
        }
    }
    
  • + 0 comments

    First find the number of different prime factors of all numbers in range [2,2000000] using an algorithm similar to sieve of eratosten. Then for each test case, iterate over all numbers from 2 to N and check is there is an answer or not.