代码之家  ›  专栏  ›  技术社区  ›  Vivek Modi

按给定的阶乘数求数

  •  -2
  • Vivek Modi  · 技术社区  · 6 年前

    我需要根据给定的阶乘数求数。我知道如何求一个数的阶乘,但却坚持要求给定阶乘的数。。

    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;
      }
    }
    
    0 回复  |  直到 6 年前
        1
  •  2
  •   Matthew I.    6 年前

    我正确理解您的目标,下面是您要查找的代码:

    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 bc这个函数有两个可能的结果-0,1(见这里的结果表 https://en.wikipedia.org/wiki/Factorial n >= 14 由于整数溢出。 只为传递的阶乘返回正确的数字。

        2
  •  0
  •   forpas    6 年前

    这种方法:

    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
    

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

        3
  •  0
  •   Madhumitha Kolkar    5 年前
        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");
            }
        }