Skip to content

Instantly share code, notes, and snippets.

@brunoborges
Last active August 29, 2015 14:20
Show Gist options
  • Save brunoborges/3466169ab70505f28851 to your computer and use it in GitHub Desktop.
Save brunoborges/3466169ab70505f28851 to your computer and use it in GitHub Desktop.
import java.util.HashSet;
import java.util.Set;
import java.util.stream.IntStream;
public class HappyNumber {
public static void main(String[] args) {
IntStream.range(0, args.length == 0 ? 1001 : Integer.parseInt(args[0]))
.parallel()
.filter((number) -> {
Set<Integer> unique = new HashSet<>();
while (unique.add(number)) {
int value = 0;
while (number > 0) {
value += Math.pow(number % 10, 2);
number /= 10;
}
number = value;
}
return number == 1;
})
.mapToObj((n) -> "Happy: " + n)
.forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment