Last active
August 29, 2015 14:20
-
-
Save brunoborges/3466169ab70505f28851 to your computer and use it in GitHub Desktop.
This file contains 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
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