Skip to content
Related Articles

Related Articles

Print prime factors of a given integer in decreasing order using Stack
  • Last Updated : 20 Jan, 2021

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:



  1. Initialize a stack, say st.
  2. 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.
  3. Finally, print all the values from top to bottom of stack st.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

2

 

Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :