以递减顺序打印给定整数的质因数

原文:

给定一个整数 n ,任务是使用。

示例:

输入: n = 34 输出: 17 2 说明: 数字 34 的质因数是 2 和 17。

输入:n = 8 t3】输出: 2

方式:思路是使用存储nt7】的所有中的所有值。按照以下步骤解决问题:

  1. 初始化一个,比如 st
  2. 同时 n!= 1 。从 i = 2、开始,对于 i 的每个值,运行一个循环,直到 n % i == 0st 并将 n 更新为 n/i.
  3. 最后, st

下面是上述方法的实现:

c

// c   program for the above approach
#include 
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 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 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

python 3

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

java 描述语言


output: 

2

时间复杂度: o(sqrt(n)) 辅助空间: o(1)