<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/the-ulam-spiral-euler-problem-28/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/euler-problem-28/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Euler Problem 28 takes us to the world of the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Ulam_spiral" target="_blank">Ulam Spiral</a>. This is a spiral that contains sequential positive integers in a square spiral, marking the prime numbers. Stanislaw Ulam discovered that a lot of primes are located along the diagonals. These diagonals can be described as polynomials. The Ulam Spiral is thus a way of generating quadratic primes (<a rel="nofollow" href="http:" target="_blank">Euler Problem 27</a>).</p>
<div class="wp-caption"><img alt="Euler Problem 28: The Ulam Spiral" src="https://i1.wp.com/upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Ulam_spiral_howto_primes_only.svg/507px-Ulam_spiral_howto_primes_only.svg.png?resize=300%2C285&ssl=1" data-recalc-dims="1">
<p class="wp-caption-text">Ulam Spiral (WikiMedia).</p>
</div>
<h2>Euler Problem 28 Definition</h2>
<p>Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:</p>
<p><span>21</span> 22 23 24 <span>25</span><br>
20 <span>07</span> 08 <span>09</span> 10<br>
19 06 <span>01</span> 02 11<br>
18 <span>05</span> 04 <span>03</span> 12<br>
<span>17</span> 16 15 14 <span>13</span></p>
<p>It can be verified that the sum of the numbers on the diagonals is 101. What is the <a rel="nofollow" href="https://projecteuler.net/problem=28" target="_blank">sum of the numbers on the diagonals in </a>a 1001 by 1001 spiral formed in the same way?</p>
<h2>Proposed Solution</h2>
<p>To solve this problem we do not need to create a matrix. This code calculates the values of the corners of a matrix with size <img alt="n" title="n" src="http://s0.wp.com/latex.php?latex=n&bg=ffffff&fg=000&s=0" class="latex">. The lowest number in the matrix with size <img alt="n" title="n" src="http://s0.wp.com/latex.php?latex=n&bg=ffffff&fg=000&s=0" class="latex"> is <img alt="n(n-3)+4" title="n(n-3)+4" src="http://s0.wp.com/latex.php?latex=n%28n-3%29%2B4&bg=ffffff&fg=000&s=0" class="latex">. The numbers increase by <img alt="n-1" title="n-1" src="http://s0.wp.com/latex.php?latex=n-1&bg=ffffff&fg=000&s=0" class="latex">.</p>
<p>The code steps through all matrices from size 3 to 1001. The solution uses only the uneven sized matrices because these have a centre. The answer to the problem is the sum of all numbers.</p>
<pre>
size &lt;- 1001 # Size of matrix
answer &lt;- 1 # Starting number
# Define corners of subsequent matrices
for (n in seq(from = 3, to = size, by = 2)) {
    corners &lt;- seq(from = n * (n - 3) + 3, by = n - 1, length.out = 4)
    answer &lt;- answer + sum(corners)
}
print(answer)
</pre>
<h2>Plotting the Ulam Spiral</h2>
<p>We can go beyond Euler Problem 28 and play with the mathematics. This code snippet plots all the prime numbers in the Ulam Spiral. Watch the video for an explanation of the patterns that appear along the diagonals.</p>
<div class="wp-caption"><a rel="nofollow" href="http://r.prevos.net/?attachment_id=547" target="_blank"><img data-orig-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/06/ulamspiral.png?w=450" data-orig-size="1646,756" data-medium-file="http://r.prevos.net/wp-content/uploads/sites/11/2017/06/ulamspiral-300x138.png" data-attachment-id="547" data-image-meta="{" data-image-description="" src="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/06/ulamspiral.png?w=450" data-permalink="http://r.prevos.net/euler-problem-28/ulamspiral/" data-large-file="http://r.prevos.net/wp-content/uploads/sites/11/2017/06/ulamspiral-1024x470.png" alt="Ulam Spiral prime numbers" data-image-title="Ulam Spiral prime numbers" sizes="(max-width: 1646px) 100vw, 1646px" data-comments-opened="1" class="wp-image-547" data-recalc-dims="1"></a>
<p class="wp-caption-text">Ulam Spiral prime numbers.</p>
</div>
<p><iframe allowfullscreen="true" src="https://www.youtube.com/embed/iFuR97YcSLM?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent" width="450" type="text/html"></iframe></p>
<p>The code creates a matrix of the required size and fills it with the Ulam Spiral. The code then identifies all primes using the <em>is.prime</em> function from <a rel="nofollow" href="http://r.prevos.net/euler-problem-7/" target="_blank">Euler Problem 7</a>. A heat map visualises the results.</p>
<pre>
# Ulam Spiral
size &lt;- 201 # Size of matrix
ulam &lt;- matrix(ncol = size, nrow = size)
mid &lt;- floor(size / 2 + 1)
ulam[mid, mid] &lt;- 1
for (n in seq(from = 3, to = size, by = 2)) {
    numbers &lt;- (n * (n - 4) + 5) : ((n + 2) * ((n + 2) - 4) + 4)
    c &lt;- mid - floor(n / 2)
    l &lt;- length(numbers)
    ulam &lt;- numbers[(l - n + 1):l]
    ulam &lt;- numbers[(n - 1):(n - 2 + n)]
    ulam[(c + 1):(c + n - 2), c] &lt;- numbers[(l - n):(l - 2 * n + 3)]
    ulam[(c + 1):(c + n - 2), c + n - 1] &lt;- numbers[1:(n - 2)]
}
ulam.primes &lt;- apply(ulam, c(1, 2), is.prime)

# Visualise
library(ggplot2)
library(reshape2)
ulam.primes &lt;- melt(ulam.primes)
ggplot(ulam.primes, aes(x=Var1, y=Var2, fill=value)) + geom_tile() + 
    scale_fill_manual(values = c(&quot;white&quot;, &quot;black&quot;)) + 
    guides(fill=FALSE) +
    theme(panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          panel.background = element_blank()) + 
    theme(axis.title.x=element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank(),
          axis.title.y=element_blank(),
          axis.text.y=element_blank(),
          axis.ticks.y=element_blank()
    )
</pre>
<p>The post <a rel="nofollow" href="http://r.prevos.net/euler-problem-28/" target="_blank">The Ulam Spiral (Euler Problem 28)</a> appeared first on <a rel="nofollow" href="http://r.prevos.net/" target="_blank">The Devil is in the Data</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r.prevos.net/euler-problem-28/"> The Devil is in the Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/NcvtacfOmOM" height="1"></a></div>