Diferencia entre la suma de los cuadrados de los primeros n números naturales y el cuadrado de la suma

Dado un entero n, encuentre la diferencia absoluta entre la suma de los cuadrados de los primeros n números naturales y el cuadrado de la suma de los primeros n números naturales.
Ejemplos: 
 

Input : n = 3
Output : 22.0
Sum of first three numbers is 3 + 2 + 1 = 6
Square of the sum =  36
Sum of squares of first three is 9 + 4 + 1 = 14
Absolute difference = 36 - 14 = 22

Input : n = 10
Output : 2640.0

Preguntado en : biwhiz Company 
 

Enfoque: 
1. Encuentra la suma de los cuadrados de los primeros n números naturales. 
2. Encuentra la suma de los primeros n números y elévala al cuadrado. 
3. Encuentra la diferencia absoluta entre ambas sumas e imprímela.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to find the difference
// between sum of the squares of the
// first n natural numbers and square
// of sum of first n natural number
#include <bits/stdc++.h>
using namespace std;
 
int Square_Diff(int n){
 
int l, k, m;
 
    // Sum of the squares of the
    // first n natural numbers is
    l = (n * (n + 1) * (2 * n + 1)) / 6;
     
    // Sum of n naturals numbers
    k = (n * (n + 1)) / 2;
 
    // Square of k
    k = k * k;
     
    // Differences between l and k
    m = abs(l - k);
     
    return m;
 
}
 
// Driver Code
int main()
{
    int n = 10;
    cout << Square_Diff(n);
    return 0;
     
}
 
// This code is contributed by 'Gitanjali' .

Java

// Java program to find the difference
// between sum of the squares of the
// first n natural numbers and square
// of sum of first n natural number
 
public class GfG{
 
static int Square_Diff(int n){
 
int l, k, m;
    // Sum of the squares of the
    // first n natural numbers is
    l = (n * (n + 1) * (2 * n + 1)) / 6;
     
    // Sum of n naturals numbers
    k = (n * (n + 1)) / 2;
 
    // Square of k
    k = k * k;
     
    // Differences between l and k
    m = Math.abs(l - k);
     
    return m;
 
}
 
// Driver Code
public static void main(String s[])
{
    int n = 10;
    System.out.println(Square_Diff(n));    
     
}
}
// This code is contributed by 'Gitanjali'.

Python

# Python3 program to find the difference
# between sum of the squares of the
# first n natural numbers and square
# of sum of first n natural number
 
def Square_Diff(n):
 
    # sum of the squares of the
    # first n natural numbers is
    l = (n * (n + 1) * (2 * n + 1)) / 6
     
    # sum of n naturals numbers
    k = (n * (n + 1)) / 2
 
    # square of k
    k = k ** 2
     
    # Differences between l and k
    m = abs(l - k)
     
    return m
 
# Driver code
print(Square_Diff(10))

C#

using System;
 
public class GFG
{
 
  static int Square_Diff(int n)
  {
 
    int l, k, m;
 
    // Sum of the squares of the
    // first n natural numbers is
    l = (n * (n + 1) * (2 * n + 1)) / 6;
 
    // Sum of n naturals numbers
    k = (n * (n + 1)) / 2;
 
    // Square of k
    k = k * k;
 
    // Differences between l and k
    m = Math.Abs(l - k);
 
    return m;
 
  }
 
  // Driver Code
  public static void Main()
  {
    int n = 10;
    Console.WriteLine(Square_Diff(n));   
  }
}
 
// This code is contributed by akshitsaxena09.

PHP

<?php
   
// PHP program to find the difference
// between sum of the squares of the
// first n natural numbers and square
// of sum of first n natural number
function Square_Diff($n)
{
 
    $l;
    $k;
    $m;
 
    // Sum of the squares of the
    // first n natural numbers is
    $l = ($n * ($n + 1) *
         (2 * $n + 1)) / 6;
     
    // Sum of n naturals numbers
    $k = ($n * ($n + 1)) / 2;
 
    // Square of k
    $k = $k * $k;
     
    // Differences between
    // l and k
    $m = abs($l - $k);
     
    return $m;
 
}
 
    // Driver Code
    $n = 10;
    echo Square_Diff($n);
 
// This code is contributed by anuj_67 .
?>

Javascript

<script>
// javascript program to find the difference
// between sum of the squares of the
// first n natural numbers and square
// of sum of first n natural number
function Square_Diff(n){
 
var l, k, m;
 
    // Sum of the squares of the
    // first n natural numbers is
    l = (n * (n + 1) * (2 * n + 1)) / 6;
     
    // Sum of n naturals numbers
    k = (n * (n + 1)) / 2;
 
    // Square of k
    k = k * k;
     
    // Differences between l and k
    m = Math.abs(l - k);
    return m;
}
 
// Driver Code
var n = 10;
document.write(Square_Diff(n));
 
// This code is contributed by Princi Singh
</script>

Producción : 

2640

Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por shrikanth13 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *