Site icon R-bloggers

Euler Coding Challenge: Build Maths’ Most Beautiful Formula in R

[This article was first published on R-Bloggers – Learning Machines, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


In this post, we will first give some intuition for and then demonstrate what is often called the most beautiful formula in mathematics, Euler’s identity, in R – first numerically with base R and then also symbolically, so read on!

Euler’s identity (also known as Euler’s equation) is the equality:

   

where

It is often credited as the most beautiful formula in mathematics, nerds sport it on T-shirts and even get tattoos with it.

It combines three of the basic arithmetic operations: addition, multiplication, and exponentiation and links five fundamental mathematical constants:

We won’t go into the mathematical details here (when you google it you can find literally thousands of posts, articles, videos, and even whole books on it) but just give you some hand-waving intuition: as stated above is the ratio of the circumference of a circle to its diameter, which means that when you have a radius of 1 (= unit circle) you will need to go full circle. This is illustrated in the following animation:

Source: wikimedia

Many of you know the exponential function (thanks to Covid anyway) which is nothing else but taking Euler’s number to some power. Something magical happens when you take imaginary/complex instead of the “normal” real numbers: the exponential function starts to rotate:

Source: wikimedia

As we have seen above a rotation by boils down to a rotation by degrees. So when you start at 0 and put that into the exponential function you get 1 (because ) and when you then do a one-eighty (=) you will end up at -1. To get to the right-hand side of the identity you just have to add 1 to that -1 which equals 0. So Euler’s identity basically means:

When you turn around, you will look in the opposite direction!

Seen this way, it is easy, isn’t it!

Now for the R part. The following is the original task from Rosetta Code (for more solved Rosetta code tasks see the respective Category: Rosetta Code on this blog):

Show in your language that Euler’s identity is true. As much as possible and practical, mimic the Euler’s identity equation.

Most languages are limited to IEEE 754 floating point calculations so will have some error in the calculation.

If that is the case, or there is some other limitation, show that is approximately equal to zero and show the amount of error in the calculation.

If your language is capable of symbolic calculations, show that is exactly equal to zero for bonus kudos points.

First, as always, you should give it a try yourself…

…and now for the solution!

For coding the left-hand side of the identity we have to know the following:

Putting it all together:

exp(1i * pi) + 1
## [1] 0+1.224606e-16i

Besides the small rounding error, this is the whole solution!

Now for the symbolic solution to also get the bonus kudos points.

We will use the fantastic Ryacas package (on CRAN) to finish the job (for an introduction to this package see: Doing Maths Symbolically: R as a Computer Algebra System (CAS)).

library(Ryacas)
##
## Attaching package: 'Ryacas'
## The following object is masked from 'package:stats':
## 
##     integrate
## The following objects are masked from 'package:base':
## 
##     %*%, diag, diag<-, lower.tri, upper.tri

as_r(yac_str("Exp(I * Pi) + 1"))
## [1] 0

And this finishes the task. It is also the solution I contributed to Rosetta code.

Isn’t maths beautiful! And isn’t R beautiful!

To leave a comment for the author, please follow the link and comment on their blog: R-Bloggers – Learning Machines.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.