Last active
August 29, 2015 13:57
-
-
Save ahmad-elbatanouni/9403692 to your computer and use it in GitHub Desktop.
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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.