Last active
August 29, 2015 14:04
-
-
Save ed-flanagan/5c72eca8c16d411dee23 to your computer and use it in GitHub Desktop.
Print the largest perfect square within given range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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