Skip to content

Instantly share code, notes, and snippets.

@ed-flanagan
Last active August 29, 2015 14:04
Show Gist options
  • Save ed-flanagan/5c72eca8c16d411dee23 to your computer and use it in GitHub Desktop.
Save ed-flanagan/5c72eca8c16d411dee23 to your computer and use it in GitHub Desktop.
Print the largest perfect square within given range
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
if (argc < 2) {
printf("usage: %s <limit>\n", argv[0]);
return 1;
}
unsigned int i, n, limit = atoi(argv[1]);
for (i = 1, n = i * i; n <= limit; i++)
n += (2 * i) + 1;
n -= (2 * --i) + 1;
printf("%d: %d\n", i, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment