//ProjectEuler/Java/Problem12.java //Matthew Ellison // Created: 03-04-19 //Modified: 03-28-19 //What is the value of the first triangle number to have over five hundred divisors? //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 . */ import mattrixwv.Stopwatch; import mattrixwv.Algorithms; import java.util.ArrayList; public class Problem12{ private static final Long GOAL_DIVISORS = 500L; //The minimum number of divisors that you want public static void main(String[] argv){ Stopwatch timer = new Stopwatch(); //Allows timing of the algorithm //Setup the other variables Boolean foundNumber = false; //TO flag whether the number has been found Long sum = 1L; Long counter = 2L; //The next number to be added to the sum to make a triangular number ArrayList divisors = new ArrayList(); //Start the timer timer.start(); //Loop until you find the appropriate number while((!foundNumber) && (sum > 0)){ divisors = Algorithms.getDivisors(sum); //If the number of divisors is correct set the flag if(divisors.size() > GOAL_DIVISORS.intValue()){ foundNumber = true; } //Otherwise add to the sum and increase the next number else{ sum += counter; ++counter; } } //Stop the timer timer.stop(); //Print the results System.out.printf("The triangulare number %d is the sum of all numbers >= %d and has %d divisors\n", sum, counter - 1, divisors.size()); System.out.println("It took " + timer.getStr() + " to run this algorithms"); } } /* Results: The triangulare number 76576500 is the sum of all numbers >= 12375 and has 576 divisors It took 758.987 milliseconds to run this algorithms */