Solved ProjectEuler/015: php, ruby
This commit is contained in:
		
							
								
								
									
										19
									
								
								ProjectEuler/015/desc.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								ProjectEuler/015/desc.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
title: Find the longest sequence using a starting number under one million.	
 | 
			
		||||
url: http://projecteuler.net/problem=15
 | 
			
		||||
 | 
			
		||||
desc: |
 | 
			
		||||
  Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner.
 | 
			
		||||
  How many routes are there through a 2020 grid?
 | 
			
		||||
  
 | 
			
		||||
solution: |
 | 
			
		||||
  (2n)!/n!^2 - Where n is the size of the grid -
 | 
			
		||||
  
 | 
			
		||||
todo: Find a more "programmable way" instead of this cheating mathsolution
 | 
			
		||||
 | 
			
		||||
solutions:
 | 
			
		||||
  solve.php:
 | 
			
		||||
    desc: Using BCMath to cope with large numbers
 | 
			
		||||
    language: php
 | 
			
		||||
  solve.rb:
 | 
			
		||||
    desc: Basic solution
 | 
			
		||||
    language: ruby
 | 
			
		||||
							
								
								
									
										11
									
								
								ProjectEuler/015/solve.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								ProjectEuler/015/solve.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
<?php
 | 
			
		||||
define('GRID_SIZE',20);
 | 
			
		||||
function factorial($num) {
 | 
			
		||||
	if($num == 0) 
 | 
			
		||||
		return 1;
 | 
			
		||||
	else 
 | 
			
		||||
		return bcmul($num,factorial(bcsub($num,1)));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
echo bcdiv(factorial(2* GRID_SIZE),bcpow(factorial( GRID_SIZE),2));
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								ProjectEuler/015/solve.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								ProjectEuler/015/solve.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
GRID_SIZE = 20
 | 
			
		||||
 | 
			
		||||
def factorial(num) 
 | 
			
		||||
	if(num == 0) 
 | 
			
		||||
		return 1
 | 
			
		||||
	else 
 | 
			
		||||
		return num * factorial(num - 1)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
puts factorial(2 * GRID_SIZE) / factorial(GRID_SIZE) ** 2
 | 
			
		||||
		Reference in New Issue
	
	Block a user