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.
//here is the exact JAVA solution by using formula for the sum of an arithmetic progression//
public static long sum(int n){
n--;
long sum=0;
long n3 = n/3;
long n5 = n/5;
long n15 = n/15;
sum=(3*n3*(n3+1))/2 + (5*n5*(n5+1))/2 - (15 * n15*(n15+1))/2;
return sum;
}
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 →
//here is the exact JAVA solution by using formula for the sum of an arithmetic progression//