Skip to content

Instantly share code, notes, and snippets.

@rubywai
Last active May 1, 2022 04:08
Show Gist options
  • Save rubywai/af58d3dc281fa2f47ad3c4cba1a9319d to your computer and use it in GitHub Desktop.
Save rubywai/af58d3dc281fa2f47ad3c4cba1a9319d to your computer and use it in GitHub Desktop.
void main() {
int n = 4;
int besideSq = 2 * beside(n);
int middleSq = middle(n);
int totalSquares = besideSq + middleSq;
print(totalSquares);
}
int beside(int n){ //numbers of squares from each side
int squares = 0;
for(int i=1; i<(2 *(n-1)); i+=2){
squares += i;
}
return squares;
}
int middle(int n){ //number of squares from middle row
return 1 + 2 * (n-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment