Given a number N, print all its unique prime factors and their powers in N.
N = 100
Factor Power
2 2
5 2
N = 35
Factor Power
5 1
7 1
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.
Output:
Print all prime factors and their powers separated by spaces. The output should be printed in increasing order of prime factors.
Constraints:
1 ≤ T ≤ 200
2 ≤ N ≤ 10000
Example:
Input:
2
100
35
Output:
2 2 5 2
5 1 7 1
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.
Output:
Print all prime factors and their powers separated by spaces. The output should be printed in increasing order of prime factors.
Constraints:
1 ≤ T ≤ 200
2 ≤ N ≤ 10000
Example:
Input:
2
100
35
Output:
2 2 5 2
5 1 7 1
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n = sc.nextInt();
primeFact(n);
System.out.println();
}
}
public static void primeFact(int n){
int num=n;
int count[]= new int[n+1];
Arrays.fill(count,0);
if(num%2==0){
while(num%2==0){
num=num/2;
count[2]+=1;
}
}
for(int i=3;i<count.length;i+=2){
if(num%i==0 && n!=1){
while(num%i==0){
num=num/i;
count[i]+=1;
}
}
}
count[num]=1;
for(int j=2;j<count.length;j++){
if(count[j]!=0){System.out.print(j+" "+count[j]+" ");}
//System.out.print(j+" "+count[j]+" ");
}
}
}
EXECUTION TIME: 0.12s
Comments
Post a Comment