Project Euler #1: Multiples of 3 and 5

  • + 0 comments

    handle long only:

    public class Solution {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int t = in.nextInt();
            for(int a0 = 0; a0 < t; a0++){
                int n = in.nextInt()-1;
                //code below:
    						
                long ans=(3*sum(n/3))+(5*sum(n/5))-(15*sum(n/15));
                System.out.println(ans);
                
                //code above:
                
            }
        }
        
        public static long sum(long num){
            return (num*(num+1))/2;
        }
    }