简体   繁体   中英

Determine factors of a number (n), then return possible pairs of positive integers that when multiplied together are less than (n)?

Basically, the directions go as such:

  1. Enter positive integer greater than 3 (n)
  2. Program should print all possible pairs of positive integers greater than one whose product is <= to number entered (n)

Here is a sample output:

Enter an integer: 24
4 = 2 x 2
6 = 2 x 3
8 = 2 x 4
10 = 2 x 5
12 = 2 x 6
14 = 2 x 7
16  = 2 x 8
18 =  2 x 9
22 = 2 x 11
24 = 2 x 12
9 = 3 x 3
12 =  3 x 4
15 = 3 x 5
18 = 3 x 6
21 = 3 x 7
24 = 3 x 8
16 = 4 x 4
20 = 4 x 5
24 = 4 x 6

(Note: Products can appear more than once, but pairs should not)

For my solution, I started by determining the factors of n like so:

 public static void main(String[] args){
        Scanner keyboard = new Scanner(System.in);
        int factors = 0;

        System.out.println("Enter integer:");
        int n = keyboard.nextInt();

            for(int i = 2; i <=n ; i++) {
                if(n % i == 0) {
                    System.out.println(i);
                  }
               }

From there, it seems I should pull each factor and multiply it by an incremented variable starting at 2 until it is equal to or exceeds (n). I started to thing maybe this was incorrect, so I tried something like this instead:

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter integer:");
    int n = keyboard.nextInt();

    int index = 2;
    int multiplier = 2;
    int result = 0;
    while(result < n) {
        result = multiplier * index;
        System.out.println(result);
        index++;
    }

Which works, but only for result 4 - 24 , as multiplier never increments two 3. Is the actual solution just a hybrid of these possible solutions? Guidance in the correct direction would be appreciated, thank you!

I think your current approach is off, and in particular is missing one critical element to solving this: a double loop. I think the easiest way to handle this is to loop twice and generate all pairs meeting the requirements.

int number = 24;

for (int i=2; i < (int)Math.ceil(Math.sqrt(number)); ++i) {
    for (int j=i; j <= number / 2; ++j) {
        int pair = i * j;
        if (pair <= number) {
            System.out.println(pair + " = " + i + " x " + j);
        }
    }
}

The tricky part here was in determining the bounds for the two for loops. The second loop variable starts with the value of the first, and can get as large as half the input number. This is because the widest pair would occur when the first number be 2 , forcing the second number to be the input halved. The bounds on the first loop are a bit more complex. We used the ceiling of the square root as the bounds, because the largest it can ever be would occur when both numbers are the same.

Try something like this.

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter integer: ");
    int n = keyboard.nextInt();

    for (int i = 2; i <= n / 2; i++) {
        for (int j = 2; i * j <= n; j++) {
            System.out.println(i + " x " + j + " = " + (i * j));
        }
    }

The result as follows.

Enter integer: 24
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
2 x 11 = 22
2 x 12 = 24
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
7 x 2 = 14
7 x 3 = 21
8 x 2 = 16
8 x 3 = 24
9 x 2 = 18
10 x 2 = 20
11 x 2 = 22
12 x 2 = 24

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM