Skip to content

Instantly share code, notes, and snippets.

@ahmad-elbatanouni
Last active August 29, 2015 13:57
Show Gist options
  • Save ahmad-elbatanouni/9403692 to your computer and use it in GitHub Desktop.
Save ahmad-elbatanouni/9403692 to your computer and use it in GitHub Desktop.
public class t {
public static void main(String[] args) {
int current_val = 0, counter = 0;
for (int i = 1; i < 10000000; i++) {
current_val = i;
while (current_val != 1) {
current_val = calculateSquares(current_val);
if (current_val == 89) {
counter++;
break;
}
}
}
System.out.println(counter);
}
public static int calculateSquares(int num) {
int result = 0;
int current_digit = 0;
while(num > 0) {
current_digit = num % 10;
num = (num - current_digit) / 10;
result += current_digit * current_digit;
}
return result;
}
}
@bashmohandes
Copy link

This can run much faster if you didn't convert from int -> char -> int again, just use division to extract the digits from the number.

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