📜  找出自然数的所有因子|套装1

📅  最后修改于: 2021-05-05 00:42:43             🧑  作者: 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迭代到n,检查该数字是否除以n并打印出来。以下是相同的程序:

C++
// C++ implementation of Naive method to print all
// divisors
#include 
 
// function to print the divisors
void printDivisors(int n)
{
    for (int i=1;i<=n;i++)
        if (n%i==0)
            printf("%d ",i);
}
 
/* Driver program to test above function */
int main()
{
    printf("The divisors of 100 are: \n");
    printDivisors(100);
    return 0;
}


Java
// Java implementation of Naive method to print all
// divisors
 
class Test
{
    // method to print the divisors
    static void printDivisors(int n)
    {
        for (int i=1;i<=n;i++)
            if (n%i==0)
                System.out.print(i+" ");
    }
 
    // Driver method
    public static void main(String args[])
    {
        System.out.println("The divisors of 100 are: ");
        printDivisors(100);;
    }
}


Python
# Python implementation of Naive method
# to print all divisors
 
# method to print the divisors
def printDivisors(n) :
    i = 1
    while i <= n :
        if (n % i==0) :
            print i,
        i = i + 1
         
# Driver method
print "The divisors of 100 are: "
printDivisors(100)
 
#This code is contributed by Nikita Tiwari.


C#
// C# implementation of Naive method
// to print all divisors
using System;
 
class GFG {
     
    // method to print the divisors
    static void printDivisors(int n)
    {
        for (int i = 1; i <= n; i++)
            if (n % i == 0)
                Console.Write( i + " ");
    }
 
    // Driver method
    public static void Main()
    {
        Console.Write("The divisors of",
                          " 100 are: ");
        printDivisors(100);;
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


C++
// A Better (than Naive) Solution to find all divisiors
#include 
 
// Function to print the divisors
void printDivisors(int n)
{
    // Note that this loop runs till square root
    for (int i=1; i<=sqrt(n); i++)
    {
        if (n%i == 0)
        {
            // If divisors are equal, print only one
            if (n/i == i)
                printf("%d ", i);
 
            else // Otherwise print both
                printf("%d %d ", i, n/i);
        }
    }
}
 
/* Driver program to test above function */
int main()
{
    printf("The divisors of 100 are: \n");
    printDivisors(100);
    return 0;
}


Java
// A Better (than Naive) Solution to find all divisors
 
class Test
{
    // method to print the divisors
    static void printDivisors(int n)
    {
        // Note that this loop runs till square root
        for (int i=1; i<=Math.sqrt(n); i++)
        {
            if (n%i==0)
            {
                // If divisors are equal, print only one
                if (n/i == i)
                    System.out.print(" "+ i);
      
                else // Otherwise print both
                    System.out.print(i+" " + n/i + " " );
            }
        }
    }
 
    // Driver method
    public static void main(String args[])
    {
        System.out.println("The divisors of 100 are: ");
        printDivisors(100);;
    }
}


Python
# A Better (than Naive) Solution to find all divisiors
import math
 
# method to print the divisors
def printDivisors(n) :
     
    # Note that this loop runs till square root
    i = 1
    while i <= math.sqrt(n):
         
        if (n % i == 0) :
             
            # If divisors are equal, print only one
            if (n / i == i) :
                print i,
            else :
                # Otherwise print both
                print i , n/i,
        i = i + 1
 
# Driver method
print "The divisors of 100 are: "
printDivisors(100)
 
#This code is contributed by Nikita Tiwari.


C#
// A Better (than Naive) Solution to
// find all divisors
using System;
 
class GFG {
     
    // method to print the divisors
    static void printDivisors(int n)
    {
         
        // Note that this loop runs
        // till square root
        for (int i = 1; i <= Math.Sqrt(n);
                                      i++)
        {
            if (n % i == 0)
            {
                 
                // If divisors are equal,
                // print only one
                if (n / i == i)
                    Console.Write(i + " ");
                 
                // Otherwise print both
                else
                    Console.Write(i + " "
                            + n / i + " ");
            }
        }
    }
 
    // Driver method
    public static void Main()
    {
        Console.Write("The divisors of "
                          + "100 are: \n");
        printDivisors(100);
    }
}
 
// This code is contributed by Smitha


PHP


Javascript


输出:

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

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

我们可以改善上述解决方案吗?
如果我们仔细观察,所有除数都是成对出现的。例如,如果n = 100,则各对除数分别为:(1,100),(2,50),(4,25),(5,20),(10,10)
利用这个事实,我们可以大大加快我们的程序的速度。
但是,与(10,10)的情况一样,如果有两个相等的除数,我们必须要小心。在这种情况下,我们将仅打印其中之一。

以下是相同的实现:

C++

// A Better (than Naive) Solution to find all divisiors
#include 
 
// Function to print the divisors
void printDivisors(int n)
{
    // Note that this loop runs till square root
    for (int i=1; i<=sqrt(n); i++)
    {
        if (n%i == 0)
        {
            // If divisors are equal, print only one
            if (n/i == i)
                printf("%d ", i);
 
            else // Otherwise print both
                printf("%d %d ", i, n/i);
        }
    }
}
 
/* Driver program to test above function */
int main()
{
    printf("The divisors of 100 are: \n");
    printDivisors(100);
    return 0;
}

Java

// A Better (than Naive) Solution to find all divisors
 
class Test
{
    // method to print the divisors
    static void printDivisors(int n)
    {
        // Note that this loop runs till square root
        for (int i=1; i<=Math.sqrt(n); i++)
        {
            if (n%i==0)
            {
                // If divisors are equal, print only one
                if (n/i == i)
                    System.out.print(" "+ i);
      
                else // Otherwise print both
                    System.out.print(i+" " + n/i + " " );
            }
        }
    }
 
    // Driver method
    public static void main(String args[])
    {
        System.out.println("The divisors of 100 are: ");
        printDivisors(100);;
    }
}

Python

# A Better (than Naive) Solution to find all divisiors
import math
 
# method to print the divisors
def printDivisors(n) :
     
    # Note that this loop runs till square root
    i = 1
    while i <= math.sqrt(n):
         
        if (n % i == 0) :
             
            # If divisors are equal, print only one
            if (n / i == i) :
                print i,
            else :
                # Otherwise print both
                print i , n/i,
        i = i + 1
 
# Driver method
print "The divisors of 100 are: "
printDivisors(100)
 
#This code is contributed by Nikita Tiwari.

C#

// A Better (than Naive) Solution to
// find all divisors
using System;
 
class GFG {
     
    // method to print the divisors
    static void printDivisors(int n)
    {
         
        // Note that this loop runs
        // till square root
        for (int i = 1; i <= Math.Sqrt(n);
                                      i++)
        {
            if (n % i == 0)
            {
                 
                // If divisors are equal,
                // print only one
                if (n / i == i)
                    Console.Write(i + " ");
                 
                // Otherwise print both
                else
                    Console.Write(i + " "
                            + n / i + " ");
            }
        }
    }
 
    // Driver method
    public static void Main()
    {
        Console.Write("The divisors of "
                          + "100 are: \n");
        printDivisors(100);
    }
}
 
// This code is contributed by Smitha

的PHP


Java脚本


输出:

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

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