Numbers less than N which are product of exactly two distinct prime numbers
Given a number
. The task is to find all such numbers less than N and are a product of exactly two distinct prime numbers.
For Example, 33 is the product of two distinct primes i.e 11 * 3, whereas numbers like 60 which has three distinct prime factors i.e 2 * 2 * 3 * 5.
Note: These numbers cannot be a perfect square.
Examples:
Input : N = 30
Output : 6, 10, 14, 15, 21, 22, 26Input : N = 50
Output : 6, 10, 14, 15, 21, 22, 26, 33, 34, 35, 38, 39, 46
Algorithm:
- Traverse till N and check whether each number has exactly two prime factors or not.
- Now to avoid the situation like 49 having 7 * 7 product of two prime numbers, check whether the number is a perfect square or not to ensure that it has two distinct prime.
- If Step 1 and Step 2 satisfies then add the number in the vector list.
- Traverse the vector and print all the elements in it.
Below is the implementation of the above approach:
C++
// C++ program to find numbers that are product // of exactly two distinct prime numbers #include <bits/stdc++.h> using namespace std; // Function to check whether a number // is a PerfectSquare or not bool isPerfectSquare(long double x) { long double sr = sqrt(x); return ((sr - floor(sr)) == 0); } // Function to check if a number is a // product of exactly two distinct primes bool isProduct(int num) { int cnt = 0; for (int i = 2; cnt < 2 && i * i <= num; ++i) { while (num % i == 0) { num /= i; ++cnt; } } if (num > 1) ++cnt; return cnt == 2; } // Function to find numbers that are product // of exactly two distinct prime numbers. void findNumbers(int N) { // Vector to store such numbers vector<int> vec; for (int i = 1; i <= N; i++) { if (isProduct(i) && !isPerfectSquare(i)) { // insert in the vector vec.push_back(i); } } // Print all numers till n from the vector for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } } // Driver function int main() { int N = 30; findNumbers(N); return 0; } |
Java
// Java program to find numbers that are product // of exactly two distinct prime numbers import java.util.*; class GFG{ // Function to check whether a number // is a PerfectSquare or not static boolean isPerfectSquare(double x) { double sr = Math.sqrt(x); return ((sr - Math.floor(sr)) == 0); } // Function to check if a number is a // product of exactly two distinct primes static boolean isProduct(int num) { int cnt = 0; for (int i = 2; cnt < 2 && i * i <= num; ++i) { while (num % i == 0) { num /= i; ++cnt; } } if (num > 1) ++cnt; return cnt == 2; } // Function to find numbers that are product // of exactly two distinct prime numbers. static void findNumbers(int N) { // Vector to store such numbers Vector<Integer> vec = new Vector<Integer>(); for (int i = 1; i <= N; i++) { if (isProduct(i) && !isPerfectSquare(i)) { // insert in the vector vec.add(i); } } // Print all numers till n from the vector Iterator<Integer> itr = vec.iterator(); while(itr.hasNext()){ System.out.print(itr.next()+" "); } } // Driver function public static void main(String[] args) { int N = 30; findNumbers(N); } } // This Code is Contributed by mits |
Python 3
# Python 3 program to find numbers that are product # of exactly two distinct prime numbers import math # Function to check whether a number # is a PerfectSquare or not def isPerfectSquare(x): sr = math.sqrt(x) return ((sr - math.floor(sr)) == 0) # Function to check if a number is a # product of exactly two distinct primes def isProduct( num): cnt = 0 i = 2 while cnt < 2 and i * i <= num: while (num % i == 0) : num //= i cnt += 1 i += 1 if (num > 1): cnt += 1 return cnt == 2 # Function to find numbers that are product # of exactly two distinct prime numbers. def findNumbers(N): # Vector to store such numbers vec = [] for i in range(1,N+1) : if (isProduct(i) and not isPerfectSquare(i)) : # insert in the vector vec.append(i) # Print all numers till n from the vector for i in range(len( vec)): print(vec[i] ,end= " ") # Driver function if __name__=="__main__": N = 30 findNumbers(N) |
C#
// C# program to find numbers that are product // of exactly two distinct prime numbers using System; using System.Collections.Generic; class GFG { // Function to check whether a number // is a PerfectSquare or not static bool isPerfectSquare(double x) { double sr = Math.Sqrt(x); return ((sr - Math.Floor(sr)) == 0); } // Function to check if a number is a // product of exactly two distinct primes static bool isProduct(int num) { int cnt = 0; for (int i = 2; cnt < 2 && i * i <= num; ++i) { while (num % i == 0) { num /= i; ++cnt; } } if (num > 1) ++cnt; return cnt == 2; } // Function to find numbers that are product // of exactly two distinct prime numbers. static void findNumbers(int N) { // Vector to store such numbers List<int> vec = new List<int>(); for (int i = 1; i <= N; i++) { if (isProduct(i) && !isPerfectSquare(i)) { // insert in the vector vec.Add(i); } } // Print all numers till n from the vector foreach(var a in vec) Console.Write(a + " "); } // Driver code public static void Main(String[] args) { int N = 30; findNumbers(N); } } // This code has been contributed by 29AjayKumar |
PHP
<?php // PHP program to find numbers that are product // of exactly two distinct prime numbers // Function to check whether a number // is a PerfectSquare or not function isPerfectSquare($x) { $sr = sqrt($x); return (($sr - floor($sr)) == 0); } // Function to check if a number is a // product of exactly two distinct primes function isProduct($num) { $cnt = 0; for ($i = 2; $cnt < 2 && $i * $i <= $num; ++$i) { while ($num % $i == 0) { $num /= $i; ++$cnt; } } if ($num > 1) ++$cnt; return $cnt == 2; } // Function to find numbers that are product // of exactly two distinct prime numbers. function findNumbers($N) { // Vector to store such numbers $vec = array(); for ($i = 1; $i <= $N; $i++) { if (isProduct($i) && !isPerfectSquare($i)) { // insert in the vector array_push($vec, $i); } } // Print all numers till n from the vector for ($i = 0; $i < sizeof($vec); $i++) { echo $vec[$i] . " "; } } // Driver Code $N = 30; findNumbers($N); // This code is contributed by ita_c |
6 10 14 15 21 22 26
Time Complexity: O(
*
)
Recommended Posts:
- Find two distinct prime numbers with given product
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Exactly n distinct prime factor numbers from a to b
- Check if all sub-numbers have distinct Digit product
- Number of distinct prime factors of first n natural numbers
- Product of all prime numbers in an Array
- Find product of prime numbers between 1 to n
- Find the Product of first N Prime Numbers
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Check if each element of the given array is the product of exactly K prime numbers
- Sum and product of k smallest and k largest prime numbers in the array
- Check if product of array containing prime numbers is a perfect square
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Distinct Prime Factors of Array Product
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



