Wil*_*son 6 java integer for-loop while-loop factors
我有这样的事情:
int f = 120;
for(int ff = 1; ff <= f; ff++){
while (f % ff != 0){
}
Run Code Online (Sandbox Code Playgroud)
我的循环找到因素有什么问题吗?我对for和while语句的工作方式感到困惑,所以很可能他们完全错了.
在此之后,我将如何为所述因素分配变量?
小智 10
public class Solution {
public ArrayList<Integer> allFactors(int a) {
int upperlimit = (int)(Math.sqrt(a));
ArrayList<Integer> factors = new ArrayList<Integer>();
for(int i=1;i <= upperlimit; i+= 1){
if(a%i == 0){
factors.add(i);
if(i != a/i){
factors.add(a/i);
}
}
}
Collections.sort(factors);
return factors;
}
}
Run Code Online (Sandbox Code Playgroud)
上述解决方案就像计算素因子一样.不同之处在于我们一直在计算产品的其他部分,即reqd数.
Tot*_*Zam 10
以下代码将返回给定数字的所有因子的列表:
public ArrayList<Integer> findFactors(int num) {
ArrayList<Integer> factors = new ArrayList<Integer>();
// Skip two if the number is odd
int incrementer = num % 2 == 0 ? 1 : 2;
for (int i = 1; i <= Math.sqrt(num); i += incrementer) {
// If there is no remainder, then the number is a factor.
if (num % i == 0) {
factors.add(i);
// Skip duplicates
if (i != num / i) {
factors.add(num / i);
}
}
}
// Sort the list of factors
Collections.sort(factors);
return factors;
}
Run Code Online (Sandbox Code Playgroud)
这个答案以两种方式改善了Sharad Dargan的答案:
根据本答案中使用的想法,您可以根据数字是偶数还是奇数来确定要递增的值,从而加快解决方案的速度.
在for循环之前添加以下代码行:
int incrementer = num % 2 == 0 ? 1 : 2;
Run Code Online (Sandbox Code Playgroud)
然后将循环的最后一部分更改为:
i += incrementer
Run Code Online (Sandbox Code Playgroud)
如果数字是奇数,则它将跳过所有偶数,而不是总是递增1,无论如何.
Sharad将上限值存储在变量中,然后在for循环中使用该变量:
int upperlimit = (int)(Math.sqrt(a));
...
for(int i = 1; i <= upperlimit; i+= 1)
Run Code Online (Sandbox Code Playgroud)
而是Math.sqrt(num)直接放在for循环中并跳过上限变量:
for (int i = 1; i <= Math.sqrt(num); i += incrementer) {
Run Code Online (Sandbox Code Playgroud)
这将允许您跳过代码的转换部分,创建更清晰的代码.
然后可以使用一些JUnit测试用例:
@Test
public void test12() {
FindFactors find = new FindFactors();
int num = 12;
List<Integer> factors = Arrays.asList(1, 2, 3, 4, 6, 12);
assertEquals(factors, find.findFactors(num));
}
@Test
public void test1000000() {
FindFactors find = new FindFactors();
int num = 1000000;
List<Integer> factors = Arrays.asList(1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200,
250, 320, 400, 500, 625, 800, 1000, 1250, 1600, 2000, 2500, 3125, 4000, 5000, 6250, 8000, 10000, 12500,
15625, 20000, 25000, 31250, 40000, 50000, 62500, 100000, 125000, 200000, 250000, 500000, 1000000);
assertEquals(factors, find.findFactors(num));
}
@Test
public void test1() {
FindFactors find = new FindFactors();
int num = 1;
List<Integer> factors = Arrays.asList(1);
assertEquals(factors, find.findFactors(num));
}
@Test
public void test0() {
FindFactors find = new FindFactors();
int num = 0;
List<Integer> factors = new ArrayList<Integer>();
assertEquals(factors, find.findFactors(num));
}
Run Code Online (Sandbox Code Playgroud)
以下是如何获得给定数字的所有因子.
public class Factors {
public static void main(String[] args){
int n = 420;
for(int i=2; i<=n; i++){
while(n%i==0){
System.out.println(i + "| " + n);
System.out.println(" -----");
n = n/i;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
2| 420
-----
2| 210
-----
3| 105
-----
5| 35
-----
7| 7
-----
Run Code Online (Sandbox Code Playgroud)
看起来您不会在 while 循环中使用f或执行某些操作?ff如果是这样,则表达式f%ff != 0要么为 false(然后它将转到 for 循环中的下一个),要么为 true,并且最终将陷入无限循环。
你确定你需要这样的时间吗?