热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

打印给定范围内的所有完美方块

打印给定范围内的所有完美方块原文:https://www . geesforgeks . org/print-all-perfe

打印给定范围内的所有完美方块

原文:https://www . geesforgeks . org/print-all-perfect-squares-from-the-给定范围/

给定一个范围【L,R】,任务是打印给定范围内的所有完美方块。
例:

输入: L = 2,R = 24
输出: 4 9 16
输入: L = 1,R = 100
输出: 1 4 9 16 25 36 49 64 81 100

天真的做法:从 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 <    }
}
// 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

Python 3

# 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

java 描述语言


Output: 

4 9 16

它是含氧(氮)的溶液。此外,平方根数量的使用导致计算费用。
有效方法:这种方法基于这样一个事实,即在数字 L 之后的第一个完美正方形肯定是 ⌈sqrt(L)⌉ 的正方形。简单来说 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 <        // 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

Python 3

# 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 描述语言


Output: 

4 9 16

推荐阅读
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
author-avatar
117061771_af0556
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有