简体   繁体   English

确定一个数(n)的因数,然后返回乘以小于(n)的可能的正整数对?

[英]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) 输入大于3(n)的正整数
  2. Program should print all possible pairs of positive integers greater than one whose product is <= to number entered (n) 程序应打印所有可能的正整数对,这些对整数的乘积小于等于所输入数字(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: 对于我的解决方案,我首先确定n的因子,如下所示:

 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). 从那里开始,我似乎应该拉出每个因子,然后将其乘以一个从2开始的递增变量,直到等于或超过(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? 哪个有效,但仅对result 4 - 24 ,因为乘数永远不会增加2 3.实际解决方案只是这些可能解决方案的混合吗? 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. 这里最棘手的部分是确定两个for循环的边界。 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. 这是因为当第一个数字为2 ,将出现最宽的一对,从而迫使第二个数字被减半。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 小于n的平方根的n的因数 - number of factors of n that are less than square root of n 计算乘以给定数字N的非素数对的数量, - Count number of non-prime pairs that when multiplied form a given number N, 如何计算小于n且总和大于n * 2的3个整数的组合数? - How to calculate the number of combinations of 3 integers less than n whose sum is greater than n * 2? 编写一个 while 循环,打印所有可被 10 整除且小于给定数 n 的正数 - Write a while loop that prints all positive numbers that are divisible by 10 and less than a given number n 计算小于或等于N的两个数字的对数,以使对数的数字总和为质数 - Count number of pairs of two numbers less than or equal to N such that Sum of the digits of numbers of pair is Prime 给定数字n,则返回true就是n的所有因子都是质数。 请注意,1和数字本身不视为因素 - Given a number n, return true is all the factors of n are prime numbers. Note that 1 and the number itself are not considered as factors 在小于 O(n^2) 的范围内对给定的整数列表进行排名 - Ranking a given list of integers in less than O(n^2) 如何找到n次因子的因子 - How to find factors of factors of a number n times 打印出素数小于给定数N - Print out prime Number less than a given number N 查找数组在小于O(n ^ 2)内重复的次数 - Find the number of times a number is repeated in an array in less than O(n^2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM