[Project Euler] - Problem 58

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed.

37 36 35 34 33 32 31
38 17 16 15 14 13 30
39  18  5 4  3 12  29
40  19  6   1  2  11  28
41 20   7 8   9  10  27
42 21 22 23 24 25 26
43 44 45 46 47 48 49

It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 = 62%.

If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?

?View Code RSPLUS
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  side.length = 1 x <- 1 iter <- 1 ratio = 0.6 isp.n <- 0 N <- 0 while(ratio > 0.1) { last.x <- x[length(x)] side.length <- side.length + 2 x <- rep(last.x,4 ) + c(2,4,6,8)* iter iter <- iter + 1 isp <- gmp::isprime(x) isp.n <- isp.n + sum(as.logical(isp)) N <- N + 4 ratio <- isp.n/N print(side.length) }

m4s0n501

Related Posts

1. It's cool that you're participating to Project Euler but please stop posting solutions!
It completely spoils the fun for other people and devalues other players' score, since there's a suspicion that they simply looked up the solution on the internet.

On top of that, those posts appear in R-Bloggers and its subscribers can't avoid them. How would you feel if you were sent the solutions without having had a chance to have a go at a problem?

Hope you understand,
And good luck for the rest of the problems.

You may noticed that many of the solutions were on the internet.
It is very easy to look up the solution if someone wants to.

Maybe there is some other people who read my post and got to know and participate Project Euler.
At this point of view, what I've done can be a good thing.

Just have fun, don't be so serious.

I solved it by using a search engine, does that matter?

Making use of the internet to research a problem is to be encouraged
as there could be hidden treasures of mathematics to be discovered
beneath the surface of many of these problems. However, there is a
fine line between researching ideas and using the answer you found
on another website. If you photocopy a crossword solution then
what have you achieved?