重复连接中点形成的正方形面积
给定一个边长为L的正方形。通过连接第一个正方形的边的中点,在第一个正方形内形成另一个正方形。现在第三个正方形通过连接第二个正方形的边的中点在第二个正方形内形成,依此类推。
你们之间有 10 个方格。并且你得到最大正方形的边长。你必须找到这 10 个正方形的面积。
例子:
Input : L = 5, n = 10
Output :49.9512
Input : L = 2, n = 2
Output :7.99219
可以使用描述整个问题的图表来理解本文。
从图中——
最外面正方形的面积将由 L^2 给出。
第二个正方形的面积 - 由于第二个正方形的长度可以使用毕达哥拉斯定理计算,因此该正方形的边将为 \\
第三个正方形的面积-
同样可以计算第三个正方形的面积,我们得到
4平方的面积-
类似地,可以计算出四平方的面积,我们得到
因此,这种形式的几何级数,第一项为 L^2,公比 = 1/2。
现在我们必须找到这个 GP 的前 n 项的总和。
C++
// C++ program to find Area of squares
// formed by joining mid points repeatedly
#include
#define ll long long int
using namespace std;
int main()
{
double L = 2, n = 10;
double firstTerm = L * L;
double ratio = 1 / 2.0;
// apply GP sum formula
double sum = firstTerm * (pow(ratio, 10) - 1) / (ratio - 1);
cout << sum << endl;
return 0;
}
Java
// Java program to find Area of squares
// formed by joining mid points repeatedly
import java.util.*;
import java.lang.*;
import java.io.*;
import java.lang.Math;
/* Name of the class has to be "Main" only if the class is public. */
class GFG
{
public static void main (String[] args) throws java.lang.Exception
{
double L = 2, n = 10;
double firstTerm = L * L;
double ratio = 1 / 2.0;
// apply GP sum formula
double sum = firstTerm * (Math.pow(ratio, 10) - 1) / (ratio - 1);
System.out.println(sum) ;
}
}
// This code is contributed by Shrikant13
Python 3
# Python3 program to find Area of squares
# formed by joining mid points repeatedly
if __name__=='__main__':
L = 2
n = 10
firstTerm = L * L
ratio = 1 / 2
# apply GP sum formula
sum = firstTerm * ((ratio**10) - 1) / (ratio - 1)
print(sum)
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to find area of squares
// formed by joining mid points repeatedly
using System;
// Name of the class has to be
// "Main" only if the class is public.
class GFG{
public static void Main(string[] args)
{
double L = 2;
//double n = 10;
double firstTerm = L * L;
double ratio = 1 / 2.0;
// Apply GP sum formula
double sum = firstTerm *
(Math.Pow(ratio, 10) - 1) /
(ratio - 1);
Console.Write(Math.Round(sum, 5));
}
}
// This code is contributed by rutvik_56
Javascript
输出:
7.99219
时间复杂度: O(1)