(Follow Up) Armstrong numbers in a given range using Java 8 (Follow Up)
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.
(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.