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 ordervoid 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 Codeint 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 orderstatic 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 orderdef 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 Codeif __name__ == "__main__":Â Â Â Â N = 8Â
    # function Call    PrimeFactors(N)Â
    # This code is contributed by chitranayal. |
C#
// C# program for the above approachusing System;using System.Collections.Generic; Â
class GFG{         // Function to print prime factors// of N in decreasing orderstatic 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 orderfunction 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 CallPrimeFactors(N);Â
Â
// This code is contributed by avanitrachhadiya2155Â
</script> |
2
Â
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Find More Info here to that Topic: geeksforgeeks.org/print-prime-factors-of-a-given-integer-in-decreasing-order-using-stack/ […]
… [Trackback]
[…] Here you will find 66302 more Info to that Topic: geeksforgeeks.org/print-prime-factors-of-a-given-integer-in-decreasing-order-using-stack/ […]
… [Trackback]
[…] Find More to that Topic: geeksforgeeks.org/print-prime-factors-of-a-given-integer-in-decreasing-order-using-stack/ […]