Updated for performance

This commit is contained in:
2020-06-17 18:42:28 -04:00
parent 465b6165c0
commit 86b10bae89

View File

@@ -1,11 +1,11 @@
//ProjectEuler/Java/Problem26.java //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem26.java
//Matthew Ellison //Matthew Ellison
// Created: 07-28-19 // Created: 07-28-19
//Modified: 07-28-19 //Modified: 06-17-20
//What is the index of the first term in the Fibonacci sequence to contain 1000 digits? //Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
//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
@@ -30,33 +30,33 @@ import java.util.ArrayList;
public class Problem26 extends Problem{ public class Problem26 extends Problem{
//The number of digits needed in the number //The number of digits needed in the number
private static final Integer TOP_NUM = 999; private static final int TOP_NUM = 999;
public Problem26(){ public Problem26(){
super("What is the index of the first term in the Fibonacci sequence to contain 1000 digits?"); super("Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.");
} }
public void solve(){ public void solve(){
//The length of the longest cycle //The length of the longest cycle
Integer longestCycle = 0; int longestCycle = 0;
//The starting denominator of the longest cycle //The starting denominator of the longest cycle
Integer longestNumber = 0; int longestNumber = 0;
//Start the timer //Start the timer
timer.start(); timer.start();
//Start with 1/2 and find out how long the longest cycle is by checking the remainders //Start with 1/2 and find out how long the longest cycle is by checking the remainders
//Loop through every number from 2-999 and use it for the denominator //Loop through every number from 2-999 and use it for the denominator
for(Integer denominator = 2;denominator <= TOP_NUM;++denominator){ for(int denominator = 2;denominator <= TOP_NUM;++denominator){
ArrayList<Integer> denomList = new ArrayList<Integer>(); ArrayList<Integer> denomList = new ArrayList<Integer>();
Boolean endFound = false; //Holds whether we have found an end to the number (either a cycle or a 0 for remainder) boolean endFound = false; //Holds whether we have found an end to the number (either a cycle or a 0 for remainder)
Boolean cycleFound = false; //Holds whether a cycle was detected boolean cycleFound = false; //Holds whether a cycle was detected
Integer numerator = 1; //The numberator that will be divided. Always starts at 1 int numerator = 1; //The numerator that will be divided. Always starts at 1
while(!endFound){ while(!endFound){
//Get the remainder after the division //Get the remainder after the division
Integer remainder = numerator % denominator; int remainder = numerator % denominator;
//Check if the remainder is 0 //Check if the remainder is 0
//If it is set the flag //If it is set the flag
if(remainder.equals(0)){ if(remainder == 0){
endFound = true; endFound = true;
} }
//Check if the remainder is in the list //Check if the remainder is in the list
@@ -85,7 +85,7 @@ public class Problem26 extends Problem{
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
//Save the restuls //Save the results
result = String.format("The longest cycle is %d digits long\nIt started with the number %d\n", longestCycle, longestNumber); result = String.format("The longest cycle is %d digits long\nIt started with the number %d\n", longestCycle, longestNumber);
} }
} }
@@ -93,5 +93,5 @@ public class Problem26 extends Problem{
/* Results: /* Results:
The longest cycle is 982 digits long The longest cycle is 982 digits long
It started with the number 983 It started with the number 983
It took 28.969 milliseconds to solve this problem. It took 10.429 milliseconds to solve this problem.
*/ */