Skip to content

Instantly share code, notes, and snippets.

@nokok
Created March 26, 2014 10:21
Show Gist options
  • Save nokok/9780335 to your computer and use it in GitHub Desktop.
Save nokok/9780335 to your computer and use it in GitHub Desktop.
import java.util.function.IntFunction;
import java.util.stream.IntStream;
public class CodeIQ_Collatz {
static IntFunction<Integer> d = n -> n / 2;
static IntFunction<Integer> m = n -> n * 3 + 1;
static IntFunction<Integer> ev = n -> {
if (n % 2 == 0)
return d.apply(n);
else
return m.apply(n);
};
static IntFunction<Boolean> e = n -> {
int r = m.apply(n);
while (r != 1) {
if (r == n)
return true;
r = ev.apply(r);
}
return false;
};
public static void main(String[] args) {
System.out.println(IntStream.range(2, 10000).filter(p -> p % 2 == 0).filter(e::apply).count());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment