📌  相关文章
📜  打印给定范围内的所有理想正方形

📅  最后修改于: 2021-05-04 17:30:09             🧑  作者: Mango

给定范围[L,R] ,任务是打印给定范围内的所有理想正方形。
例子:

天真的方法:从L到R,检查当前元素是否为理想正方形。如果是,则打印它。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print all the perfect
// squares from the given range
void perfectSquares(float l, float r)
{
 
    // For every element from the range
    for (int i = l; i <= r; i++) {
 
        // If current element is
        // a perfect square
        if (sqrt(i) == (int)sqrt(i))
            cout << i << " ";
    }
}
 
// Driver code
int main()
{
    int l = 2, r = 24;
 
    perfectSquares(l, r);
 
    return 0;
}


Java
//Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to print all the perfect
// squares from the given range
static void perfectSquares(int l, int r)
{
 
    // For every element from the range
    for (int i = l; i <= r; i++)
    {
 
        // If current element is
        // a perfect square
        if (Math.sqrt(i) == (int)Math.sqrt(i))
            System.out.print(i + " ");
    }
}
 
// Driver code
public static void main (String[] args)
{
    int l = 2, r = 24;
    perfectSquares(l, r);
}
}
 
// This code is contributed by jit_t


Python3
# Python3 implementation of the approach
 
# Function to print all the perfect
# squares from the given range
def perfectSquares(l, r):
 
    # For every element from the range
    for i in range(l, r + 1):
 
        # If current element is
        # a perfect square
        if (i**(.5) == int(i**(.5))):
            print(i, end=" ")
 
# Driver code
l = 2
r = 24
 
perfectSquares(l, r)
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to print all the perfect
// squares from the given range
static void perfectSquares(int l, int r)
{
 
    // For every element from the range
    for (int i = l; i <= r; i++)
    {
 
        // If current element is
        // a perfect square
        if (Math.Sqrt(i) == (int)Math.Sqrt(i))
            Console.Write(i + " ");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int l = 2, r = 24;
    perfectSquares(l, r);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print all the perfect
// squares from the given range
void perfectSquares(float l, float r)
{
 
    // Getting the very first number
    int number = ceil(sqrt(l));
 
    // First number's square
    int n2 = number * number;
 
    // Next number is at the difference of
    number = (number * 2) + 1;
 
    // While the perfect squares
    // are from the range
    while ((n2 >= l && n2 <= r)) {
 
        // Print the perfect square
        cout << n2 << " ";
 
        // Get the next perfect square
        n2 = n2 + number;
 
        // Next odd number to be added
        number += 2;
    }
}
 
// Driver code
int main()
{
    int l = 2, r = 24;
 
    perfectSquares(l, r);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function to print all the perfect
// squares from the given range
static void perfectSquares(float l, float r)
{
 
    // Getting the very first number
    int number = (int) Math.ceil(Math.sqrt(l));
 
    // First number's square
    int n2 = number * number;
 
    // Next number is at the difference of
    number = (number * 2) + 1;
 
    // While the perfect squares
    // are from the range
    while ((n2 >= l && n2 <= r))
    {
 
        // Print the perfect square
        System.out.print(n2 + " ");
 
        // Get the next perfect square
        n2 = n2 + number;
 
        // Next odd number to be added
        number += 2;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int l = 2, r = 24;
 
    perfectSquares(l, r);
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
 
from math import ceil, sqrt
 
# Function to print all the perfect
# squares from the given range
def perfectSquares(l, r) :
 
 
    # Getting the very first number
    number = ceil(sqrt(l));
 
    # First number's square
    n2 = number * number;
 
    # Next number is at the difference of
    number = (number * 2) + 1;
 
    # While the perfect squares
    # are from the range
    while ((n2 >= l and n2 <= r)) :
 
        # Print the perfect square
        print(n2, end= " ");
 
        # Get the next perfect square
        n2 = n2 + number;
 
        # Next odd number to be added
        number += 2;
 
# Driver code
if __name__ == "__main__" :
 
    l = 2; r = 24;
 
    perfectSquares(l, r);
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to print all the perfect
// squares from the given range
static void perfectSquares(float l, float r)
{
 
    // Getting the very first number
    int number = (int) Math.Ceiling(Math.Sqrt(l));
 
    // First number's square
    int n2 = number * number;
 
    // Next number is at the difference of
    number = (number * 2) + 1;
 
    // While the perfect squares
    // are from the range
    while ((n2 >= l && n2 <= r))
    {
 
        // Print the perfect square
        Console.Write(n2 + " ");
 
        // Get the next perfect square
        n2 = n2 + number;
 
        // Next odd number to be added
        number += 2;
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int l = 2, r = 24;
 
    perfectSquares(l, r);
}
}
 
// This code is contributed by Rajput Ji


Javascript


输出:
4 9 16

它是O(n)的解。此外,平方根数的使用导致计算费用。
高效的方法:该方法基于以下事实:数字L之后的第一个完美平方肯定是ssqrt(L) the的平方。用非常简单的术语来说, L的平方根将非常接近我们试图找到其平方根的数字。因此,该数字将为pow(ceil(sqrt(L)),2)
对于此方法,第一个完美的正方形非常重要。现在,原始答案已隐藏在该模式下,即0 1 4 9 16 25
0和1之差为1
1和4之差是3
4和9之间的差是5,依此类推…
这意味着两个完美平方之间的差总是一个奇数。
现在,出现了一个问题,必须添加什么才能得到下一个数字,答案是(sqrt(X)* 2)+ 1 ,其中X是已知的理想平方。
假设当前的理想平方为4,那么下一个理想平方将肯定为4 +(sqrt(4)* 2 +1)= 9 。在这里,数字5被添加,下一个要添加的数字将是7然后是9 ,依此类推……这将产生一系列的奇数。
加法在计算上比执行乘法或查找每个数字的平方根便宜。
下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print all the perfect
// squares from the given range
void perfectSquares(float l, float r)
{
 
    // Getting the very first number
    int number = ceil(sqrt(l));
 
    // First number's square
    int n2 = number * number;
 
    // Next number is at the difference of
    number = (number * 2) + 1;
 
    // While the perfect squares
    // are from the range
    while ((n2 >= l && n2 <= r)) {
 
        // Print the perfect square
        cout << n2 << " ";
 
        // Get the next perfect square
        n2 = n2 + number;
 
        // Next odd number to be added
        number += 2;
    }
}
 
// Driver code
int main()
{
    int l = 2, r = 24;
 
    perfectSquares(l, r);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
 
// Function to print all the perfect
// squares from the given range
static void perfectSquares(float l, float r)
{
 
    // Getting the very first number
    int number = (int) Math.ceil(Math.sqrt(l));
 
    // First number's square
    int n2 = number * number;
 
    // Next number is at the difference of
    number = (number * 2) + 1;
 
    // While the perfect squares
    // are from the range
    while ((n2 >= l && n2 <= r))
    {
 
        // Print the perfect square
        System.out.print(n2 + " ");
 
        // Get the next perfect square
        n2 = n2 + number;
 
        // Next odd number to be added
        number += 2;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int l = 2, r = 24;
 
    perfectSquares(l, r);
 
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 implementation of the approach
 
from math import ceil, sqrt
 
# Function to print all the perfect
# squares from the given range
def perfectSquares(l, r) :
 
 
    # Getting the very first number
    number = ceil(sqrt(l));
 
    # First number's square
    n2 = number * number;
 
    # Next number is at the difference of
    number = (number * 2) + 1;
 
    # While the perfect squares
    # are from the range
    while ((n2 >= l and n2 <= r)) :
 
        # Print the perfect square
        print(n2, end= " ");
 
        # Get the next perfect square
        n2 = n2 + number;
 
        # Next odd number to be added
        number += 2;
 
# Driver code
if __name__ == "__main__" :
 
    l = 2; r = 24;
 
    perfectSquares(l, r);
 
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to print all the perfect
// squares from the given range
static void perfectSquares(float l, float r)
{
 
    // Getting the very first number
    int number = (int) Math.Ceiling(Math.Sqrt(l));
 
    // First number's square
    int n2 = number * number;
 
    // Next number is at the difference of
    number = (number * 2) + 1;
 
    // While the perfect squares
    // are from the range
    while ((n2 >= l && n2 <= r))
    {
 
        // Print the perfect square
        Console.Write(n2 + " ");
 
        // Get the next perfect square
        n2 = n2 + number;
 
        // Next odd number to be added
        number += 2;
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int l = 2, r = 24;
 
    perfectSquares(l, r);
}
}
 
// This code is contributed by Rajput Ji

Java脚本


输出:
4 9 16