Create a gist now

Instantly share code, notes, and snippets.

@dbasch /y.java Secret
Last active Aug 29, 2015

What would you like to do?
class y{static int s(int x){return(x<1?x:x%10+s(x/10));}public static void main(String a[]){int i=0,j=0;for(;i++<1e6;j+=s(i)==42?1:0);System.out.print(j);}}

Here's a shorter version using Java 8:

class S{public static void main(String a[]){int i=0,j=0;for(;i++<1e6;j+=(i+"").chars().map(x->x-48).sum()==42?1:0);System.out.print(j);}}
Owner

dbasch commented Jul 31, 2014

Nice. Most Java people are probably still not used to the functional style, but it does make the language less verbose :)

brikis98 commented Aug 4, 2014

Java 8 does improve things. I actually tried to change your for loop to use Java 8 as well:

java.util.stream.IntStream.range(0, 1000000).filter(x->(x+"").chars().map(x->x-48).sum()==42).count()

Although arguably more readable, your crazy minified for loop ended up shorter due to the need to import IntStream and spell out 1e6 fully :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment