📜  查找自然数的所有除数|套装2

📅  最后修改于: 2021-05-04 23:46:56             🧑  作者: Mango

给定自然数n,则打印其所有除数。

例子:

Input : n = 10       
 Output: 1 2 5 10

 Input:  n = 100
 Output: 1 2 4 5 10 20 25 50 100

 Input:  n = 125
 Output: 1 5 25 125 

我们强烈建议您参考以下文章作为先决条件。
查找自然数的所有除数|套装1
在上面的文章中,我们找到了一种找到O(sqrt(n))中所有除数的方法。
但是,解决方案中仍然存在一个小问题,您能猜到吗?
是的!输出结果不是采用暴力技术获得的排序方式。

如何按排序打印输出?
如果观察得到的输出,则可以分析除数以Z字形(小,大对)打印。因此,如果我们存储其中的一半,则可以按排序顺序进行打印。

以下是相同的实现:

C++
// A O(sqrt(n)) program that prints all divisors
// in sorted order
#include 
using namespace std;
 
// function to print the divisors
void printDivisors(int n)
{
    // Vector to store half of the divisors
    vector v;
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
 
            // check if divisors are equal
            if (n / i == i)
                printf("%d ", i);
            else {
                printf("%d ", i);
 
                // push the second divisor in the vector
                v.push_back(n / i);
            }
        }
    }
 
    // The vector will be printed in reverse
    for (int i = v.size() - 1; i >= 0; i--)
        printf("%d ", v[i]);
}
 
/* Driver program to test above function */
int main()
{
    printf("The divisors of 100 are: n");
    printDivisors(100);
    return 0;
}


Java
// A O(sqrt(n)) java program that prints all divisors
// in sorted order
 
import java.util.Vector;
 
class Test {
    // method to print the divisors
    static void printDivisors(int n)
    {
        // Vector to store half of the divisors
        Vector v = new Vector<>();
        for (int i = 1; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
 
                // check if divisors are equal
                if (n / i == i)
                    System.out.printf("%d ", i);
                else {
                    System.out.printf("%d ", i);
 
                    // push the second divisor in the vector
                    v.add(n / i);
                }
            }
        }
 
        // The vector will be printed in reverse
        for (int i = v.size() - 1; i >= 0; i--)
            System.out.printf("%d ", v.get(i));
    }
 
    // Driver method
    public static void main(String args[])
    {
        System.out.println("The divisors of 100 are: ");
        printDivisors(100);
    }
}


Python3
# A O(sqrt(n)) java program that prints
# all divisors in sorted order
import math
 
# Method to print the divisors
def printDivisors(n) :
    list = []
     
    # List to store half of the divisors
    for i in range(1, int(math.sqrt(n) + 1)) :
         
        if (n % i == 0) :
             
            # Check if divisors are equal
            if (n / i == i) :
                print (i, end =" ")
            else :
                # Otherwise print both
                print (i, end =" ")
                list.append(int(n / i))
                 
    # The list will be printed in reverse   
    for i in list[::-1] :
        print (i, end =" ")
         
# Driver method
print ("The divisors of 100 are: ")
printDivisors(100)
 
# This code is contributed by Gitanjali


C#
// A O(sqrt(n)) C# program that
// prints all divisors in sorted order
using System;
 
class GFG {
 
    // method to print the divisors
    static void printDivisors(int n)
    {
        // Vector to store half
        // of the divisors
        int[] v = new int[n];
        int t = 0;
        for (int i = 1;
             i <= Math.Sqrt(n); i++) {
            if (n % i == 0) {
 
                // check if divisors are equal
                if (n / i == i)
                    Console.Write(i + " ");
                else {
                    Console.Write(i + " ");
 
                    // push the second divisor
                    // in the vector
                    v[t++] = n / i;
                }
            }
        }
 
        // The vector will be
        // printed in reverse
        for (int i = t - 1; i >= 0; i--)
            Console.Write(v[i] + " ");
    }
 
    // Driver Code
    public static void Main()
    {
        Console.Write("The divisors of 100 are: \n");
        printDivisors(100);
    }
}
 
// This code is contributed
// by ChitraNayal


PHP
= 0; $i--)
        echo $v[$i] . " ";
}
 
// Driver code
echo "The divisors of 100 are: \n";
printDivisors(100);
 
// This code is contributed by mits
?>


Javascript


C++
// A O(sqrt(n)) program that prints all divisors
// in sorted order
#include 
using namespace std;
 
// function to print the divisors
void printDivisors(int n)
{
    for (int i = 1; i*i < n; i++) {
        if (n % i == 0)
            printf("%d ", i);
    }
    for (int i = sqrt(n); i >= 1; i--) {
        if (n % i == 0)
            printf("%d ", n / i);
    }
}
 
/* Driver program to test above function */
int main()
{
    printf("The divisors of 100 are: \n");
    printDivisors(100);
    return 0;
}


Java
// A O(sqrt(n)) program that prints all
// divisors in sorted order
import java.lang.Math;
 
class GFG{
     
// Function to print the divisors
public static void printDivisors(int n)
{
    for(int i = 1; i * i < n; i++)
    {
        if (n % i == 0)
            System.out.print(i + " ");
    }
    for(int i = (int)Math.sqrt(n);
            i >= 1; i--)
    {
        if (n % i == 0)
            System.out.print(n / i + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    System.out.println("The divisors of 100 are: ");
     
    printDivisors(100);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# A O(sqrt(n)) program that prints all divisors
# in sorted order
from math import *
 
# Function to print the divisors
def printDivisors (n):
 
    i = 1
    while (i * i < n):
        if (n % i == 0):
            print(i, end = " ")
 
        i += 1
 
    for i in range(int(sqrt(n)), 0, -1):
        if (n % i == 0):
            print(n // i, end = " ")
 
# Driver Code
print("The divisors of 100 are: ")
 
printDivisors(100)
 
# This code is contributed by himanshu77


C#
// A O(sqrt(n)) program that prints
// all divisors in sorted order
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
 
// Function to print the divisors
static void printDivisors(int n)
{
    for(int i = 1; i * i < n; i++)
    {
        if (n % i == 0)
            Console.Write(i + " ");
    }
    for(int i = (int)Math.Sqrt(n); i >= 1; i--)
    {
        if (n % i == 0)
            Console.Write(n / i + " ");
    }
} 
  
// Driver code  
public static void Main(string []arg)
{
    Console.Write("The divisors of 100 are: \n");
     
    printDivisors(100);
}
}
 
// This code is contributed by rutvik_56


Javascript


输出 :

The divisors of 100 are: 
1 2 4 5 10 20 25 50 100 

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

AO(sqrt(n))时间和O(1)空间解决方案:

C++

// A O(sqrt(n)) program that prints all divisors
// in sorted order
#include 
using namespace std;
 
// function to print the divisors
void printDivisors(int n)
{
    for (int i = 1; i*i < n; i++) {
        if (n % i == 0)
            printf("%d ", i);
    }
    for (int i = sqrt(n); i >= 1; i--) {
        if (n % i == 0)
            printf("%d ", n / i);
    }
}
 
/* Driver program to test above function */
int main()
{
    printf("The divisors of 100 are: \n");
    printDivisors(100);
    return 0;
}

Java

// A O(sqrt(n)) program that prints all
// divisors in sorted order
import java.lang.Math;
 
class GFG{
     
// Function to print the divisors
public static void printDivisors(int n)
{
    for(int i = 1; i * i < n; i++)
    {
        if (n % i == 0)
            System.out.print(i + " ");
    }
    for(int i = (int)Math.sqrt(n);
            i >= 1; i--)
    {
        if (n % i == 0)
            System.out.print(n / i + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    System.out.println("The divisors of 100 are: ");
     
    printDivisors(100);
}
}
 
// This code is contributed by divyeshrabadiya07

Python3

# A O(sqrt(n)) program that prints all divisors
# in sorted order
from math import *
 
# Function to print the divisors
def printDivisors (n):
 
    i = 1
    while (i * i < n):
        if (n % i == 0):
            print(i, end = " ")
 
        i += 1
 
    for i in range(int(sqrt(n)), 0, -1):
        if (n % i == 0):
            print(n // i, end = " ")
 
# Driver Code
print("The divisors of 100 are: ")
 
printDivisors(100)
 
# This code is contributed by himanshu77

C#

// A O(sqrt(n)) program that prints
// all divisors in sorted order
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
 
// Function to print the divisors
static void printDivisors(int n)
{
    for(int i = 1; i * i < n; i++)
    {
        if (n % i == 0)
            Console.Write(i + " ");
    }
    for(int i = (int)Math.Sqrt(n); i >= 1; i--)
    {
        if (n % i == 0)
            Console.Write(n / i + " ");
    }
} 
  
// Driver code  
public static void Main(string []arg)
{
    Console.Write("The divisors of 100 are: \n");
     
    printDivisors(100);
}
}
 
// This code is contributed by rutvik_56

Java脚本


输出:

The divisors of 100 are: 
1 2 4 5 10 20 25 50 100 

感谢Mysterious Mind建议上述解决方案。