Project Euler #6: Sum square difference

  • + 0 comments

    JAVA 8

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        long n[] = new long[t];
        long count[]= new long[10000];
        long result[] = new long[t];
        double result1=0;
        double result2=0;
    
        for(int i=0; i<t; i++){
            n[i] = in.nextInt();
            for(int j=0; j<n[i]; j++) {
                count[i]+=j+1;
                result2+=Math.pow(j+1,2);
            }
            result1=Math.pow(count[i], 2);
            result[i]=((long)(result1-result2));
            result1=0; result2=0;
        }
    
        for(int i=0; i<t; i++) {
            System.out.println(result[i]);
        }
        in.close();
    }