Was fiddling with java-8; trying to write a program for getting all Armstrong Numbers between 1
and 10_000_000
.
Following is my working solution:
public class ArmstrongNumbers {
public static void main(String[] args) {
IntStream.range(1, 10_000_000)
.filter((n) -> {
int c = 0, temp = n;
while (temp > 0) {
c += Math.pow(temp % 10, Integer.toString(n).length());
temp /= 10;
}
return c == 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
Why I'm here:
- Can this be made shorter?
- Can any other Java 8 concept be used here?
- Can the use of the
while
loop be avoided and a stream be used somehow (given that it is more elegant)? - Review overall correctness
Here is a link to a list of Armstrong Numbers for verifying the outputs.
Please note that there is a follow up question follow up question.
Was fiddling with java-8; trying to write a program for getting all Armstrong Numbers between 1
and 10_000_000
.
Following is my working solution:
public class ArmstrongNumbers {
public static void main(String[] args) {
IntStream.range(1, 10_000_000)
.filter((n) -> {
int c = 0, temp = n;
while (temp > 0) {
c += Math.pow(temp % 10, Integer.toString(n).length());
temp /= 10;
}
return c == 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
Why I'm here:
- Can this be made shorter?
- Can any other Java 8 concept be used here?
- Can the use of the
while
loop be avoided and a stream be used somehow (given that it is more elegant)? - Review overall correctness
Here is a link to a list of Armstrong Numbers for verifying the outputs.
Please note that there is a follow up question.
Was fiddling with java-8; trying to write a program for getting all Armstrong Numbers between 1
and 10_000_000
.
Following is my working solution:
public class ArmstrongNumbers {
public static void main(String[] args) {
IntStream.range(1, 10_000_000)
.filter((n) -> {
int c = 0, temp = n;
while (temp > 0) {
c += Math.pow(temp % 10, Integer.toString(n).length());
temp /= 10;
}
return c == 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
Why I'm here:
- Can this be made shorter?
- Can any other Java 8 concept be used here?
- Can the use of the
while
loop be avoided and a stream be used somehow (given that it is more elegant)? - Review overall correctness
Here is a link to a list of Armstrong Numbers for verifying the outputs.
Please note that there is a follow up question.
Was fiddling with java-8; trying to write a program for getting all Armstrong Numbers between 1
and 10_000_000
.
Following is my working solution:
public class ArmstrongNumbers {
public static void main(String[] args) {
IntStream.range(1, 10_000_000)
.filter((n) -> {
int c = 0, temp = n;
while (temp > 0) {
c += Math.pow(temp % 10, Integer.toString(n).length());
temp /= 10;
}
return c == 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
Why I'm here:
- Can this be made shorter?
- Can any other Java 8 concept that can be used here?
- Can the use of the
while
loop be avoided and a stream be used somehow (given that it is more elegant)? - Review overall correctness
Here is a link to a list of Armstrong Numbers for verifying the outputs.
Please note that there is a follow up question.
Was fiddling with java-8; trying to write a program for getting all Armstrong Numbers between 1
and 10_000_000
.
Following is my working solution:
public class ArmstrongNumbers {
public static void main(String[] args) {
IntStream.range(1, 10_000_000)
.filter((n) -> {
int c = 0, temp = n;
while (temp > 0) {
c += Math.pow(temp % 10, Integer.toString(n).length());
temp /= 10;
}
return c == 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
Why I'm here:
- Can this be made shorter?
- Can any other Java 8 concept that can be used here?
- Can the use of the
while
loop be avoided and a stream be used somehow (given that it is more elegant)? - Review overall correctness
Here is a link to a list of Armstrong Numbers for verifying the outputs.
Please note that there is a follow up question.
Was fiddling with java-8; trying to write a program for getting all Armstrong Numbers between 1
and 10_000_000
.
Following is my working solution:
public class ArmstrongNumbers {
public static void main(String[] args) {
IntStream.range(1, 10_000_000)
.filter((n) -> {
int c = 0, temp = n;
while (temp > 0) {
c += Math.pow(temp % 10, Integer.toString(n).length());
temp /= 10;
}
return c == 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
Why I'm here:
- Can this be made shorter?
- Can any other Java 8 concept be used here?
- Can the use of the
while
loop be avoided and a stream be used somehow (given that it is more elegant)? - Review overall correctness
Here is a link to a list of Armstrong Numbers for verifying the outputs.
Please note that there is a follow up question.
Was fiddling with java-8; trying to write a program for getting all Armstrong Numbers between 1
and 10_000_000
.
Following is my working solution:
public class ArmstrongNumbers {
public static void main(String[] args) {
IntStream.range(1, 10_000_000)
.filter((n) -> {
int c = 0, temp = n;
while (temp > 0) {
c += Math.pow(temp % 10, Integer.toString(n).length());
temp /= 10;
}
return c == 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
Why I'm here:
- Can this be made shorter?
- Can any other Java 8 concept that can be used here?
- Can the use of the
while
loop be avoided and a stream be used somehow (given that it is more elegant)? - Review overall correctness
Update #1
Corrected exponent of Math.pow()
, updated range and updated output.
Here is a link to a list of Armstrong Numbers for verifying the outputs.
Update #2
Link to follow up Please note that there is a questionfollow up question.
Was fiddling with java-8; trying to write a program for getting all Armstrong Numbers between 1
and 10_000_000
.
Following is my working solution:
public class ArmstrongNumbers {
public static void main(String[] args) {
IntStream.range(1, 10_000_000)
.filter((n) -> {
int c = 0, temp = n;
while (temp > 0) {
c += Math.pow(temp % 10, Integer.toString(n).length());
temp /= 10;
}
return c == 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
Why I'm here:
- Can this be made shorter?
- Can any other Java 8 concept that can be used here?
- Can the use of the
while
loop be avoided and a stream be used somehow (given that it is more elegant)? - Review overall correctness
Update #1
Corrected exponent of Math.pow()
, updated range and updated output.
Here is a link to a list of Armstrong Numbers for verifying the outputs.
Update #2
Link to follow up question.
Was fiddling with java-8; trying to write a program for getting all Armstrong Numbers between 1
and 10_000_000
.
Following is my working solution:
public class ArmstrongNumbers {
public static void main(String[] args) {
IntStream.range(1, 10_000_000)
.filter((n) -> {
int c = 0, temp = n;
while (temp > 0) {
c += Math.pow(temp % 10, Integer.toString(n).length());
temp /= 10;
}
return c == 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
Why I'm here:
- Can this be made shorter?
- Can any other Java 8 concept that can be used here?
- Can the use of the
while
loop be avoided and a stream be used somehow (given that it is more elegant)? - Review overall correctness
Here is a link to a list of Armstrong Numbers for verifying the outputs.
Please note that there is a follow up question.