Snippet

project_euler/33.rb
Code:
# Project Euler Problem 33: Digit Cancelling Fractions

# https://projecteuler.net/problem=33

# The fraction 49/98 is a curious fraction, as an inexperienced mathematician
# in attempting to simplify it may incorrectly believe that 49/98 = 4/8,
# which is correct, is obtained by cancelling the 9s.
# We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
# There are exactly four non-trivial examples of this type of fraction, less
# than one in value, and containing two digits in the numerator and denominator.
# If the product of these four fractions is given in its lowest common terms,
# find the value of the denominator.

require_relative './helpers.rb'

def take_one(arr, element)
    took = false
    arr.map { |e| element == e ? (took ? e : (took = true; nil)) : e }.compact
end

def find
    nums, dens = [], []
    (10..99).each do |den|
        (10..99).each do |num|
            next if (den == num) || (num > den)
            common = (digits(num) & digits(den))[0]
            next if common == nil || common == 0
            n, d = [num, den].map { |k| take_one(digits(k), common)[0] }
            if ((n / d.to_f) == (num / den.to_f))
                puts "#{num} / #{den} = #{n} / #{d}"
                nums << n
                dens << d
            end
        end
    end
    return nums, dens
end

def solve
    n, d = find().map { |arr| arr.inject(:*) }
    hcf = intersection(prime_factors(n), prime_factors(d)).inject(:*)
    return d / hcf
end

puts solve()
Output:
16 / 64 = 1 / 4
26 / 65 = 2 / 5
19 / 95 = 1 / 5
49 / 98 = 4 / 8
100