Skip to content

Instantly share code, notes, and snippets.

@mahmoufadel
Created March 6, 2014 22:33
Show Gist options
  • Save mahmoufadel/9401229 to your computer and use it in GitHub Desktop.
Save mahmoufadel/9401229 to your computer and use it in GitHub Desktop.
GetSquareDigitChains
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;
}
@bashmohandes
Copy link

A little Dynamic programming will get you better perf, also getting the digits using division instead of string comparison will help.

@amahfouz
Copy link

amahfouz commented Mar 6, 2014

Ditto to "bashmohandes" comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment