Tuesday, 3 July 2018

Project Euler #1: Multiples of 3 and 5

import java.io.*;
import java.util.Arrays;
class hackerrank
{   
    public static void main(String args[])throws IOException
    {
        
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        long t=Integer.parseInt(br.readLine());
        for(int q=1;q<=t;q++)
        {
            long n=Integer.parseInt(br.readLine());
            long n3=0,n5=0,n15=0;
            if(n%3!=0)
             n3=(long)Math.floor(n/3);
            else
            n3=n/3-1;
            if(n%5!=0)
             n5=(long)Math.floor(n/5);
            else
            n5=n/5-1;
            if(n%15!=0)
             n15=(long)Math.floor(n/15);
            else
            n15=n/15-1;
            long s3=(n3)*(2*3+(n3-1)*3)/2;
            long s5=(n5)*(2*5+(n5-1)*5)/2;
            long s15=(n15)*(2*15+(n15-1)*15)/2;
            
            System.out.println(s3+s5-s15);
        }
    }
}

No comments:

Post a Comment

Project Euler #1: Multiples of 3 and 5

import java.io.* ; import java.util.Arrays ; class hackerrank { public static void main ( String args []) throws IOExcepti...