GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

All numbers with minimum prime factors as X

Given a prime number X, you need to give the count of all numbers in range 1-10^6 which have minimum prime factor X.

 

For ex:

input: 2

outpot: 500000 //because [2,4,6,8...]

input: 11

output: 20779 //becuase [11, 121, 143...]

 

Please help me with solving this question.

This is my approach:

def new_prime():
    T = int(raw_input())
    for t in xrange(T):
        X = int(raw_input())
        cnt = 0
        for num in xrange(X, 10**6, X):
            flag = 0
            if num < 4 and num == X:
                cnt += 1
                flag = 1
            for j in xrange(2, int(sqrt(num))+1):
                if num%j==0:
                    if j == X:
                        cnt += 1
                    flag = 0
                    break
                flag = 1
            if flag:
                cnt += 1

        print(cnt)


if __name__ == '__main__':
    new_prime()

but it is dead slow and I doubt it gives right answer for all the cases.

asked Oct 31, 2015 by Amit
How about this one?
def check_prime(number):
    # 2, 3 is prime by default
    if number == 1:
        return False, 1
    elif number < 4:
        return True, number
    else:
        i = 0
        max_range = int(sqrt(number))+1
        # Check whether its a prime or not
        for i in xrange(2, max_range):
            if number % i == 0:
                return False, i
        return True, i

if __name__ == '__main__':
    from math import sqrt
    # Number of test cases
    total_test_cases = int(raw_input())
    for t in xrange(total_test_cases):
        count = 0
        # get the prime number
        test_prime = int(raw_input())
        if check_prime(test_prime)[0]:
            count = 1
        else:
            count = 0
        for num in xrange(test_prime, 10**6, test_prime):
            result = check_prime(num)
            if result[1] == test_prime:
                count += 1

        print count

 

3 Answers

First , Find the minimum prime of all the numbers from 1 to 10^6. Store it in an array. In my program this array is min_prime. min_prime[i] gives, the smallest prime of i.

When you want to find count of the numbers whose minimum prime is n, then run a for loop with iterator i from 2 to 10^6,  if mim_prime of i is equals to n, then increase count by 1.

#include <stdio.h>

int min_prime[1000001]={0};// Initially min_prime for all the numbers is 0.
 
int main( void ) {
    int t,i,j,n,cnt;
 	for(i=2;i<=1000000;i++){
		if(!min_prime[i]){ // if i does not have any factor less than i.
			min_prime[i]=i; 
			for(j=2;(i*j)<=1000000;j++){ // for all multiples of i where i*j is multiple of i.
				if(!min_prime[i*j])      // if i*j does not have any prime factor less than i.
					min_prime[i*j]=i;    // then min_prime for i*j will be i.
			}
		}
	}
 	scanf("%i", &t);
	while(t--){
		cnt=0;
		scanf("%i", &n);
		for(i=2;i<=1000000;i++){
			if(min_prime[i]==n) // if min_prime of i is n
				cnt++;          // then add it to the answer
		}
		printf("%d\n",cnt);
	}
    return 0;
}

You can easily convert this code into any other language.

answered Oct 31, 2015 by gccode
def check_prime(number):
    # 2, 3 is prime by default
    if number == 1:
        return False, 1
    elif number < 4:
        return True, number
    else:
        i = 0
        max_range = int(sqrt(number))+1
        # Check whether its a prime or not
        for i in xrange(2, max_range):
            if number % i == 0:
                return False, i
        return True, i
 
if __name__ == '__main__':
    from math import sqrt
    # Number of test cases
    total_test_cases = int(raw_input())
    for t in xrange(total_test_cases):
        count = 0
        # get the prime number
        test_prime = int(raw_input())
        if check_prime(test_prime)[0]:
            count = 1
        else:
            count = 0
        for num in xrange(test_prime, 10**6, test_prime):
            result = check_prime(num)
            if result[1] == test_prime:
                count += 1
 
        print count

 

answered Oct 31, 2015 by vijayanandrp
#include<bits/stdc++.h>
#define t 1000001
using namespace std;

int minPrime(int x)
{
    vector<int > prime(t,1);
    prime[0] = 0;prime[1] = 0;

    for(int i=2;i<=x && i*i<=t;i++){
        if(prime[i] == 1){

            for(int j=2;j*i<=t;j++)
                if(prime[i*j]==1)
                    prime[i*j] = i;

            prime[i] = i;
        }
    }

    return count(prime.begin(),prime.end(),x);
}
int main()
{
    int x;
    cin>>x;
    cout<<minPrime(x);
}

 

answered Sep 13, 2016 by Nitin Yadav
...