You are viewing a single comment's thread. Return to all comments →
O(1) Solution : say N= 100 so, max multiple of 3 here below N is 99 i.e. 3*33 so, sum of all multiples of 3 has a pattern 3(1+2+3+....+33) use this
Here is the code in C++
long N,num,three,five,fifteen=0; cin>>N; for(int i=0;i<N;i++) { cin>>num; //int sum=0; three=(num-1)/3; five=(num-1)/5; fifteen=(num-1)/15; cout << 3*(three*(three+1)/2)+5*(five*(five+1)/2)-15*(fifteen*(fifteen+1)/2)<<endl; } return 0;
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #1: Multiples of 3 and 5
You are viewing a single comment's thread. Return to all comments →
O(1) Solution : say N= 100 so, max multiple of 3 here below N is 99 i.e. 3*33 so, sum of all multiples of 3 has a pattern 3(1+2+3+....+33) use this
Here is the code in C++