Title / Description
Code #include <iostream> #include <ctime> using namespace std; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Problem: #1 Find the sum of all the multiples of 3 or 5 below 1000. // // Example: 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. // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int main() { clock_t tStart = clock(); //This line is to start the clock calculating execution time int sum=0, i=1, j=1; while(i*3<1000) { if((i*3)%5 == 0) i++; else { sum += (i*3); i++; } } while(j*5 <1000) { sum += (j*5); j++; } cout << sum << endl; printf("Execution Time: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC); //Stop clock and print execution time return 0; }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code