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


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:

    \[e^{i \pi}+1=0\]

where

  • e is Euler’s number, the base of natural logarithms
  • i is the imaginary unit, which satisfies i^2 = -1
  • \pi is the ratio of the circumference of a circle to its diameter

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:

  • The number 0
  • The number 1
  • The number \pi = 3.14159+
  • The number e = 2.71828+
  • The number i the imaginary unit of the complex numbers

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 \pi 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 2\pi 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 e 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 \pi boils down to a rotation by 180 degrees. So when you start at 0\pi and put that into the exponential function you get 1 (because e^{i\times0\pi}=e^0=1) and when you then do a one-eighty (=1\pi) 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 e^{i \pi}+1 is approximately equal to zero and show the amount of error in the calculation.

If your language is capable of symbolic calculations, show that e^{i \pi}+1 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:

  • e is not a built-in constant. Instead, the exponential function exp() is used (if you want to get Euler’s number just use exp(1))
  • R can handle complex numbers! You can use the complex() function for that, or the Re() and Im() functions for the real and the imaginary parts. In this case it is even easier because we will only need 1i and this is exactly the way we code it in R: 1i!
  • \pi is an built-in constant: pi

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!

3 thoughts on “Euler Coding Challenge: Build Maths’ Most Beautiful Formula in R”

  1. Hi, great post! There is an error though, imaginary unit should satisfy i^2 = -1, not i^2 = 1.

    1. Thank you, fixed it!

      It was strange though: there was a sign that looked like the minus sign in the source code but it wasn’t rendered correctly. I typed it again and now it renders alright. Strange…

Leave a Reply

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

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

This site uses Akismet to reduce spam. Learn how your comment data is processed.