giovedì 23 maggio 2013

Problema di Eulero 6

La formulazione del sesto problema di Eulero e' il seguente
----------------------------------------------------------

The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum

----------------------------------------------------------

In questo caso l'algoritmo e' molto piu' banale dei casi precedenti ma c'e' sempre da trarre un insegnamento. In C++ la variabili vanno sempre inizializzate

-----------------------------------------------------------

#include <iostream>

using namespace std;

int main()
{
    double somma_quadrati=0;
    int somma=0;
    for (int t=1;t<=100;t++)
    {
        somma = somma + t;
        somma_quadrati = somma_quadrati + (t*t);
        cout << t << endl;
    }
    double quadrato_somma = somma * somma;
    cout.precision(15);
    cout << fixed << (quadrato_somma-somma_quadrati) << endl;

    return 0;
}

-----------------------------------------------------------
La soluzione e' 25164150 con un tempo di calcolo inferiore al decimo di secondo
E' abbastanza chiaro che per ottenere la somma dei numeri inferiori a 100 esiste una formula da Gauss ma visto che comunque un ciclo era previsto, tanto vale sfruttarlo fino in fondo



Nessun commento:

Posta un commento

Physics informed neural network Fukuzono

Visto che puro ML non funziona per le serie tempo di cui mi sto occupando ed le regressioni basate su formule analitiche mostrano dei limiti...