mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-07 09:23:57 -05:00
Updated for performance
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
//ProjectEuler/Java/Problem6.java
|
||||
//Matthew Ellison
|
||||
// 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.
|
||||
//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
|
||||
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{
|
||||
//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
|
||||
private static final Integer END_NUM = 100;
|
||||
private static final int END_NUM = 100;
|
||||
|
||||
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.");
|
||||
@@ -37,11 +37,11 @@ public class Problem6 extends Problem{
|
||||
timer.start();
|
||||
|
||||
//Setup the variables
|
||||
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 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
|
||||
|
||||
//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
|
||||
squareOfSum += currentNum; //Add the number to the correct variable to squaring later
|
||||
}
|
||||
@@ -58,5 +58,5 @@ public class Problem6 extends Problem{
|
||||
|
||||
/* Results:
|
||||
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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user