So finden Sie alle Faktoren einer natürlichen Zahl in C++, Python und JavaScript

Ein Faktor ist eine Zahl, die eine gegebene Zahl genau teilt; das heißt, es dividiert die Zahl, ohne einen Rest zu hinterlassen. Das Finden der Faktoren einer Zahl mit Hilfe der Programmierung kann Ihnen helfen, Ihre Konzepte von Schleifen, bedingten Anweisungen und Modulo-Operatoren zu festigen.

In diesem Artikel erfahren Sie, wie Sie mit C++, Python und JavaScript alle Faktoren einer natürlichen Zahl finden.

Problemstellung

Sie erhalten eine natürliche Zahl num , Sie müssen alle unterschiedlichen Faktoren von num finden und ausgeben .

Beispiel 1 : Sei num = 60.

Die Faktoren von 60 sind: 1 2 3 4 5 6 10 12 15 20 30 60

Somit beträgt die Ausgabe 1 2 3 4 5 6 10 12 15 20 30 60.

Beispiel 2 : Sei num = 100.

Die Faktoren von 100 sind: 1 2 4 5 10 20 25 50 100

Somit beträgt die Ausgabe 1 2 4 5 10 20 25 50 100.

Beispiel 3 : Sei num = 85.

Die Faktoren von 85 sind: 1 5 17 85

Somit beträgt die Ausgabe 1 5 17 85.

Grundlegender Ansatz zur Lösung des Problems

Sie können alle unterschiedlichen Faktoren einer Zahl finden, indem Sie dem folgenden Ansatz folgen:

  1. Iteriere alle Zahlen von 1 bis num .
  2. Wenn die Zahl num perfekt teilt, drucken Sie die Zahl aus.

Bei diesem Ansatz wäre die Zeitkomplexität der Lösung O(n) und der benötigte Hilfsraum wäre O(1).

C++-Programm zum Ermitteln von Faktoren einer Zahl

Unten ist das C++-Programm, um alle Faktoren einer Zahl zu finden:

 // C++ program to find all factors of a natural number
#include <iostream>
using namespace std;
void findFactors(int num)
{
for(int i=1; i<=num; i++)
{
if(num%i == 0)
{
cout << i << " ";
}
}
cout << endl;
}
int main()
{
int num1 = 60;
cout << "Factors of " << num1 << " are: " << endl;
findFactors(num1);
int num2 = 100;
cout << "Factors of " << num2 << " are: " << endl;
findFactors(num2);
int num3 = 85;
cout << "Factors of " << num3 << " are: " << endl;
findFactors(num3);
int num4 = 66;
cout << "Factors of " << num4 << " are: " << endl;
findFactors(num4);
int num5 = 71;
cout << "Factors of " << num5 << " are: " << endl;
findFactors(num5);
return 0;
}

Ausgabe:

 Factors of 60 are:
1 2 3 4 5 6 10 12 15 20 30 60
Factors of 100 are:
1 2 4 5 10 20 25 50 100
Factors of 85 are:
1 5 17 85
Factors of 66 are:
1 2 3 6 11 22 33 66
Factors of 71 are:
1 71

Python-Programm zum Finden von Faktoren einer Zahl

Unten ist das Python-Programm, um alle Faktoren einer Zahl zu finden:

 # Python program to find all factors of a natural number
def findFactors(num):
for i in range(1,num+1):
if (num%i==0):
print(i, end=" ")
print()
num1 = 60
print("Factors of", num1, "are:")
findFactors(num1)
num2 = 100
print("Factors of", num2, "are:")
findFactors(num2)
num3 = 85
print("Factors of", num3, "are:")
findFactors(num3)
num4 = 66
print("Factors of", num4, "are:")
findFactors(num4)
num5 = 71
print("Factors of", num5, "are:")
findFactors(num5)

Ausgabe:

 Factors of 60 are:
1 2 3 4 5 6 10 12 15 20 30 60
Factors of 100 are:
1 2 4 5 10 20 25 50 100
Factors of 85 are:
1 5 17 85
Factors of 66 are:
1 2 3 6 11 22 33 66
Factors of 71 are:
1 71

Verwandte: So finden Sie die LCM und GCD von zwei Zahlen in mehreren Sprachen

JavaScript-Programm zum Finden von Faktoren einer Zahl

Unten ist das JavaScript-Programm, um alle Faktoren einer Zahl zu finden:

 // JavaScript program to find all factors of a natural number
function findFactors(num) {
for(let i=1; i<=num; i++) {
if(num%i == 0) {
document.write(i + " ");
}
}
document.write("<br>");
}

let num1 = 60;
document.write("Factors of " + num1 + " are: " + "<br>");
findFactors(num1);
let num2 = 100;
document.write("Factors of " + num2 + " are: " + "<br>");
findFactors(num2);
let num3 = 85;
document.write("Factors of " + num3 + " are: " + "<br>");
findFactors(num3);
let num4 = 66;
document.write("Factors of " + num4 + " are: " + "<br>");
findFactors(num4);
let num5 = 71;
document.write("Factors of " + num5 + " are: " + "<br>");
findFactors(num5);

Ausgabe:

 Factors of 60 are:
1 2 3 4 5 6 10 12 15 20 30 60
Factors of 100 are:
1 2 4 5 10 20 25 50 100
Factors of 85 are:
1 5 17 85
Factors of 66 are:
1 2 3 6 11 22 33 66
Factors of 71 are:
1 71

Verwandte: Eine Einführung in den Merge-Sortieralgorithmus

Optimierter Ansatz zur Lösung des Problems

Betrachtet man die Faktoren einer Zahl, so erscheinen sie paarweise. Wenn beispielsweise num = 64, wären die Faktoren von 64: (1, 64), (2, 32), (4, 16) und (8, 8). Diesen Umstand können Sie nutzen, um Ihre Lösung zu optimieren.

Bei zwei gleichen Faktoren müssen Sie den Faktor jedoch nur einmal ausdrucken. Wie im obigen Beispiel sind (8, 8) zwei gleiche Faktoren. Sie müssen sie also nur einmal ausdrucken.

Daher können Sie den folgenden optimierten Ansatz verwenden, um alle unterschiedlichen Faktoren einer Zahl zu finden:

  1. Iteriere alle Zahlen von 1 bis zur Quadratwurzel von num .
  2. Wenn die Zahl num perfekt teilt, bedeutet dies, dass die Zahl ein Faktor von num ist .
  3. Prüfen Sie nun, ob der zweite Faktor ( num / 1. Faktor) gleich dem ersten Faktor ist.
  4. Wenn beide Faktoren gleich sind, drucken Sie den Faktor einmal aus.
  5. Wenn beide Faktoren ungleich sind, drucken Sie beide Faktoren aus.

Bei diesem Ansatz beträgt die Zeitkomplexität der Lösung O(sqrt(n)) und der erforderliche Hilfsraum ist O(1).

C++-Programm, das einen optimierten Ansatz verwendet, um Faktoren einer Zahl zu finden

Unten ist das C++-Programm, um alle Faktoren einer Zahl zu finden:

 // C++ program to find all factors of a natural number
#include <bits/stdc++.h>
using namespace std;
void findFactors(int num)
{
for(int i=1; i<=sqrt(num); i++)
{
if(num%i == 0)
{
if(num/i == i)
{
cout << i << " ";
}
else
{
cout << i << " " << num/i << " ";
}
}
}
cout << endl;
}
int main()
{
int num1 = 60;
cout << "Factors of " << num1 << " are: " << endl;
findFactors(num1);
int num2 = 100;
cout << "Factors of " << num2 << " are: " << endl;
findFactors(num2);
int num3 = 85;
cout << "Factors of " << num3 << " are: " << endl;
findFactors(num3);
int num4 = 66;
cout << "Factors of " << num4 << " are: " << endl;
findFactors(num4);
int num5 = 71;
cout << "Factors of " << num5 << " are: " << endl;
findFactors(num5);
return 0;
}

Ausgabe:

 Factors of 60 are:
1 60 2 30 3 20 4 15 5 12 6 10
Factors of 100 are:
1 100 2 50 4 25 5 20 10
Factors of 85 are:
1 85 5 17
Factors of 66 are:
1 66 2 33 3 22 6 11
Factors of 71 are:
1 71

Verwandte: Wie man einen String in C++, Python und JavaScript umkehrt

Python-Programm, das einen optimierten Ansatz verwendet, um Faktoren einer Zahl zu finden

Unten ist das Python-Programm, um alle Faktoren einer Zahl zu finden:

 # Python program to find all factors of a natural number
import math
def findFactors(num):
i = 1
while i <= math.sqrt(num):
if (num%i==0):
if (num/i == i):
print(i, end=" ")
else:
print(i, num//i, end=" ")
i = i + 1
print()
num1 = 60
print("Factors of", num1, "are:")
findFactors(num1)
num2 = 100
print("Factors of", num2, "are:")
findFactors(num2)
num3 = 85
print("Factors of", num3, "are:")
findFactors(num3)
num4 = 66
print("Factors of", num4, "are:")
findFactors(num4)
num5 = 71
print("Factors of", num5, "are:")
findFactors(num5)

Ausgabe:

 Factors of 60 are:
1 60 2 30 3 20 4 15 5 12 6 10
Factors of 100 are:
1 100 2 50 4 25 5 20 10
Factors of 85 are:
1 85 5 17
Factors of 66 are:
1 66 2 33 3 22 6 11
Factors of 71 are:
1 71

JavaScript-Programm mit optimiertem Ansatz zum Auffinden von Faktoren einer Zahl

Unten ist das JavaScript-Programm, um alle Faktoren einer Zahl zu finden:

 // JavaScript program to find all factors of a natural number
function findFactors(num) {
for(let i=1; i<=Math.sqrt(num); i++) {
if(num%i == 0) {
if (parseInt(num/i, 10) == i)
{
document.write(i + " ");
} else {
document.write(i + " " + parseInt(num/i, 10) + " ");
}
}
}
document.write("<br>");
}

let num1 = 60;
document.write("Factors of " + num1 + " are: " + "<br>");
findFactors(num1);
let num2 = 100;
document.write("Factors of " + num2 + " are: " + "<br>");
findFactors(num2);
let num3 = 85;
document.write("Factors of " + num3 + " are: " + "<br>");
findFactors(num3);
let num4 = 66;
document.write("Factors of " + num4 + " are: " + "<br>");
findFactors(num4);
let num5 = 71;
document.write("Factors of " + num5 + " are: " + "<br>");
findFactors(num5);

Ausgabe:

 Factors of 60 are:
1 60 2 30 3 20 4 15 5 12 6 10
Factors of 100 are:
1 100 2 50 4 25 5 20 10
Factors of 85 are:
1 85 5 17
Factors of 66 are:
1 66 2 33 3 22 6 11
Factors of 71 are:
1 71

Grundlegende Programmierprinzipien verstehen

Als Programmierer ist es sehr wichtig, grundlegende Programmierprinzipien wie KISS (Keep It Simple, Stupid), DRY (Don't Repeat Yourself), Single Responsibility, YAGNI (Du wirst es nicht brauchen), Open/Closed, Zusammensetzung über Vererbung usw.

Das Befolgen der grundlegenden Programmierprinzipien macht Sie letztendlich zu einem besseren Programmierer.