Project Euler – Problem 33 Solution

Yan Cui

I help clients go faster for less using serverless technologies.

Problem

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.

Solution

// define function to check if two numbers has the cancelling behaviour
let isCancelling a b =
    let aStr, bStr = a.ToString(), b.ToString()
    if aStr.[0] = bStr.[0] then double(aStr.[1].ToString())/double(bStr.[1].ToString()) = double(a)/double(b)
    else if aStr.[0] = bStr.[1] then double(aStr.[1].ToString())/double(bStr.[0].ToString()) = double(a)/double(b)
    else if aStr.[1] = bStr.[0] then double(aStr.[0].ToString())/double(bStr.[1].ToString()) = double(a)/double(b)
    else if aStr.[1] = bStr.[1] then double(aStr.[0].ToString())/double(bStr.[0].ToString()) = double(a)/double(b)
    else false

// define function to find the greatest common dividor
let gcd a b = [2..min a b] |> List.rev |> Seq.filter (fun x -> a % x = 0 && b % x = 0) |> Seq.head

// define function that returns numbers >= n which are not multiples of 10
// and have two distinct digits (i.e. 11, 22 only have one distinct digits)
let numbers n = [n..99] |> List.filter (fun x -> x % 10 <> 0 && (x % 10 <> x/10))

let answer =
    // first work out the product of all 4 fractions, in fractions form (num, denom)
    let fraction =
        numbers 10
        |> List.collect (fun x -> numbers (x+1) |> List.map (fun y -> (x, y)))
        |> List.filter (fun (x, y) -> isCancelling x y)
        |> List.reduce (fun (num, denom) (x, y) -> (num*x, denom*y))
    // then define the denominator by the greatest common divisor
    (snd fraction) / gcd (fst fraction) (snd fraction)

 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


Leave a Comment

Your email address will not be published. Required fields are marked *