Finding factors of a given integer

前端 未结 14 1370
终归单人心
终归单人心 2020-12-16 19:52

I have something like this down:

int f = 120;
for(int ff = 1; ff <= f; ff++){
    while (f % ff != 0){            
}

Is there anything w

14条回答
  •  生来不讨喜
    2020-12-16 19:56

    This code will give you the factors.

    ArrayList arr = new ArrayList<>();
            int x=48;
            int y=1;
            while(x!=1)
            {
                if(x%y==0)
                {
                    x=x/y;
                    arr.add(y);
                    if(y==1)
                    {
                        y++;
                    }
                }
                else
                {
                    y+=1;
                }
            }
            System.out.println(arr);
    

提交回复
热议问题