The sum of the square sums of the first n natural numbers
The sum of the squares of the first n natural numbers is the sum of the squares of up to n terms. This series finds the sum of every number up to n and adds the sum to the sum variable.
The sum of the square sums of the first 4 natural numbers is -
sum = (12) (12 22 ) (12 22 32) (12 22 3 2 4 2 ) = 1 5 14 30 = 50
There are two ways to find the sum of the squares of the first n natural numbers.
1) Use a for loop.
In this method we will loop through each number from 1 to N and find the sum of squares and then add this sum of squares to the sum variable. This method requires iteration over n numbers, so will be very time consuming for larger numbers.
Example
#include <stdio.h> int main() { int n = 6; int sum = 0; for (int i = 1; i <= n; i++) sum += ((i * (i + 1) * (2 * i + 1)) / 6); printf("The square-sum of first %d natural number is %d",n,sum); return 0; }
Output
The square-sum of first 6 natural number is 196
2) Use mathematical formula −
based on finding the nth term of the sequence and the general formula , derive mathematical formulas for summing. The formula for finding the sum of the squares of the first n natural numbers is sum = n*(n 1)*(n 1)*(n 2)/12
According to this formula we can write a program to find the sum,
Example
#include <stdio.h> int main() { int n = 6; int sum = (n*(n+1)*(n+1)*(n+2))/12; printf("The square-sum of first %d natural number is %d",n,sum); return 0; }
Output
The square-sum of first 6 natural number is 196
The above is the detailed content of The sum of the square sums of the first n natural numbers. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Which one is better, Celeron g4900 or i36100? When it comes to the two processors Celeron G4900 and I36100, there is no doubt that the performance of I36100 is superior. Celeron processors are generally considered low-end processors and are primarily used in budget laptops. The I3 processor is mainly used for high-end processors, and its performance is very good. Whether you are playing games or watching videos, you will not experience any lagging when using the I3 processor. Therefore, if possible, try to buy Intel I-series processors, especially for desktop computers, so that you can enjoy the fun of the online world. How is the performance of the Celeron G4900T? From a performance perspective, the Pentium G4900T performs well in terms of frequency. Compared with the previous version, the CPU performance is

Which ASUS motherboard should be paired with the R55600? The ASUS ROGStrixB550-FGaming motherboard is an excellent choice. It is perfectly compatible with Ryzen55600X processor and provides excellent performance and features. This motherboard has a reliable power supply system, can support overclocking, and provides a wealth of expansion slots and ports to meet daily use and gaming needs. ROGStrixB550-FGaming is also equipped with high-quality audio solutions, fast network connections and reliable heat dissipation design to ensure that the system remains efficient and stable. In addition, this motherboard adopts a gorgeous ROG style and is equipped with gorgeous RGB lighting effects, adding visual enjoyment to your computer. All in all, ASUS ROGStri

The average of the squares of natural numbers is calculated by adding all the squares of n natural numbers and then dividing by that number. The first two natural numbers in the example are 2.5, 12+22=5=>5/2=2.5. There are two methods of calculation in programming - Using loops Using formulas Calculating the average of squares of natural numbers using loops This logic works by finding the squares of all natural numbers. Find the square of each by looping from 1 to n and add to the sum variable. Then divide that sum by n. Program to calculate the sum of squares of natural numbers - sample code real-time demonstration #include<stdio.h>intmain(){ intn=2;

Which one is better, Huntkey s980 or Bauhaus? Huntkey S980 and Bauhaus (BeQuiet) are two different brands of computer power supply (PSU) manufacturers. Which one is better to choose depends on your needs and personal preferences. It has been recognized and praised by consumers. Their products are widely used in personal computers, servers, industrial equipment and other fields. Huntkey is committed to providing high-quality power products and constantly introducing new technologies and innovative designs. Their products undergo strict quality control and testing to ensure they can power devices stably and efficiently. Huntkey also pays attention to environmental protection and energy conservation, and strives to reduce its impact on the environment. Their power supplies comply with international standards and have received multiple certifications and awards. As a reputable brand

The series of squares of the first n odd numbers takes the square of the first n odd numbers in the series. The series is: 1,9,25,49,81,121…The series can also be written as -12,32,52,72,92,112….The sum of this series has a mathematical formula -n(2n+1)(2n -1)/3=n(4n2-1)/3For example, Input:N=4Output:sum=Explanation 12+32+52+72=1+9+25+49=84 Using the formula, sum=4 (4(4)2-1)/3=4(64-1)/3=4(63)/3=4*21=84 Both methods are good, but the method using mathematical formulas is better , which reduces the time complexity since it does not use a look

How to turn off the mouse and keyboard lights when Gigabyte motherboard GA78LMTS2 is turned off? For help, please check whether there is "Deeppoweroffmode" in the BIOS, that is, deep power-down mode. This option is located in the Power settings option of the BIOS. If this option is set to enabled, the device will not light up after shutting down the computer. The differences between Gigabyte motherboard DLED and LED are as follows: 1. Different positions: direct-type DLED is behind the LCD panel, and the LED lights are arranged in a matrix; side In-type LED installs the LED lights at the frame of the panel so that the light source shines out from the side. 2. Energy saving: Side-in LED TVs save more power than direct-type DLED TVs. The difference between DLED and LED is that DLED

In this article, we will discuss the program to find pairs with a given sum in a given matrix. For example -Input:matrix[n][m]={ {4,6,4,65}, {56,1,12,32}, {4,5,6,44}, {13,9,11, 25}},SUM=20Output:Pairexists.Explanation:Sum=20isequaltothesumofnumbers9an

Twoelementsgivingthemaximumsuminanarraymeans,wehavetofindtwolargestarrayelementswhichwilleventuallygivethemaximumsumpossible.InthisarticlewewillseehowwecanfindthemaximumsumoftwoelementsinJava.ToshowyousomeinstancesThe Chinese translation of Instance-1 is: Example-1Supposewehaveth
