Updated for performance

This commit is contained in:
2020-06-15 12:30:02 -04:00
parent 0b4cdebfe9
commit efe2d5a07a

View File

@@ -1,11 +1,11 @@
//ProjectEuler/Java/Problem6.java //ProjectEuler/Java/Problem6.java
//Matthew Ellison //Matthew Ellison
// Created: 03-01-19 // Created: 03-01-19
//Modified: 03-28-19 //Modified: 06-15-20
//Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. //Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/* /*
Copyright (C) 2019 Matthew Ellison Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -25,9 +25,9 @@ package mattrixwv.ProjectEuler.Problems;
public class Problem6 extends Problem{ public class Problem6 extends Problem{
//The first number that needs to be counted //The first number that needs to be counted
private static final Integer START_NUM = 1; private static final int START_NUM = 1;
//The last number that needs to be counted //The last number that needs to be counted
private static final Integer END_NUM = 100; private static final int END_NUM = 100;
public Problem6(){ public Problem6(){
super("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum."); super("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.");
@@ -37,11 +37,11 @@ public class Problem6 extends Problem{
timer.start(); timer.start();
//Setup the variables //Setup the variables
Long sumOfSquares = 0L; //Holds the sum of the squares of all the numbers long sumOfSquares = 0L; //Holds the sum of the squares of all the numbers
Long squareOfSum = 0L; //Holds the square of the sum of all the numbers long squareOfSum = 0L; //Holds the square of the sum of all the numbers
//Run through all numbers and add them to the appropriate sums //Run through all numbers and add them to the appropriate sums
for(Integer currentNum = START_NUM;currentNum <= END_NUM;++currentNum){ for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){
sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable
squareOfSum += currentNum; //Add the number to the correct variable to squaring later squareOfSum += currentNum; //Add the number to the correct variable to squaring later
} }
@@ -58,5 +58,5 @@ public class Problem6 extends Problem{
/* Results: /* Results:
The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150 The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150
It took 51.999 microseconds to solve this problem. It took 3.100 microseconds to solve this problem.
*/ */