通过给定阶乘数查找数字

时间:2019-05-26 08:33:08

标签: java

我需要通过给定的阶乘数查找数字。我知道如何找到一个数字的阶乘。但是要找到给定阶乘的数字。

class Fact{  
 public static void main(String args[]){  
  int i,fact=1;  
  int number=5;  
  for(i=1;i<=number;i++){    
      fact=fact*i;    
  }    
  int yo = number(number);
  System.out.println(fact + " " + yo );   

 }  

  public static int number(int no){
      for(int i = 1 ; i >= no ; i--){
          no = no / i;
      }
      return no;
  }
}

3 个答案:

答案 0 :(得分:2)

据我所知,您正在寻找的代码如下:

class Fact {

    public static void main(String args[]) {
        int n = 5;

        int fact = factorial(n);
        int number = number(fact);

        System.out.println(n + " "+fact + " " + number);
    }

    public static int factorial(int n) {
        int fact = 1;
        for (int i = 1; i <= n; i++) {
            fact = fact * i;
        }
        return fact;
    }

    public static int number(int factorial) {
        int i = 0;
        do {
            i++;
            factorial = factorial / i;
        } while (factorial > 1);
        return i;
    }
}

输出:

5 120 5

P.S。函数number对于参数n! = 1不能正确工作,因为该功能有两个可能的结果-0、1(请参见此处的结果表https://en.wikipedia.org/wiki/Factorial)和n >= 14到整数溢出。 number仅对于通过的阶乘返回正确的数字。

答案 1 :(得分:0)

此方法:

public static int number(int factorial) {
    if (factorial <= 0) return -1;
    if ((factorial == 1) || (factorial == 2)) return factorial;
    if (factorial % 2 != 0) return -1;

    int i = 1;
    int prod = 1;
    while (prod < factorial) {
        prod *= ++i;
    }
    return (prod == factorial) ? i : -1;
}

将整数factorial用作参数,并检查它是否为整数的阶乘并返回该整数。
最多限制为12!(这是int数据类型造成的限制)
对于1作为参数,它返回1而不返回0,这也是自0! = 1起的解决方案。
如果不是整数的阶乘,则返回-1
所以,这:

public static void main(String[] args) {
    for (int i = -1; i <= 1_000_000_000; i++) {
        int x = number(i);
        if (x > 0)
            System.out.println("Number = " + i + " is the factorial of " + x);
    }
}

将打印:

Number = 1 is the factorial of 1
Number = 2 is the factorial of 2
Number = 6 is the factorial of 3
Number = 24 is the factorial of 4
Number = 120 is the factorial of 5
Number = 720 is the factorial of 6
Number = 5040 is the factorial of 7
Number = 40320 is the factorial of 8
Number = 362880 is the factorial of 9
Number = 3628800 is the factorial of 10
Number = 39916800 is the factorial of 11
Number = 479001600 is the factorial of 12

我不确定这是否是最有效的方法,但是它会给出正确的结果。

答案 2 :(得分:0)

    import java.util.Scanner;

    public class FindNumber{
        public static void main(String args[]){
            int num,fact=1,i;
            Scanner sc = new Scanner(System.in);

            num = sc.nextInt();
            if(num<=0){
                System.out.println("Invalid Input");
                System.exit(0);
            }

            if(num==1){
                System.out.println("1");
                System.exit(0);
            }
if(num==2){
System.out.println("2");
System.exit(0);
}

            for(i=1;i<=num/2;i++){
                fact = fact*i;

                if(fact==num){
                    System.out.println(""+i);
                    System.exit(0);
                }
            }
            System.out.println("Sorry. The given number is not a perfect factorial");
        }
    }