Given an integer N, the task is to print prime factors of N in decreasing order using the stack data structure.
Examples:
Input: N = 34
Output:
17 2
Explanation:
The prime factors of the number 34 is 2 and 17.Input: N = 8
Output: 2
Approach: The idea is to use the Stack data structure to store all the prime factors of N and in the end, print all the values in the Stack. Follow the steps below to solve the problem:
- Initialize a stack, say st.
- Run a loop while N != 1. From i = 2, for each value of i, run a loop until N % i == 0 and push i into the stack st and update N to N/i.
- Finally, print all the values from top to bottom of stack st.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to print prime factors // of N in decreasing order void PrimeFactors( int N)
{ // Stores prime factors of N
// in decreasing order
stack< int > st;
int i = 2;
while (N != 1) {
if (N % i == 0) {
// Insert i into stack
st.push(i);
while (N % i == 0) {
// Update N
N = N / i;
}
}
// Update i
i++;
}
// Print value of stack st
while (!st.empty()) {
printf ( "%d " , st.top());
st.pop();
}
} // Driver Code int main()
{ int N = 8;
// function Call
PrimeFactors(N);
return 0;
} |
Java
// Java program for the above approach import java.util.*;
class GFG{
// Function to print prime factors // of N in decreasing order static void PrimeFactors( int N)
{ // Stores prime factors of N
// in decreasing order
Stack<Integer> st = new Stack<>();
int i = 2 ;
while (N != 1 )
{
if (N % i == 0 )
{
// Insert i into stack
st.push(i);
while (N % i == 0 )
{
// Update N
N = N / i;
}
}
// Update i
i++;
}
// Print value of stack st
while (!st.isEmpty())
{
System.out.println(st.peek());
st.pop();
}
} // Driver Code public static void main (String[] args)
{ int N = 8 ;
// Function Call
PrimeFactors(N);;
} } // This code is contributed by susmitakundugoaldanga |
Python3
# Python3 program for the above approach # Function to print prime factors # of N in decreasing order def PrimeFactors(N):
# Stores prime factors of N
# in decreasing order
st = []
i = 2
while (N ! = 1 ):
if (N % i = = 0 ):
# Insert i into stack
st.append(i)
while (N % i = = 0 ):
# Update N
N = N / / i
# Update i
i + = 1
# Print value of stack st
while ( len (st) ! = 0 ):
print (st[ - 1 ])
st.pop()
# Driver Code if __name__ = = "__main__" :
N = 8
# function Call
PrimeFactors(N)
# This code is contributed by chitranayal.
|
C#
// C# program for the above approach using System;
using System.Collections.Generic;
class GFG{
// Function to print prime factors // of N in decreasing order static void PrimeFactors( int N)
{ // Stores prime factors of N
// in decreasing order
Stack< int > st = new Stack< int >();
int i = 2;
while (N != 1)
{
if (N % i == 0)
{
// Insert i into stack
st.Push(i);
while (N % i == 0)
{
// Update N
N = N / i;
}
}
// Update i
i++;
}
// Print value of stack st
while (st.Count != 0)
{
Console.Write(st.Peek());
st.Pop();
}
} // Driver Code public static void Main ()
{ int N = 8;
// Function Call
PrimeFactors(N);;
} } // This code is contributed by code_hunt |
Javascript
<script> // JavaScript program for the above approach // Function to print prime factors // of N in decreasing order function PrimeFactors(N)
{ // Stores prime factors of N
// in decreasing order
let st = [];
let i = 2;
while (N != 1)
{
if (N % i == 0)
{
// Insert i into stack
st.push(i);
while (N % i == 0)
{
// Update N
N = Math.floor(N / i);
}
}
// Update i
i++;
}
// Print value of stack st
while (st.length!=0)
{
document.write(st.pop());
}
} // Driver Code let N = 8; // Function Call PrimeFactors(N); // This code is contributed by avanitrachhadiya2155 </script> |
Output:
2
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)