You are viewing a single comment's thread. Return to all comments →
JAVA 8: Used sum of natural formula for calculation
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i = 0; i < n; i++) { long a = sc.nextLong(); long x= (a-1)/3; long y= (a-1)/5; long z= (a-1)/15; long sum= (3*((x*(x+1))/2)) + (5*((y*(y+1))/2)) - (15*((z*(z+1))/2)); System.out.println(sum); } sc.close(); } }
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 →
JAVA 8: Used sum of natural formula for calculation