Skip to content

Instantly share code, notes, and snippets.

@dbasch

dbasch/y.java Secret

Last active August 29, 2015 14:04
Show Gist options
  • Save dbasch/f212a600f8170576db9e to your computer and use it in GitHub Desktop.
Save dbasch/f212a600f8170576db9e to your computer and use it in GitHub Desktop.
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);}}
@brikis98
Copy link

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);}}

@dbasch
Copy link
Author

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
Copy link

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