Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created December 2, 2011 03:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakedobkin/1421545 to your computer and use it in GitHub Desktop.
Save jakedobkin/1421545 to your computer and use it in GitHub Desktop.
Euler 28
#!/usr/local/bin/node
/* this is the pattern formed by the diagonals
ring 2 -- 2+ ([+1] + 3 + [+2] + 5 + [+2] + 7 + [+2])+ 9
ring 3 -- 10+([+3] + 13 + [+4] + 17 + [+4] + 21 + [+4])+25
where the numbers in brackets increase by 2 each time
how many rings? well ring 1 is a 1x1
and then every ring after adds 2 columns or rows
so by ring 2 you have 3 rows and 3 columns
by ring 3 you have 5 rows and 5 columns
by ring 4 you have 7 rows and 7 columns
so each ring adds 2
so you need 500 rings to get to get 1001 rows by 1001 columns
*/
total = 1;
term = 2;
diagonal_total = 1;
for(i=1;i<=500;i++)
{
start = total + 1;
a = start + (term-1);
b = a + term;
c = b + term;
d = c + term;
// console.log("ring " + i + " is " + a+" "+b+" "+c+" "+d+ " equals "+ (a+b+c+d));
diagonal_total += (a+b+c+d);
total = d;
term +=2;
}
console.log(diagonal_total);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment