Skip to main content
Code Review

Return to Question

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

Was fiddling with ; 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 ; 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 ; 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.

deleted 9 characters in body
Source Link
Bilesh Ganguly
  • 499
  • 1
  • 8
  • 23

Was fiddling with ; 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 ; 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 ; 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.

removed edid-scars
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141

Was fiddling with ; 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 ; 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 ; 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.

added 134 characters in body
Source Link
Bilesh Ganguly
  • 499
  • 1
  • 8
  • 23
Loading
deleted 1 character in body
Source Link
Bilesh Ganguly
  • 499
  • 1
  • 8
  • 23
Loading
added 151 characters in body
Source Link
Bilesh Ganguly
  • 499
  • 1
  • 8
  • 23
Loading
added 151 characters in body
Source Link
Bilesh Ganguly
  • 499
  • 1
  • 8
  • 23
Loading
Source Link
Bilesh Ganguly
  • 499
  • 1
  • 8
  • 23
Loading
lang-java

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