//ProjectEuler/Java/Problem15.java //Matthew Ellison // Created: 03-04-19 //Modified: 03-28-19 //How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses //This program has not been tested fully and has not even been run to completion because of the long time it takes to run /* 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; public class Problem15{ private static final Integer WIDTH = 20; private static final Integer LENGTH = 20; public static void main(String[] argv){ Stopwatch timer = new Stopwatch(); //Used to determine the algorithm's run time //Setup the rest of the variables Long numOfRoutes = 0L; //The number of routes from 0, 0, to 20, 20 Integer currentX = 0; //The current x location on the grid Integer currentY = 0; //The current y location on the grid //Start the timer timer.start(); //We write this as a recursive function //When in a location it always moves right first, then down move(currentX, currentY, numOfRoutes); //Stop the timer timer.stop(); //Print the results System.out.println("The number of routes is " + numOfRoutes.toString()); System.out.println("It took " + timer.getStr() + " to run this algorithm"); } //This function acts as a handler for moving the position on the grid and counting the distance //It moves right first, then down private static void move(Integer currentX, Integer currentY, Long numOfRoutes){ //Check if you are at the end and act accordingly if((currentX == WIDTH) && (currentY == LENGTH)){ ++numOfRoutes; return; } //Move right if possible if(currentX < WIDTH){ move(currentX + 1, currentY, numOfRoutes); } //Move down if possible if(currentY < LENGTH){ move(currentX, currentY + 1, numOfRoutes); } } } /* Results: */