package org.asaph.happynumber;

import java.util.HashSet;
import java.util.Set;

public class HappyNumber {
	public static int nextNumber(int number) {
		String numberString = Integer.toString(number);
		int sum=0;
		for (int i=0, length=numberString.length(); i<length; i++) {
			char ch = numberString.charAt(i);
			int digit = Integer.parseInt("" + ch);
			int square = digit * digit;
			sum += square;
		}
		return sum;
	}

	public static boolean isHappy(int number) {
		return isHappy(number, new HashSet<Integer>());
	}

	public static boolean isHappy(int number, Set<Integer> numbersSoFar) {
		int next = nextNumber(number);
		if (next == 1) {
			return true;
		}
		if (numbersSoFar.contains(next)) {
			return false;
		}
		numbersSoFar.add(next);
		return isHappy(next, numbersSoFar);
	}

	public static void main(String[] args) {
		for (int i=1; i<=1000; i++) {
			if (isHappy(i)) {
				System.out.println(i);
			}
		}
	}
}

@@ -149,58 +149,8 @@
 ) %7B%0A
-%09%09String numberString = Integer.toString(number);%0A
 %09%09in
@@ -158,168 +158,74 @@
  sum
-=
+ = 
 0;%0A%09%09
-for (int i=0, length=numberString.length(); i%3Clength; i++) %7B%0A%09%09%09char ch = numberString.charAt(i);%0A%09%09%09int digit = Integer.parseInt(%22%22 + ch);%0A%09%09%09int square 
+while (number != 0) %7B%0A%09%09%09int digit = number %25 10;%0A%09%09%09sum +
 = di
@@ -244,21 +244,38 @@
 %0A%09%09%09
-sum += square
+number = (number - digit) / 10
 ;%0A%09%09

Break number apart into digits using math instead of string operations.