Created
March 6, 2014 22:33
-
-
Save mahmoufadel/9401229 to your computer and use it in GitHub Desktop.
GetSquareDigitChains
This file contains hidden or 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
int resualt=GetSquareDigitChains(2,10000000); | |
static int GetSquareDigitChains(int strt, int end) | |
{ | |
int i = 0; | |
for (; strt < end; strt++) | |
{ | |
int nextstrt = 0; | |
int strtTemp = strt; | |
while (nextstrt != 89) | |
{ | |
strtTemp.ToString().ToCharArray().ToList().ForEach(x => | |
{ | |
nextstrt = nextstrt + int.Parse(x.ToString()) * int.Parse(x.ToString()); | |
}); | |
if (nextstrt == 1) { break; } | |
if (nextstrt == 89){ i++; break;} | |
else { strtTemp = nextstrt; nextstrt = 0; } | |
} | |
} | |
return i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A little Dynamic programming will get you better perf, also getting the digits using division instead of string comparison will help.