Euler Problem 33 takes us back to the world of fractions from our primary school days. Many kids hate and struggle learning about fractions but once you master them, a new world of numbers opens up. Unfortunately, the proliferation of digital calculators has negated the use of fractions in favour of decimal expressions. Fractions are an aesthetic way to express numbers, without having to resort to ugly random sequences of decimals. This is why I prefer to use 22/7 as an approximation of Pi over the ugly infinite series of decimals.

This Numberphile video below explains fractions and Farey sequences. A Farey sequence contains all fractions between 0 and 1 with a maximum denominator. More formally, a Farey sequence of order *n* is the sequence of completely reduced fractions between 0 and 1 which, when in lowest terms, have denominators less than or equal to

These sequences can be visualised in fractal-esque Ford Circles, but before we get to this, first solve Euler problem 33.

## Euler Problem 33 Definition

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 nontrivial 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.

## Proposed Solution in R

To solve this problem, we create a pseudo-Farey sequence by generating all different fractions with two decimals in the numerator and denominator. The loop generates all combinations of demoninators and numerators, excluding the trivial ones (multiples of 10 and 11). This solution converts the numbers to strings, strips any common duplicates, and tests the condition. The code concatenates vectors, which is not good practice. However, the loop is so short it does not matter much.

You can view the code below or download it from my GitHub page.

num## Farey Sequences and Ford Circles

Next step is to generalise Euler problem 33 and write a function to generate Farey Sequences and visualise them using Ford Circles.

The

`farey`

function generates a data table with the numerators (p) and denominators (q) of a Farey sequence. The function builds a list of all possible fractions for the solution space, excluding those with one as a Greatest Common Dominator, as defined by the`gcc`

function.fareyStandard ggplot2 cannot draw circles where the radius of the circles is related to the coordinate system. I tried to use the ggforce package to plot circles in ggplot2, but for some reason, I was not able to install this package on Ubuntu. As an alternative, I used a circle function sourced from StackOverflow. This function is called in a for-loop to build the circles on top of the empty canvas.

library(tidyverse) lm_palette % mutate(x = p / q, y = 1 / (2* q^2), r = y, c = lm_palette[(q - 1)%%6 + 1]) g_circleThe post Euler Problem 33: Digit Cancelling Fractions and Ford Circles appeared first on The Lucid Manager.

Related

R-bloggers.com offersdaily e-mail updatesabout R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Source link https://www.r-bloggers.com/euler-problem-33-digit-cancelling-fractions-and-ford-circles/