Project Euler #1: Multiples of 3 and 5

  • + 0 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();
            
         
    }
    }