If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Answer
We just need to calculate the sum of all divisible by 3 then add the sum of all divisible by 5 and finally remove the sum of all divisible by 15 (both divisible by 3 and 5).
Code:
double gaussSum ( double number )
{
return ( number * ( number + 1 ) ) / 2 ;
}
//Proyect euler 1
int SumMulti3and5 ( int numero )
{
int total = ( 3 * gaussSum ( numero / 3 ) ) + ( 5 * gaussSum ( numero / 5 ) ) ;
int menos = 15 * gaussSum ( numero / 15 ) ;
return total - menos ;
}
{
return ( number * ( number + 1 ) ) / 2 ;
}
//Proyect euler 1
int SumMulti3and5 ( int numero )
{
int total = ( 3 * gaussSum ( numero / 3 ) ) + ( 5 * gaussSum ( numero / 5 ) ) ;
int menos = 15 * gaussSum ( numero / 15 ) ;
return total - menos ;
}
No comments:
Post a Comment