mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
93 lines
3.3 KiB
Java
93 lines
3.3 KiB
Java
//ProjectEuler/Java/Problem26.java
|
|
//Matthew Ellison
|
|
// Created: 07-28-19
|
|
//Modified: 07-28-19
|
|
//What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
|
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
|
/*
|
|
Copyright (C) 2019 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
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
|
|
import mattrixwv.Stopwatch;
|
|
import mattrixwv.Algorithms;
|
|
import java.util.ArrayList;
|
|
|
|
public class Problem26{
|
|
private static final Integer TOP_NUM = 999;
|
|
public static void main(String[] args){
|
|
//Setup the variables
|
|
Stopwatch timer = new Stopwatch();
|
|
Integer longestCycle = 0; //The length of the longest cycle
|
|
Integer longestNumber = 0; //The starting denominator of the longest cycle
|
|
|
|
//Start the timer
|
|
timer.start();
|
|
|
|
//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
|
|
for(Integer denominator = 2;denominator <= TOP_NUM;++denominator){
|
|
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 cycleFound = false; //Holds whether a cycle was detected
|
|
Integer numerator = 1; //The numberator that will be divided. Always starts at 1
|
|
while(!endFound){
|
|
//Get the remainder after the division
|
|
Integer remainder = numerator % denominator;
|
|
//Check if the remainder is 0
|
|
//If it is set the flag
|
|
if(remainder.equals(0)){
|
|
endFound = true;
|
|
}
|
|
//Check if the remainder is in the list
|
|
//If it is in the list, set the appropriate flags
|
|
else if(Algorithms.isFound(denomList, remainder)){
|
|
endFound = true;
|
|
cycleFound = true;
|
|
}
|
|
//Else add it to the list
|
|
else{
|
|
denomList.add(remainder);
|
|
}
|
|
//Multiply the remainder by 10 to continue finding the next remainder
|
|
numerator = remainder * 10;
|
|
}
|
|
//If a cycle was found check the size of the list against the largest cycle
|
|
if(cycleFound){
|
|
//If it is larger than the largest, set it as the new largest
|
|
if(denomList.size() > longestCycle){
|
|
longestCycle = denomList.size();
|
|
longestNumber = denominator;
|
|
}
|
|
}
|
|
}
|
|
|
|
//Stop the timer
|
|
timer.stop();
|
|
|
|
//Print the restuls
|
|
System.out.printf("The longest cycle is %d digits long\n", longestCycle);
|
|
System.out.printf("It started with the number %d\n", longestNumber);
|
|
System.out.println("It took " + timer.getStr() + " to run this algorithm");
|
|
}
|
|
}
|
|
|
|
/* Results:
|
|
The longest cycle is 982 digits long
|
|
It started with the number 983
|
|
It took 41.482 milliseconds to run this algorithm
|
|
*/
|