mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Adjusted to improve performance
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEuler/Java/Problem1.java
|
//ProjectEuler/Java/Problem1.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 03-28-19
|
//Modified: 06-15-20
|
||||||
//What is the sum of all the multiples of 3 or 5 that are less than 1000
|
//What is the sum of all the multiples of 3 or 5 that are less than 1000
|
||||||
//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,13 +25,13 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
public class Problem1 extends Problem{
|
public class Problem1 extends Problem{
|
||||||
//The largest number to be checked
|
//The largest number to be checked
|
||||||
private static final Integer TOP_NUM = 999;
|
private static final int TOP_NUM = 999;
|
||||||
|
|
||||||
public Problem1(){
|
public Problem1(){
|
||||||
super("What is the sum of all the multiples of 3 or 5 that are less than 1000");
|
super("What is the sum of all the multiples of 3 or 5 that are less than 1000");
|
||||||
}
|
}
|
||||||
public void solve(){
|
public void solve(){
|
||||||
Integer sum = 0; //Holds the sum of all the correct elements
|
int sum = 0; //Holds the sum of all the correct elements
|
||||||
//Check every number < 1000 to see if it is a multiple of 3 or 5. If it is add it to the running sum
|
//Check every number < 1000 to see if it is a multiple of 3 or 5. If it is add it to the running sum
|
||||||
timer.start();
|
timer.start();
|
||||||
for(int cnt = 1;cnt <= TOP_NUM;++cnt){
|
for(int cnt = 1;cnt <= TOP_NUM;++cnt){
|
||||||
@@ -44,11 +44,11 @@ public class Problem1 extends Problem{
|
|||||||
}
|
}
|
||||||
timer.stop();
|
timer.stop();
|
||||||
//Save the results
|
//Save the results
|
||||||
result = "The sum of all numbers < " + TOP_NUM.toString() + 1 + " is " + sum.toString();
|
result = "The sum of all numbers < " + (TOP_NUM + 1) + " is " + sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The sum of all numbers < 1000 is 233168
|
The sum of all numbers < 1000 is 233168
|
||||||
It took 140.000 microseconds to solve this problem.
|
It took 22.000 microseconds to solve this problem.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEuler/Java/Problem2.java
|
//ProjectEuler/Java/Problem2.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 03-28-19
|
//Modified: 06-15-20
|
||||||
//The sum of the even Fibonacci numbers less than 4,000,000
|
//The sum of the even Fibonacci numbers less than 4,000,000
|
||||||
//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
|
||||||
@@ -39,9 +39,9 @@ public class Problem2 extends Problem{
|
|||||||
timer.start();
|
timer.start();
|
||||||
//Get a list of all fibonacci numbers < 4,000,000
|
//Get a list of all fibonacci numbers < 4,000,000
|
||||||
ArrayList<Integer> fibNums = Algorithms.getAllFib(TOP_NUM);
|
ArrayList<Integer> fibNums = Algorithms.getAllFib(TOP_NUM);
|
||||||
Integer sum = 0;
|
int sum = 0;
|
||||||
//Step through every element in the list checking if it is even
|
//Step through every element in the list checking if it is even
|
||||||
for(Integer num : fibNums){
|
for(int num : fibNums){
|
||||||
//If the number is even add it to the running tally
|
//If the number is even add it to the running tally
|
||||||
if((num % 2) == 0){
|
if((num % 2) == 0){
|
||||||
sum += num;
|
sum += num;
|
||||||
@@ -57,5 +57,5 @@ public class Problem2 extends Problem{
|
|||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The sum of all even fibonacci numbers <= 3999999 is 4613732
|
The sum of all even fibonacci numbers <= 3999999 is 4613732
|
||||||
It took 551.500 microseconds to solve this problem.
|
It took 36.299 microseconds to solve this problem.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEuler/Java/Problem3.java
|
//ProjectEuler/Java/Problem3.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 03-28-19
|
//Modified: 06-15-20
|
||||||
//The largest prime factor of 600851475143
|
//The largest prime factor of 600851475143
|
||||||
//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
|
||||||
@@ -54,5 +54,5 @@ public class Problem3 extends Problem{
|
|||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The largest factor of the number 600851475143 is 6857
|
The largest factor of the number 600851475143 is 6857
|
||||||
It took 337.600 milliseconds to solve this problem.
|
It took 242.138 milliseconds to solve this problem.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEuler/Java/Problem4.java
|
//ProjectEuler/Java/Problem4.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 03-28-19
|
//Modified: 06-15-20
|
||||||
//Find the largest palindrome made from the product of two 3-digit numbers
|
//Find the largest palindrome made from the product of two 3-digit numbers
|
||||||
//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
|
||||||
@@ -29,9 +29,9 @@ import java.util.Collections;
|
|||||||
|
|
||||||
public class Problem4 extends Problem{
|
public class Problem4 extends Problem{
|
||||||
//The first number to be multiplied
|
//The first number to be multiplied
|
||||||
private static final Integer START_NUM = 100;
|
private static final int START_NUM = 100;
|
||||||
//The last number to be multiplied
|
//The last number to be multiplied
|
||||||
private static final Integer END_NUM = 999;
|
private static final int END_NUM = 999;
|
||||||
|
|
||||||
public Problem4(){
|
public Problem4(){
|
||||||
super("Find the largest palindrome made from the product of two 3-digit numbers");
|
super("Find the largest palindrome made from the product of two 3-digit numbers");
|
||||||
@@ -43,13 +43,13 @@ public class Problem4 extends Problem{
|
|||||||
ArrayList<Integer> palindromes = new ArrayList<Integer>();
|
ArrayList<Integer> palindromes = new ArrayList<Integer>();
|
||||||
|
|
||||||
//Start at the first 3-digit number and check every one up to the last 3-digit number
|
//Start at the first 3-digit number and check every one up to the last 3-digit number
|
||||||
for(Integer firstNum = START_NUM;firstNum <= END_NUM;++firstNum){
|
for(int firstNum = START_NUM;firstNum <= END_NUM;++firstNum){
|
||||||
//You can start at the location of the first number because everything before that has already been tested. (100 * 101 == 101 * 100)
|
//You can start at the location of the first number because everything before that has already been tested. (100 * 101 == 101 * 100)
|
||||||
for(Integer secondNum = firstNum;secondNum < END_NUM;++secondNum){
|
for(int secondNum = firstNum;secondNum < END_NUM;++secondNum){
|
||||||
//Get the product
|
//Get the product
|
||||||
Integer product = firstNum * secondNum;
|
int product = firstNum * secondNum;
|
||||||
//Change the number into a string
|
//Change the number into a string
|
||||||
String productString = product.toString();
|
String productString = Integer.toString(product);
|
||||||
//Reverse the string
|
//Reverse the string
|
||||||
String reverseString = new StringBuilder(productString).reverse().toString();
|
String reverseString = new StringBuilder(productString).reverse().toString();
|
||||||
|
|
||||||
@@ -74,5 +74,5 @@ public class Problem4 extends Problem{
|
|||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The largest palindrome is 906609
|
The largest palindrome is 906609
|
||||||
It took 47.490 milliseconds to solve this problem.
|
It took 14.919 milliseconds to solve this problem.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEuler/Java/Problem5.java
|
//ProjectEuler/Java/Problem5.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 03-28-19
|
//Modified: 06-15-20
|
||||||
//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||||
//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
|
||||||
@@ -32,13 +32,13 @@ public class Problem5 extends Problem{
|
|||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Start at 20 because it must at least be divisible by 20. Increment by 2 because it must be an even number to be divisible by 2
|
//Start at 20 because it must at least be divisible by 20. Increment by 2 because it must be an even number to be divisible by 2
|
||||||
Boolean numFound = false;
|
boolean numFound = false;
|
||||||
Integer currentNum = 20;
|
int currentNum = 20;
|
||||||
while((currentNum > 0) && (!numFound)){
|
while((currentNum > 0) && (!numFound)){
|
||||||
//Start by assuming you found the number (because we throw a flag if we didn't find it)
|
//Start by assuming you found the number (because we throw a flag if we didn't find it)
|
||||||
numFound = true;
|
numFound = true;
|
||||||
//Step through every number from 1-20 seeing if the current number is divisible by it
|
//Step through every number from 1-20 seeing if the current number is divisible by it
|
||||||
for(Integer divisor = 1;divisor <= 20;++divisor){
|
for(int divisor = 1;divisor <= 20;++divisor){
|
||||||
//If it is not divisible then throw a flag and start looking at the next number
|
//If it is not divisible then throw a flag and start looking at the next number
|
||||||
if((currentNum % divisor) != 0){
|
if((currentNum % divisor) != 0){
|
||||||
numFound = false;
|
numFound = false;
|
||||||
@@ -55,11 +55,11 @@ public class Problem5 extends Problem{
|
|||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The smallest positive number evenly divisibly by all number 1-20 is " + currentNum.toString());
|
result = String.format("The smallest positive number evenly divisibly by all number 1-20 is " + currentNum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The smallest positive number evenly divisibly by all number 1-20 is 232792560
|
The smallest positive number evenly divisibly by all number 1-20 is 232792560
|
||||||
It took 393.616 milliseconds to solve this problem.
|
It took 156.873 milliseconds to solve this problem.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user