We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
longlongsum_of_multiples(intx,intn){intp=(n-1)/x;// Largest multiple of x below nreturn(longlong)x*p*(p+1)/2;// Sum of multiples formula}intmain(){intt;scanf("%d",&t);for(inta0=0;a0<t;a0++){intn;scanf("%d",&n);// Sum of multiples of 3 and 5, minus multiples of 15 (to avoid double counting)longlongsum=sum_of_multiples(3,n)+sum_of_multiples(5,n)-sum_of_multiples(15,n);printf("%lld\n",sum);}return0;}
Cookie support is required to access HackerRank
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 →