半径为 1,2,3,4……….N 的替代同心圆的面积之和
鉴于绘制了半径为1,2,3,4…….N cm 的同心圆。最小圆圈的内部被涂成白色,而有角的区域被交替涂成绿色和白色,因此没有两个相邻区域的颜色相同。任务是找出以平方厘米为单位的绿色区域的总面积。
例子:
Input: N=100
Output: 15865.042900628456
Input: N=10
Output: 172.78759594743863
方法:第一个绿色区域的面积将是-
[π(R2²- R1²)]
同样,所有替代绿色圆圈的面积将是-
[π(R4²- R3²)], [π(R6²- R5²)]…..
绿色区域的总面积是绿色区域的面积之和是-
A = [ π { (R2² – R1²) + (R4² – R3²) + (R6² – R5²) …………. (R(N)² – R(N-1)²}]
A = [ π { ((R2 – R1)(R2 + R1)) + ((R4 – R3)(R4 + R3)) + ((R6 – R5)(R6 + R5)) …………. ((RN – R(N – 1)(RN + R(N – 1)) } ]
由于 2 个同心圆的半径之差仅为 1 cm,因此R(N) – R(N – 1) = 1。因此,
A = [ π {R1 + R2 + R3 + R4 + R5 + R6 ………….R(N-1) + R(N) } ]
(R1 + R2 + R3 + R4 + R5 + R6 ………….R(N-1) + R(N) 形成一个等差数列,所以我们必须找到以起始半径为 1 和最后一个半径的等差数列之和为 N,公差为 1。
AP的总和是-
SN = N * (2 * a +(N – 1) * d) / 2
or
SN = N * (a + l) / 2
因此,绿色区域的面积是-
A = Sn * π
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to calculate area
// of concentric circles
double area(int N, int a, int l)
{
// Formula to find sum of
// Arithmetic Progression
double S = N * (a + l) / 2;
// return area
return S * M_PI;
}
// Driver code
int main()
{
// N is number of concentric circles
int N = 100;
// Radius of interior circle
int a = 1;
// l radius of exterior circle
int l = N;
// r is result
double r = area(N, a, l);
// Print result
cout << fixed << setprecision(12) << r;
}
// This code is contributed by Samim Hossain Mondal.
Java
// Java code for the above approach
class GFG
{
// Function to calculate area
// of concentric circles
static double area(int N, int a, int l)
{
// Formula to find sum of
// Arithmetic Progression
double S = N * (a + l) / 2;
// return area
return S * Math.PI;
}
// Driver code
public static void main(String args[])
{
// N is number of concentric circles
int N = 100;
// Radius of interior circle
int a = 1;
// l radius of exterior circle
int l = N;
// r is result
double r = area(N, a, l);
// Print result
System.out.println(r);
}
}
// This code is contributed by gfgking
Python3
# import pi from math module
from math import pi
# Function to calculate area
# of concentric circles
def area(N,a):
# Formula to find sum of
# Arithmetic Progression
S = N * (a + l) / 2
# return area
return S * pi
# N is number of concentric circles
N = 100
# Radius of interior circle
a = 1
# l radius of exterior circle
l = N
# r is result
r = area(N, a)
# Print result
print(r)
C#
// C# program for the above approach
using System;
class GFG
{
// Function to calculate area
// of concentric circles
static double area(int N, int a, int l)
{
// Formula to find sum of
// Arithmetic Progression
double S = N * (a + l) / 2;
// return area
return S * Math.PI;
}
// Driver Code:
public static void Main()
{
// N is number of concentric circles
int N = 100;
// Radius of interior circle
int a = 1;
// l radius of exterior circle
int l = N;
// r is result
double r = area(N, a, l);
// Print result
Console.WriteLine(r);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
15865.042900628456
时间复杂度: O(1)
辅助空间: O(1)