这条Java流的这一行是什么意思?

发布于 2025-01-21 05:47:18 字数 247 浏览 2 评论 0原文

因此,我有这条代码。它给了我输出[6,28]。 你们知道为什么吗?我不知道有人试图打印什么数字。

System.out.println( IntStream.range(1,30).filter(n -> IntStream.range(1,n).filter(i->n%i == 0).sum() ==n)
               .boxed().collect(Collectors.toList()));

So I have this line of code. It gives me output [6,28].
Do you guys know why? I dont know what kind of numbers was someone trying to print.

System.out.println( IntStream.range(1,30).filter(n -> IntStream.range(1,n).filter(i->n%i == 0).sum() ==n)
               .boxed().collect(Collectors.toList()));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以畏孤独 2025-01-28 05:47:18
IntStream.range(1,n).filter(i -> n % i == 0).sum() == n

通过检查n的所有除数,通过检查n除以潜在的除数分隔(i-> n%i == 0 )。然后将所有除数求和与n本身进行比较。

IntStream.range(1,30).filter(n -> ...).boxed().collect(Collectors.toList())

我上面描述的是1到30之间的数字,并且只保留了比较为true的数字。因此,它计算出1到30之间的所有数字,其中除数总和到数字本身。这些称为完美的数字。前两个是6和28,其次是496,...

IntStream.range(1,n).filter(i -> n % i == 0).sum() == n

calculates all the divisors of n by checking if n divided by the potential divisor has no remainder (i -> n % i == 0). All divisors are then summed and compared with n itself.

IntStream.range(1,30).filter(n -> ...).boxed().collect(Collectors.toList())

Just does what I described above for the numbers between 1 and 30 and only keeps those where the comparison is true. It therefore calculates all the numbers between 1 and 30 where the divisors sum to the number itself. Those are called perfect numbers. The first two are 6 and 28, followed by 496, ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文