This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Problem taken from: https://projecteuler.net/problem=11 | |
| # In the 2020 grid below, four numbers along a diagonal line have been marked in red. | |
| # The product of these numbers is 26 63 78 14 = 1788696. | |
| # What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 2020 grid? | |
| n = '08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 | |
| 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 | |
| 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 | |
| 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Goal: Create a method that takes an input number and creates a hash of its prime factors and their exponents. | |
| # e.g. prime_factorization(18) => { 2=>1, 3=>2 } #i.e. 2 * 3^2 | |
| # Logic: Take a number and divide by each prime number, and divide the resulting quotient by prime numbers, etc. to get the total factorization. | |
| # e.g. 100 = 50 * 2. | |
| # 50 = 25 * 2. | |
| # 25 = 5 * 5. | |
| # Prime factorization of 100 = 2*2*5*5 or 2^2*5^2. | |
| factors = Hash.new(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: | |
| # 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... | |
| # Let us list the factors of the first seven triangle numbers: | |
| # 1: 1 | |
| # 3: 1,3 | |
| # 6: 1,2,3,6 | |
| # 10: 1,2,5,10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # See my earlier Gist, Project Euler #12 - attempt 1, for context. | |
| puts Time.now | |
| def is_prime?(n) | |
| return true if n == 2 | |
| for i in 2..((n**0.5).ceil) | |
| # puts "Checking #{i}... " | |
| if n % i == 0 | |
| return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def tri_num(n) | |
| (n)*(n+1)/2 #formula for the nth triangle number. http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/runsums/triNbProof.html | |
| end | |
| def num_factors(n) #brute force method.. | |
| factors = 0 | |
| for i in 1..n | |
| factors += 1 if n % i == 0 | |
| end | |
| factors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @result = [] | |
| def flatten(array) | |
| array.each do |element| | |
| if element.respond_to?(:each) | |
| flatten(element) | |
| else | |
| @result.push(element) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Pseudo-code ## | |
| # Initialize constant (lookup hash) with digits as keys, | |
| # words as values. e.g. { 1 => one, 2 => two }. This is for 1-9 | |
| # Initialize constant (lookup hash) with numbers as keys, | |
| # words as values. e.g. { 10 => ten, 11 => eleven }. This is for 10-19. | |
| # Initialize constant (lookup hash) with 10's place digit as keys, | |
| # prefixes as values. e.g. { 2 => twenty, 3 => thirty }. This is for 20-99. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NUMBER_WORDS = | |
| { | |
| 90 => 'ninety' , | |
| 80 => 'eighty' , | |
| 70 => 'seventy' , | |
| 60 => 'sixty' , | |
| 50 => 'fifty' , | |
| 40 => 'forty' , | |
| 30 => 'thirty' , | |
| 20 => 'twenty' , |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Part 1 tests | |
| car = Car.new({:color => 'red'}) | |
| p car.drive == :driving # => true | |
| p car.brake == :stopped # => true | |
| p car.instance_variables.sort == [:@color, :@wheels, :@status].sort | |
| p (car.needs_gas? == true || car.needs_gas? == false) #this sometimes yields false, strangely.. | |
| p [true, false].include?(car.needs_gas?) #seems to work | |
| car.needs_gas? #seems to always return true or false, so i'm not sure what's going on above.. | |
| # Part 1 tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html> | |
| <head> | |
| <link rel="stylesheet" href="http://cdn.jsdelivr.net/normalize/2.1.0/normalize.css"> | |
| <link rel="stylesheet" href="main.css"> | |
| <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800"> | |
| <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Lato:100,900"> | |
| <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/3.0.2/css/font-awesome.min.css"> | |
| </head> |
OlderNewer