Cómo encontrar el número de cuadrados perfectos entre dos números con programación
La programación es una forma de instruir a la computadora para que realice varias tareas. Puede resolver una variedad de problemas mediante la programación, incluida una gran variedad de problemas matemáticos. En este artículo, aprenderá a encontrar el número total de cuadrados perfectos entre dos números usando C ++, Python y JavaScript.
Planteamiento del problema
Te dan dos números num1 y num2 . Necesitas encontrar el número total de cuadrados perfectos entre num1 y num2 .
Ejemplo 1 : Sea num1 = 10 y num2 = 100.
Cuadrados perfectos entre 10 y 100: 16, 25, 36, 49, 64, 81, 100.
Por tanto, la salida es 7.
Ejemplo 2 : Sea num1 = 15 y num2 = 82.
Cuadrados perfectos entre 15 y 82: 16, 25, 36, 49, 64, 81.
Por tanto, la salida es 6.
Ejemplo 3 : Sea num1 = 3 y num2 = 36.
Cuadrados perfectos entre 3 y 36: 4, 9, 16, 25, 36.
Por tanto, la salida es 5.
Método básico para encontrar el número total de cuadrados perfectos entre dos números
Programa C ++ para contar el número total de cuadrados perfectos entre dos números
A continuación se muestra la implementación de C ++ para resolver el problema:
// 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;
}
Salida :
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
Programa Python para contar el número total de cuadrados perfectos entre dos números
A continuación, el código Python para encontrar el número total de cuadrados perfectos entre dos números:
# 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))
Salida :
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
Programa JavaScript para contar el número total de cuadrados perfectos entre dos números
Así es como se encuentra el número total de cuadrados perfectos entre dos números 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>");
Salida :
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
Enfoque eficiente para encontrar el número de cuadrados perfectos entre dos números
También puedes encontrar el número total de cuadrados perfectos entre dos números usando la siguiente fórmula:
No total de cuadrados perfectos entre num1 y num2 = piso (sqrt (num2)) - ceil (sqrt (num1)) + 1
La complejidad temporal de esta solución ( O (log num2) ) es mejor que el enfoque anterior ( O ((num2-num1) * sqrt (num2)) ).
Implementación de C ++ usando una fórmula eficiente
A continuación se muestra la implementación de C ++ para encontrar el número total de cuadrados perfectos entre dos números:
// 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;
}
Salida :
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
Implementación de Python usando una fórmula eficiente
A continuación se muestra la implementación de Python para encontrar el número total de cuadrados perfectos entre dos números:
# 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))
Salida :
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
Implementación de JavaScript usando una fórmula eficiente
A continuación se muestra la implementación de JavaScript para encontrar el número total de cuadrados perfectos entre dos números:
// 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>");
Salida :
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
Monetice sus habilidades de programación
La codificación se considera una de las mejores habilidades profesionales del siglo XXI. Ofrece oportunidades ilimitadas para sus ideas creativas que pueden hacerle ganar algo de dinero. Puede ganar dinero trabajando independientemente, escribiendo blogs técnicos, desarrollando aplicaciones y API, vendiendo libros electrónicos y cursos, etc. ¡La única forma de descubrir cuál le gusta es sumergirse!