Skip to main content
Code Review

Return to Question

edited title
Link
Bilesh Ganguly
  • 499
  • 1
  • 8
  • 23

(Follow Up) Armstrong numbers in a given range using Java 8 (Follow Up)

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Previous Question: Armstrong numbers in a given range using Java 8 Armstrong numbers in a given range using Java 8

Updated program for getting all Armstrong Numbers between 1 and 10_000_000 as per the suggestions in this answer answer.

public class ArmstrongNumbers {
 public static void main(String[] args) {
 IntStream.range(1, 10_000_000)
 .filter((n) -> {
 int size = Integer.toString(n).length();
 return Integer.toString(n)
 .chars()
 .map(d -> d - '0')
 .mapToDouble(v -> Math.pow(v, size))
 .sum() == n;
 }).forEach(System.out::println);
 }
}

Output:

1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315

Comments:

  • Was able to replace the while loop with a stream.
  • Code isn't shorter but is more readable.

Can this program be made even shorter? Any other suggestions also welcome.

Previous Question: Armstrong numbers in a given range using Java 8

Updated program for getting all Armstrong Numbers between 1 and 10_000_000 as per the suggestions in this answer.

public class ArmstrongNumbers {
 public static void main(String[] args) {
 IntStream.range(1, 10_000_000)
 .filter((n) -> {
 int size = Integer.toString(n).length();
 return Integer.toString(n)
 .chars()
 .map(d -> d - '0')
 .mapToDouble(v -> Math.pow(v, size))
 .sum() == n;
 }).forEach(System.out::println);
 }
}

Output:

1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315

Comments:

  • Was able to replace the while loop with a stream.
  • Code isn't shorter but is more readable.

Can this program be made even shorter? Any other suggestions also welcome.

Previous Question: Armstrong numbers in a given range using Java 8

Updated program for getting all Armstrong Numbers between 1 and 10_000_000 as per the suggestions in this answer.

public class ArmstrongNumbers {
 public static void main(String[] args) {
 IntStream.range(1, 10_000_000)
 .filter((n) -> {
 int size = Integer.toString(n).length();
 return Integer.toString(n)
 .chars()
 .map(d -> d - '0')
 .mapToDouble(v -> Math.pow(v, size))
 .sum() == n;
 }).forEach(System.out::println);
 }
}

Output:

1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315

Comments:

  • Was able to replace the while loop with a stream.
  • Code isn't shorter but is more readable.

Can this program be made even shorter? Any other suggestions also welcome.

Source Link
Bilesh Ganguly
  • 499
  • 1
  • 8
  • 23

(Follow Up) Armstrong numbers in a given range using Java 8

Previous Question: Armstrong numbers in a given range using Java 8

Updated program for getting all Armstrong Numbers between 1 and 10_000_000 as per the suggestions in this answer.

public class ArmstrongNumbers {
 public static void main(String[] args) {
 IntStream.range(1, 10_000_000)
 .filter((n) -> {
 int size = Integer.toString(n).length();
 return Integer.toString(n)
 .chars()
 .map(d -> d - '0')
 .mapToDouble(v -> Math.pow(v, size))
 .sum() == n;
 }).forEach(System.out::println);
 }
}

Output:

1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315

Comments:

  • Was able to replace the while loop with a stream.
  • Code isn't shorter but is more readable.

Can this program be made even shorter? Any other suggestions also welcome.

lang-java

AltStyle によって変換されたページ (->オリジナル) /