Finding factors of a given integer

前端 未结 14 1322
终归单人心
终归单人心 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:52

    In order to find the factors of a given number, you only need to check upto the square root of the given number.

    For example, in order to find the factors of 6, you only need to check till 2.45 (√6). The factors of 6 will be 1 and 2, and their converse numbers, i.e. 3 and 6.

    I have made a program that determines the factors of a given number and displays them. Here is the necessary code:

        Scanner input = new Scanner(System.in);
    
        System.out.print("Enter integer: ");
        long num = input.nextLong();
    
        for(long i = 1; i <= Math.sqrt(num); i++) {
            if(num % i == 0) {
                System.out.println(i);
                if(i != num/i) {
                    System.out.println(num/i);
                }
            }
        }
    

    You just need this program to find the factors of a given number. However, if you want to take it a step further and display the factors arranged in ascending order, then the necessary code is as follows:

        Scanner input = new Scanner(System.in);
    
        System.out.print("Enter integer: ");
        long num = input.nextLong();
    
        ArrayList<Long> list1 = new ArrayList<>(), list2 = new ArrayList<>();
    
        long currentTime = System.currentTimeMillis();
    
        for(long i = 1; i <= Math.sqrt(num); i++) {
            if(num % i == 0) {
                list1.add(i);
                if(i != num/i) {
                    list2.add(num/i);
                }
            }
        }
    
        int n1 = list1.size() - 1;
        int n2 = list2.size() - 1;
    
        for(int i = 0; i <= n1; i++) {
            System.out.println(list1.get(i));
        }
    
        for(int i = n2; i >= 0; i--) {
            System.out.println(list2.get(i));
        }
    

    What this does: This program stores the factors of the number upto the number's square root in one list (list1), and the converse of these numbers in another list (list2). It then prints the elements of both lists (as shown).

    0 讨论(0)
  • 2020-12-16 19:55
    public static void printFactors(int number) {
        if (number < 1 )
            System.out.println("Invalid Value");
    
        for (int i = 1 ; i <= number ; ++i) {
            if ( number % i == 0)
                    System.out.println(i);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 19:56

    This code will give you the factors.

    ArrayList<Integer> 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);
    
    0 讨论(0)
  • 2020-12-16 19:59

    Easiest way using recursive function

    public static int factorial(int n){
            if(n!=1)
                return n*factorial(n-1);
            return 1; 
    }   
    
    0 讨论(0)
  • 2020-12-16 20:00

    It looks like you are not going to do something with either f or ff in your while loop? If so, the expression f%ff != 0 is either false (and then it will go to the next in the for loop), or it is true, and it will end up in an infinite loop.

    Are you sure you need the while like this?

    0 讨论(0)
  • 2020-12-16 20:03
    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;
        }
    }
    

    The above solution simply works like calculating prime factors. The difference being for every prime factor we keep calculating the other part of the product i.e the reqd number.

    0 讨论(0)
提交回复
热议问题