查找小于输入数字的最大阶乘

时间:2020-11-01 17:32:35

标签: java numbers factorial

基本上,我想要我的代码执行的操作是将阶乘结果与输入的数字进行比较,以找到小于输入数字的最大阶乘。由于某种原因,它没有打印任何内容。

public class Main {
  public static void main(String[] args) {
     int numinput = 150; //number than we are trying to find the largest factorial less than
     int num = 1; //number than we are solving a factorial for, to test against numinput
     int factorial = 1; //actual result of the factorial
     while (factorial < numinput) //finds the factorial of num
       for(int i = 1; i <= num; i++) {
         factorial *= i;
       }
     num++;
     if (factorial > numinput) {
        num--;
        System.out.println("The largest factorial less than " + numinput + "is !" + factorial);
     }
  }
}

3 个答案:

答案 0 :(得分:0)

while循环没有花括号,因此循环体内唯一的东西是for循环。 num1开始,并且循环中的任何内容都不会增加循环,因此它将永远循环。

不过,您不需要嵌套循环-可以在运行时计算阶乘的单个循环就足够了:

int numinput = 150; //number than we are trying to find the largest factorial less than
int num = 1;
int factorial = 1;

while (factorial < numinput) {
    num++;
    factorial *= num;
}

// We overshot to terminate the loop, go back one number
factorial /= num;
num--;

System.out.println
    ("The largest factorial less than " + numinput + " is " + num + "!, or " + factorial);

答案 1 :(得分:0)

那是因为您的代码在循环时无法摆脱困境-

while (factorial < numinput) //finds the factorial of num
       for(int i = 1; i <= num; i++) {
         factorial *= i;
       }

由于在while循环中没有使用括号,所以它刚好是for循环,并且由于num的值从不增加,因此它将factorial永远乘以1。我想您想这样做-

public class Main {
  public static void main(String[] args) {
     int numinput = 150; //number than we are trying to find the largest factorial less than
     int num = 1; //number than we are solivng a factorial for, to test agaisnt numinput
     int factorial = 1; //actual result of the factorial
     while (factorial < numinput) {
        for(int i = 1; i <= num; i++) {
            factorial *= i;
        }
        num++;
        if (factorial > numinput) {
            num--;
            System.out.println("The largest factorial less than " + numinput + "is !" + factorial);
        }
     }
  }
}

但是我检查了代码的输出是否为150,这是不正确的。我在下面提供我的代码-

public class Main {
  public static void main(String[] args) {
     int numinput = 150; //number than we are trying to find the largest factorial less than
     int num = 1; //number than we are solivng a factorial for, to test agaisnt numinput
     int factorial = 1; //actual result of the factorial
     while (factorial <= numinput) {    // continue multiplying even if equal
        factorial = 1;
        for(int i = 1; i <= num; i++) {
            factorial *= i;
        }
        num++;
     }
    // now the factorial is surely greater than numinput, and it is the factorial of
    // current value of num - 1, we can remove the conditional
    // and reduce the factorial by num -1 since multiplying by num - 1 has
    // made it bigger than numinput
    factorial /= (num - 1);
    System.out.println("The largest factorial less than " + numinput + "is !" + factorial);
  }
}

答案 2 :(得分:0)

我假设您是说小于输入的阶乘的最小整数阶乘。因此,代码应如下所示:


public static void main(String[] args) {
    int input = 150; // example 
    for (int i = 1; i <= input - 1; i++) {
    int sum = (input - 1) * 1;
    }

    System.out.println(input);
}

    
相关问题