<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/euler-problem-11-largest-product-in-a-grid/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/euler-problem-11/"> 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>
<h2>Euler Problem 11 Definition</h2>
<p>In the 20×20 grid below, four numbers along a diagonal line have been marked in red.</p>
<p><img sizes="(max-width: 584px) 100vw, 584px" data-image-description="" data-orig-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/02/grid20.png?fit=617%2C387" data-orig-size="617,387" data-image-title="grid20" class="wp-image-176" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="176" data-permalink="http://r.prevos.net/euler-problem-11/grid20/" alt="Euler problem 11" data-medium-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/02/grid20.png?fit=300%2C188" data-large-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/02/grid20.png?fit=584%2C366" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/02/grid20.png?resize=584%2C366"></p>
<p>The product of these numbers is 26 × 63 × 78 × 14 = 1,788,696. <a target="_blank" rel="nofollow" href="https://projecteuler.net/problem=11">What is the greatest product of four adjacent numbers in the same direction</a> (up, down, left, right, or diagonally) in the 20 by 20 grid?</p>
<h2>Solution</h2>
<p>The solution applies straightforward vector arithmetic. The product of all verticals is an array of the product of rows 1 to 4, rows 2 to 5 and so on. The code uses a similar logic for the horizontals and the diagonals.</p>
<pre>
#Read and convert data
square &lt;- readLines(&quot;Euler/p011_matrix.txt&quot;)
square &lt;- as.numeric(unlist(lapply(square, function(x){strsplit(x, &quot; &quot;)})))
square &lt;- matrix(square, ncol=20)

# Define products
prod.vert &lt;- square[1:17, ] * square[2:18, ] * square[3:19, ] * square[4:20, ]
prod.hori &lt;- square[,1:17] * square[,2:18] * square[,3:19] * square[,4:20]
prod.dia1 &lt;- square[1:17, 1:17] * square[2:18, 2:18] * square[3:19, 3:19] * square[4:20, 4:20]
prod.dia2 &lt;- square[4:20, 1:17] * square[3:19, 2:18] * square[2:18, 3:19] * square[1:17, 4:20]

answer &lt;- max(prod.vert, prod.hori, prod.dia1, prod.dia2)
</pre>
<p> </p>
<p> </p>
<p> </p>
<p>The post <a target="_blank" rel="nofollow" href="http://r.prevos.net/euler-problem-11/">Euler Problem 11: Largest Product in a Grid</a> appeared first on <a target="_blank" rel="nofollow" href="http://r.prevos.net/">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-11/"> 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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/F3qwOLF4ohY"></a></div>