Comment trouver le nombre de carrés parfaits entre deux nombres avec la programmation
La programmation est un moyen de demander à l'ordinateur d'effectuer diverses tâches. Vous pouvez résoudre une variété de problèmes en utilisant la programmation, y compris une vaste gamme de problèmes mathématiques. Dans cet article, vous apprendrez à trouver le nombre total de carrés parfaits entre deux nombres à l'aide de C++, Python et JavaScript.
Énoncé du problème
On vous donne deux nombres num1 et num2 . Vous devez trouver le nombre total de carrés parfaits entre num1 et num2 .
Exemple 1 : Soit num1 = 10 et num2 = 100.
Carrés parfaits entre 10 et 100 : 16, 25, 36, 49, 64, 81, 100.
Ainsi, la sortie est 7.
Exemple 2 : Soit num1 = 15 et num2 = 82.
Carrés parfaits entre 15 et 82 : 16, 25, 36, 49, 64, 81.
Ainsi, la sortie est de 6.
Exemple 3 : Soit num1 = 3 et num2 = 36.
Carrés parfaits entre 3 et 36 : 4, 9, 16, 25, 36.
Ainsi, la sortie est 5.
Approche de base pour trouver le nombre total de carrés parfaits entre deux nombres
Programme C++ pour compter le nombre total de carrés parfaits entre deux nombres
Vous trouverez ci-dessous l'implémentation C++ pour résoudre le problème :
// C++ program to count the total number of
// perfect squares between 2 numbers
#include <iostream>
using namespace std;
int countTotalSquares(int num1, int num2)
{
int result = 0;
for(int i=num1; i<=num2; i++)
{
for(int j=1; j*j<=i; j++)
{
if(j*j == i)
{
result++;
}
}
}
return result;
}
int main()
{
int num1 = 10;
int num2 = 100;
cout << "Total number of perfect squares between " << num1 << " and " << num2 << ": " << countTotalSquares(num1, num2) << endl;
int num3 = 15;
int num4 = 82;
cout << "Total number of perfect squares between " << num3 << " and " << num4 << ": " << countTotalSquares(num3, num4) << endl;
int num5 = 3;
int num6 = 36;
cout << "Total number of perfect squares between " << num5 << " and " << num6 << ": " << countTotalSquares(num5, num6) << endl;
int num7 = 12;
int num8 = 65;
cout << "Total number of perfect squares between " << num7 << " and " << num8 << ": " << countTotalSquares(num7, num8) << endl;
return 0;
}
Sortie :
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
Programme Python pour compter le nombre total de carrés parfaits entre deux nombres
Ensuite, le code Python pour trouver le nombre total de carrés parfaits entre deux nombres :
# Python program to count the total number of
# perfect squares between 2 numbers
def countTotalSquares(num1, num2):
result = 0
for i in range(num1, num2+1):
j = 1
while j * j <= i:
if j * j == i:
result = result + 1
j = j + 1
return result
num1 = 10
num2 = 100
print("Total number of perfect squares between", num1, "and", num2, ":", countTotalSquares(num1, num2))
num3 = 15
num4 = 82
print("Total number of perfect squares between", num3, "and", num4, ":", countTotalSquares(num3, num4))
num5 = 3
num6 = 36
print("Total number of perfect squares between", num5, "and", num6, ":", countTotalSquares(num5, num6))
num7 = 12
num8 = 65
print("Total number of perfect squares between", num7, "and", num8, ":", countTotalSquares(num7, num8))
Sortie :
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
Programme JavaScript pour compter le nombre total de carrés parfaits entre deux nombres
Voici comment trouver le nombre total de carrés parfaits entre deux nombres en JavaScript :
// JavaScript program to count the total number of
// perfect squares between 2 numbers
function countTotalSquares(num1, num2) {
var result = 0;
for(let i=num1; i<=num2; i++) {
for(let j=1; j*j<=i; j++) {
if(j*j == i) {
result++;
}
}
}
return result;
}
var num1 = 10;
var num2 = 100;
document.write("Total number of perfect squares between " + num1 + " and " + num2 + ": " + countTotalSquares(num1, num2) + "<br>");
var num3 = 15;
var num4 = 82;
document.write("Total number of perfect squares between " + num3 + " and " + num4 + ": " + countTotalSquares(num3, num4) + "<br>");
var num5 = 3;
var num6 = 36;
document.write("Total number of perfect squares between " + num5 + " and " + num6 + ": " + countTotalSquares(num5, num6) + "<br>");
var num7 = 12;
var num8 = 65;
document.write("Total number of perfect squares between " + num7 + " and " + num8 + ": " + countTotalSquares(num7, num8) + "<br>");
Sortie :
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
Approche efficace pour trouver le nombre de carrés parfaits entre deux nombres
Vous pouvez également trouver le nombre total de carrés parfaits entre deux nombres en utilisant la formule suivante :
Nombre total de carrés parfaits entre num1 et num2 = floor(sqrt(num2)) - ceil(sqrt(num1)) + 1
La complexité temporelle de cette solution ( O(log num2) ) est meilleure que l'approche précédente ( O((num2-num1) * sqrt(num2)) ).
Implémentation C++ à l'aide d'une formule efficace
Vous trouverez ci-dessous l'implémentation C++ pour trouver le nombre total de carrés parfaits entre deux nombres :
// C++ program to count the total number of
// perfect squares between 2 numbers
#include <bits/stdc++.h>
using namespace std;
int countTotalSquares(int num1, int num2)
{
return (floor(sqrt(num2)) - ceil(sqrt(num1)) + 1);
}
int main()
{
int num1 = 10;
int num2 = 100;
cout << "Total number of perfect squares between " << num1 << " and " << num2 << ": " << countTotalSquares(num1, num2) << endl;
int num3 = 15;
int num4 = 82;
cout << "Total number of perfect squares between " << num3 << " and " << num4 << ": " << countTotalSquares(num3, num4) << endl;
int num5 = 3;
int num6 = 36;
cout << "Total number of perfect squares between " << num5 << " and " << num6 << ": " << countTotalSquares(num5, num6) << endl;
int num7 = 12;
int num8 = 65;
cout << "Total number of perfect squares between " << num7 << " and " << num8 << ": " << countTotalSquares(num7, num8) << endl;
return 0;
}
Sortie :
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
Implémentation Python à l'aide d'une formule efficace
Vous trouverez ci-dessous l'implémentation Python pour trouver le nombre total de carrés parfaits entre deux nombres :
# Python program to count the total number of
# perfect squares between 2 numbers
def countTotalSquares(num1, num2):
return (math.floor(math.sqrt(num2)) - math.ceil(math.sqrt(num1)) + 1)
num1 = 10
num2 = 100
print("Total number of perfect squares between", num1, "and", num2, ":", countTotalSquares(num1, num2))
num3 = 15
num4 = 82
print("Total number of perfect squares between", num3, "and", num4, ":", countTotalSquares(num3, num4))
num5 = 3
num6 = 36
print("Total number of perfect squares between", num5, "and", num6, ":", countTotalSquares(num5, num6))
num7 = 12
num8 = 65
print("Total number of perfect squares between", num7, "and", num8, ":", countTotalSquares(num7, num8))
Sortie :
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
Implémentation de JavaScript à l'aide d'une formule efficace
Vous trouverez ci-dessous l'implémentation JavaScript pour trouver le nombre total de carrés parfaits entre deux nombres :
// JavaScript program to count the total number of
// perfect squares between 2 numbers
function countTotalSquares(num1, num2) {
return (Math.floor(Math.sqrt(num2)) - Math.ceil(Math.sqrt(num1)) + 1);
}
var num1 = 10;
var num2 = 100;
document.write("Total number of perfect squares between " + num1 + " and " + num2 + ": " + countTotalSquares(num1, num2) + "<br>");
var num3 = 15;
var num4 = 82;
document.write("Total number of perfect squares between " + num3 + " and " + num4 + ": " + countTotalSquares(num3, num4) + "<br>");
var num5 = 3;
var num6 = 36;
document.write("Total number of perfect squares between " + num5 + " and " + num6 + ": " + countTotalSquares(num5, num6) + "<br>");
var num7 = 12;
var num8 = 65;
document.write("Total number of perfect squares between " + num7 + " and " + num8 + ": " + countTotalSquares(num7, num8) + "<br>");
Sortie :
Total number of perfect squares between 10 and 100: 7
Total number of perfect squares between 15 and 82: 6
Total number of perfect squares between 3 and 36: 5
Total number of perfect squares between 12 and 65: 5
Monétisez vos compétences en programmation
Le codage est considéré comme l'une des meilleures compétences professionnelles du 21e siècle. Il offre des opportunités illimitées pour vos idées créatives qui peuvent vous rapporter de l'argent. Vous pouvez gagner de l'argent en travaillant en indépendant, en écrivant des blogs techniques, en développant des applications et des API, en vendant des livres électroniques et des cours, etc. La seule façon de découvrir ce que vous aimez est de vous y plonger !