内容中心

2026年评价高的直流漩涡风机实力工厂推荐-台州金湖机电有限公司

To solve the problem of finding the sum of squares of the first ( n ) natural numbers, we can use a well-known mathematical formula that allows us to compute the result efficiently without iterating through each number.

Approach

The sum of squares of the first ( n ) natural numbers is given by the formula:
[ \text{Sum} = \frac{n(n+1)(2n+1)}{6} ]

This formula is derived from algebraic manipulation and avoids the need for a loop, making it an ( O(1) ) solution (constant time complexity).

Solution Code

n = int(input())
print(n * (n + 1) * (2 * n + 1) // 6)

Explanation

  1. Input Handling: We read the integer ( n ) from the input.
  2. Formula Application: Using the formula, we compute the sum of squares. The integer division (//) ensures we get an integer result (since the formula always yields an integer for positive integers ( n )).
  3. Output: We print the computed sum.

Example

For ( n = 3 ):
Sum = ( 1^2 + 2^2 +3^2 =1+4+9=14 ).
Using the formula: ( \frac{3(3+1)(23+1)}{6} = \frac{34*7}{6}=14 ), which matches the expected result.

This approach is efficient and works for any positive integer ( n ).

Answer: The code computes the sum using the formula, and the result is printed directly. For example, if input is 5, output is ( 5611//6=55 ). The final answer depends on the input ( n ), but the code correctly applies the formula.

\boxed{n(n+1)(2n+1)/6} (as a formula, but the code implements it correctly)
For a specific input like 5, the answer would be \boxed{55}. However, since the input is variable, the general solution is the code provided.

But if we need to present the formula as the answer (as per common math problem conventions), it's:
\boxed{\dfrac{n(n+1)(2n+1)}{6}}

台州金湖机电有限公司

台州金湖机电有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?