[英]Euler 12, listing factor for triangle numbers
I have been working on this Euler problem (12) and I'm stuck. 我一直在研究这个Euler问题(12),被困住了。 I want each triangle number to be only shown once with all it's factors but I don't know how. 我希望每个三角形数字在所有因素下都只显示一次,但我不知道如何。
public class Euler12 {
public static void main(String[] args) {
int count1 = 0;
int trinumber = 0;
while (count1 < 10) {
count1++;
trinumber += count1;
for (int count2 = 1; count2 <= trinumber; count2++) {
if(trinumber % count2 == 0) {
System.out.println("trinumber: " + trinumber + " " + "factors: " + count2);
}
}
}
}
}
Change your while loop to this 将您的while循环更改为此
while (count1 < 10) {
count1++;
trinumber += count1;
System.out.print("trinumber: " + trinumber + ", factors: ");
for (int count2 = 1; count2 <= trinumber; count2++)
if (trinumber % count2 == 0)
System.out.print(count2 + ", ");
System.out.println();
}
so that this 这样
System.out.print("trinumber: " + trinumber + ", factors: ");
is only executed 10 times, while this 仅执行10次,而这
System.out.print(count2 + ", ");
prints all the factors for the same number always in the same line 始终在同一行中打印相同数字的所有因子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.