ProjectEuler #6 Sum square difference

https://projecteuler.net/problem=6


JS:

//(square of sum)-(sum of square)function squareofsum(number){

    function squareofsum(number){

        var sum=0;

        var i;

    for(i=1;i<=number;i++){

        sum=sum+i;

    }

    return sum*sum;

}

    function sumofsquare(number){

        var sum=0;

        var i;

    for(i=1;i<=number;i++){

        sum=sum+i*i;

    }

    return sum;

}

number=100;

result=squareofsum(number)-sumofsquare(number);

console.log(result);


C:

#include<stdio.h>


double squareofsum(int number){

    int i;

    int sum;

    for(i=1;i<=number;i++){

        sum=sum+i;

    }

    return sum*sum;

}


double sumofsquare(int number){

    int i;

    int sum;

    for(i=1;i<=number;i++){

        sum=sum+i*i;

    }

    return sum;

}


int main(void){

    int num=100;

    double result;

    result=squareofsum(num)-sumofsquare(num);

    printf("result=%.0lf",result);


    return 0;

}


© El.Viento. | Powered by LOFTER