Files
test/ProjectEuler/p001_MultipleOf3and5.cpp

40 lines
673 B
C++

/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
*/
#include <iostream>
/*
Multiples of 3 and 5
Problem 1
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.
Solution:
233168
*/
/*
Sum of all numbers below n multiple of m
*/
int sum(int n, int m )
{
int c = n/m;
return m*c*(c+1)/2;
}
int main ( void )
{
const int l = 999;
const int o1 = 3;
const int o2 = 5;
int s = sum( l, o1 ) + sum( l, o2 ) - sum( l, o1*o2 );
std::cout << s << std::endl;
return 0;
}