给定数 N 的不同素因数
给定一个数N ,任务是找到N的不同质因数。
例子:
Input: N = 12
Output: 2 3
Explanation: The factors of 12 are 1, 2, 3, 4, 6, 12.
Among these the distinct prime factors are 2 and 3.
Input: N = 39
Output: 3 13
方法:该方法是使用图来检查数字的给定因素是否更早发生。现在按照以下步骤解决此问题:
- 创建一个访问过的地图以跟踪所有以前的主要因素。
- 创建一个变量C ,并用 2 对其进行初始化。
- 虽然N可以被C整除,但如果C不存在于地图中,则打印C。现在将N除以C 。还将C加 1。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find distinct prime factor
// of a number N
void distinctPrimeFactors(int N)
{
if (N < 2) {
cout << -1;
}
int c = 2;
unordered_map visited;
while (N > 1) {
if (N % c == 0) {
if (!visited) {
cout << c << " ";
visited = 1;
}
N /= c;
}
else
c++;
}
}
// Driver Code
int main()
{
int N = 39;
distinctPrimeFactors(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find distinct prime factor
// of a number N
static void distinctPrimeFactors(int N)
{
if (N < 2) {
System.out.print(-1);
}
int c = 2;
// Create a new dictionary of
// strings, with string keys.
HashMap visited = new HashMap<>();
for(int i = 0; i < N; i++) {
visited.put(i, false);
}
while (N > 1) {
if (N % c == 0) {
if(visited.containsKey(c)){
if (!visited.get(c)) {
System.out.print(c + " ");
visited.put(c, true);
}
}
N /= c;
}
else
c++;
}
}
// Driver Code
public static void main(String[] args)
{
int N = 39;
distinctPrimeFactors(N);
}
}
// This code is contributed by Samim Hossain Mondal
Python3
# python3 program for the above approach
# Function to find distinct prime factor
# of a number N
def distinctPrimeFactors(N):
if (N < 2):
print(-1)
c = 2
visited = {}
while (N > 1):
if (N % c == 0):
if (not c in visited):
print(c, end=" ")
visited = 1 if c in visited else 0
N //= c
else:
c += 1
# Driver Code
if __name__ == "__main__":
N = 39
distinctPrimeFactors(N)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find distinct prime factor
// of a number N
static void distinctPrimeFactors(int N)
{
if (N < 2) {
Console.Write(-1);
}
int c = 2;
// Create a new dictionary of
// strings, with string keys.
Dictionary visited =
new Dictionary();
for(int i = 0; i < N; i++) {
visited[i] = false;
}
while (N > 1) {
if (N % c == 0) {
if(visited.ContainsKey(c)){
if (!visited) {
Console.Write(c + " ");
visited = true;
}
}
N /= c;
}
else
c++;
}
}
// Driver Code
public static void Main()
{
int N = 39;
distinctPrimeFactors(N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find distinct prime factor
// of a number N
void distinctPrimeFactors(int N)
{
if (N < 2) {
cout << -1;
return;
}
unordered_map visited;
for(int i=2;i*i<=N;i++)
{
while(N%i==0)
{
if(!visited[i])
{
cout<2)
cout<
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find distinct prime factor
// of a number N
static void distinctPrimeFactors(int N)
{
if (N < 2) {
System.out.print(-1);
return;
}
HashMap visited = new HashMap<>();
for (int i = 2; i * i <= N; i++) {
while (N % i == 0) {
if (!visited.containsKey(i)) {
System.out.print(i + " ");
visited.put(i, true);
}
N /= i;
}
}
if (N > 2) {
System.out.print(N);
}
}
// Driver Code
public static void main(String[] args)
{
int N = 315;
distinctPrimeFactors(N);
}
}
// This code is contributed by Taranpreet
Python3
# Python program for the above approach
# Function to find distinct prime factor
# of a number N
def distinctPrimeFactors(N):
if (N < 2):
print(-1)
return
visited = {}
i = 2
while(i * i <= N):
while(N % i == 0):
if(i not in visited):
print(i , end = " ")
visited[i] = 1
N //= i
i+=1
if(N > 2):
print(N)
# Driver Code
N = 315;
distinctPrimeFactors(N);
# This code is contributed by Shubham Singh
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find distinct prime factor
// of a number N
static void distinctPrimeFactors(int N)
{
if (N < 2) {
Console.Write(-1);
return;
}
Dictionary visited =
new Dictionary();
for (int i = 2; i * i <= N; i++) {
while (N % i == 0) {
if (!visited.ContainsKey(i)) {
Console.Write(i + " ");
visited[i] = true;
}
N /= i;
}
}
if (N > 2) {
Console.Write(N);
}
}
// Driver code
public static void Main()
{
int N = 315;
distinctPrimeFactors(N);
}
}
// This code is contributed by avijitmondal1998
Javascript
输出
3 13
时间复杂度: O(N)
辅助空间: O(N 1/2 )
有效方法:这种方法类似于我们找到主要因素的上述方法。唯一的区别是我们从2遍历到sqrt(n)以找到所有素因数,因为我们知道这也足以检查素数。如果仍然发现这个数字大于 2,那么它是素数,我们也需要打印它。
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find distinct prime factor
// of a number N
void distinctPrimeFactors(int N)
{
if (N < 2) {
cout << -1;
return;
}
unordered_map visited;
for(int i=2;i*i<=N;i++)
{
while(N%i==0)
{
if(!visited[i])
{
cout<2)
cout<
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find distinct prime factor
// of a number N
static void distinctPrimeFactors(int N)
{
if (N < 2) {
System.out.print(-1);
return;
}
HashMap visited = new HashMap<>();
for (int i = 2; i * i <= N; i++) {
while (N % i == 0) {
if (!visited.containsKey(i)) {
System.out.print(i + " ");
visited.put(i, true);
}
N /= i;
}
}
if (N > 2) {
System.out.print(N);
}
}
// Driver Code
public static void main(String[] args)
{
int N = 315;
distinctPrimeFactors(N);
}
}
// This code is contributed by Taranpreet
Python3
# Python program for the above approach
# Function to find distinct prime factor
# of a number N
def distinctPrimeFactors(N):
if (N < 2):
print(-1)
return
visited = {}
i = 2
while(i * i <= N):
while(N % i == 0):
if(i not in visited):
print(i , end = " ")
visited[i] = 1
N //= i
i+=1
if(N > 2):
print(N)
# Driver Code
N = 315;
distinctPrimeFactors(N);
# This code is contributed by Shubham Singh
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find distinct prime factor
// of a number N
static void distinctPrimeFactors(int N)
{
if (N < 2) {
Console.Write(-1);
return;
}
Dictionary visited =
new Dictionary();
for (int i = 2; i * i <= N; i++) {
while (N % i == 0) {
if (!visited.ContainsKey(i)) {
Console.Write(i + " ");
visited[i] = true;
}
N /= i;
}
}
if (N > 2) {
Console.Write(N);
}
}
// Driver code
public static void Main()
{
int N = 315;
distinctPrimeFactors(N);
}
}
// This code is contributed by avijitmondal1998
Javascript
时间复杂度: O(N^(1/2))
辅助空间: O(N^(1/2))