Hướng dẫn reverse factorial javascript - javascript giai thừa đảo ngược

-1

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi có một vấn đề với việc tạo một hàm lấy bất kỳ số nào và trả về số mà nó là một giai thừa. Tôi đang cố gắng làm điều này với các phương pháp cơ bản mà tôi biết. Tôi chỉ bắt đầu học: d.

Ví dụ

  • 120 sẽ trở lại "5!"
  • 24 sẽ trở lại "4!"
  • 150 sẽ trả về "Không"

function reverseFactorial(num) {
  let are = [];
for (let i = 1; i <= num; i++) {
  are.push(i)
   } 

  let multi = (acc, e) => {
    if (acc * e === num) {
      return are.length
    } else return "None"
  }
  are.reduce(multi, 1); 

};


console.log(reverseFactorial())

Ý tưởng tôi nhận được từ @Nina Scholz là tốt nhưng trong Chelenge 150 này sẽ trở lại "Không". Tôi đánh giá cao bất kỳ sự giúp đỡ. Điều này có thể được thực hiện với mã này? Hay tôi cần bất cứ điều gì nâng cao hơn?

function reverseFactorial(num) {
   let product = 1;
   let n = 1;
       
   while (product <= num) {
       if (product === num) {
          return `${n}!`
       } product *= ++n;
   }
  
}

Đã hỏi ngày 15 tháng 3 năm 2021 lúc 13:47Mar 15, 2021 at 13:47

Hướng dẫn reverse factorial javascript - javascript giai thừa đảo ngược

JakubjakubJakub

Phù hiệu bằng đồng 2133 bronze badges

5

Bạn có thể đơn giản hóa cách tiếp cận của mình bằng cách sử dụng một vòng lặp duy nhất và trả về giá trị nếu sản phẩm bằng với giá trị mong muốn.

Nếu không, hãy tiếp tục cho đến khi tìm thấy hoặc lớn hơn mong muốn.

Nếu không tìm thấy giá trị, hãy trả về

function reverseFactorial(num) {
   let product = 1;
   let n = 1;
       
   while (product <= num) {
       if (product === num) {
          return `${n}!`
       } product *= ++n;
   }
  
}
2 ở cuối hàm.

function reverseFactorial(num) {
   let product = 1,
       n = 1;
       
   while (product <= num) {
       if (product === num) return `${n}!`;
       product *= ++n;
   }
   return 'None';
}

console.log(reverseFactorial(24));  //   4!
console.log(reverseFactorial(120)); //   5!
console.log(reverseFactorial(150)); // None

Đã trả lời ngày 15 tháng 3 năm 2021 lúc 13:54Mar 15, 2021 at 13:54

Hướng dẫn reverse factorial javascript - javascript giai thừa đảo ngược

Nina Scholznina ScholzNina Scholz

361K24 Huy hiệu vàng325 Huy hiệu bạc365 Huy hiệu Đồng24 gold badges325 silver badges365 bronze badges

Nội dung chính ShowShow

  • Điều gì là một số tất cả về?
  • 1. Factorial hóa một số có đệ quy
  • 2. Factorial hóa một số với vòng lặp trong một thời gian
  • 3. Factorial một số với một vòng lặp cho
  • Một giai thừa trong mã hóa là gì?
  • Một giai thừa và ví dụ là gì?
  • Factorial của một mảng là gì?
  • Chức năng của một giai thừa là gì?

Chức năng giai thừa là gì?Chức năng giai thừa là một công thức toán học được đại diện bởi một dấu chấm than "!".Trong công thức giai thừa, bạn phải nhân tất cả các số nguyên và tích cực tồn tại giữa số xuất hiện trong công thức và số 1.multiply all the integers and positives that exist between the number that appears in the formula and the number 1.multiply all the integers and positives that exist between the number that appears in the formula and the number 1.

Nội dung chính Show

  • Điều gì là một số tất cả về?
  • 1. Factorial hóa một số có đệ quy
  • 2. Factorial hóa một số với vòng lặp trong một thời gian
  • 3. Factorial một số với một vòng lặp cho
  • Một giai thừa trong mã hóa là gì?
  • Một giai thừa và ví dụ là gì?
  • Factorial của một mảng là gì?
  • Chức năng của một giai thừa là gì?

Chức năng giai thừa là gì?Chức năng giai thừa là một công thức toán học được đại diện bởi một dấu chấm than "!".Trong công thức giai thừa, bạn phải nhân tất cả các số nguyên và tích cực tồn tại giữa số xuất hiện trong công thức và số 1.multiply all the integers and positives that exist between the number that appears in the formula and the number 1.multiply all the integers and positives that exist between the number that appears in the formula and the number 1., the factorial of a non-negative integer n can be a tricky algorithm. In this article, I’m going to explain three approaches, first with the recursive function, second using a while loop and third using a for loop.

Chúng ta đã thấy một cách tiếp cận đệ quy trên một chuỗi trong bài viết trước, làm thế nào để đảo ngược một chuỗi trong JavaScript theo 3 cách khác nhau? Lần này chúng tôi sẽ áp dụng cùng một khái niệm trên một số.How to Reverse a String in JavaScript in 3 Different Ways ? This time we will apply the same concept on a number.How to Reverse a String in JavaScript in 3 Different Ways ? This time we will apply the same concept on a number.How to Reverse a String in JavaScript in 3 Different Ways ? This time we will apply the same concept on a number.

Thử thách thuật toán

Trả lại giai thừa của số nguyên được cung cấp.

Nếu số nguyên được biểu diễn bằng chữ N, thì một giai thừa là sản phẩm của tất cả các số nguyên dương nhỏ hơn hoặc bằng n.

Các nhân viên thường được đại diện với ký hiệu tốc ký n!n!n!n!

Ví dụ: 5! = 1 * 2 * 3 * 4 * 5 = 1205! = 1 * 2 * 3 * 4 * 5 = 1205! = 1 * 2 * 3 * 4 * 5 = 1205! = 1 * 2 * 3 * 4 * 5 = 120


function factorialize(num) {
  return num;
}
factorialize(5);

Cung cấp các trường hợp thử nghiệm

  • Factorialize (0) sẽ trở lại 1 should return 1 should return 1 should return 1
  • Factorialize (5) sẽ trả lại 120 should return 120 should return 120 should return 120
  • Factorialize (10) sẽ trả lại 3628800 should return 3628800 should return 3628800 should return 3628800
  • Factorialize (20) sẽ trả về 2432902008176640000 should return 2432902008176640000 should return 2432902008176640000 should return 2432902008176640000

Điều gì là một số tất cả về?

Khi bạn thực hiện một số, bạn đang nhân số đó với mỗi số liên tiếp trừ đi một số.

Nếu số của bạn là 5, bạn sẽ có:

5! = 5 * 4 * 3 * 2 * 1

Mô hình sẽ là:

0! = 1
1! = 1
2! = 2 * 1
3! = 3 * 2 * 1
4! = 4 * 3 * 2 * 1
5! = 5 * 4 * 3 * 2 * 1

1. Factorial hóa một số có đệ quy

function factorialize(num) {
  // If the number is less than 0, reject it.
  if (num < 0) 
        return -1;
    
  // If the number is 0, its factorial is 1.
  else if (num == 0) 
      return 1;
    
  // Otherwise, call the recursive procedure again
    else {
        return (num * factorialize(num - 1));
        /* 
        First Part of the recursion method
        You need to remember that you won’t have just one call, you’ll have several nested calls
        
        Each call: num === "?"        	         num * factorialize(num - 1)
        1st call – factorialize(5) will return    5  * factorialize(5 - 1) // factorialize(4)
        2nd call – factorialize(4) will return    4  * factorialize(4 - 1) // factorialize(3)
        3rd call – factorialize(3) will return    3  * factorialize(3 - 1) // factorialize(2)
        4th call – factorialize(2) will return    2  * factorialize(2 - 1) // factorialize(1)
        5th call – factorialize(1) will return    1  * factorialize(1 - 1) // factorialize(0)
        
        Second part of the recursion method
        The method hits the if condition, it returns 1 which num will multiply itself with
        The function will exit with the total value
        
        5th call will return (5 * (5 - 1))     // num = 5 * 4
        4th call will return (20 * (4 - 1))    // num = 20 * 3
        3rd call will return (60 * (3 - 1))    // num = 60 * 2
        2nd call will return (120 * (2 - 1))   // num = 120 * 1
        1st call will return (120)             // num = 120
        
        If we sum up all the calls in one line, we have
        (5 * (5 - 1) * (4 - 1) * (3 - 1) * (2 - 1)) = 5 * 4 * 3 * 2 * 1 = 120
        */
    }
}
factorialize(5);
function factorialize(num) {
  if (num < 0) 
        return -1;
  else if (num == 0) 
      return 1;
  else {
      return (num * factorialize(num - 1));
  }
}
factorialize(5);

2. Factorial hóa một số với vòng lặp trong một thời gian

function factorialize(num) {
  // Step 1. Create a variable result to store num
  var result = num;
   
  // If num = 0 OR num = 1, the factorial will return 1
  if (num === 0 || num === 1) 
    return 1; 
 
  // Step 2. Create the WHILE loop 
  while (num > 1) { 
    num--; // decrementation by 1 at each iteration
    result = result * num; // or result *= num; 
    /* 
                    num           num--      var result      result *= num         
    1st iteration:   5             4            5             20 = 5 * 4      
    2nd iteration:   4             3           20             60 = 20 * 3
    3rd iteration:   3             2           60            120 = 60 * 2
    4th iteration:   2             1          120            120 = 120 * 1
    5th iteration:   1             0          120
    End of the WHILE loop 
    */
  }
     
  // Step 3. Return the factorial of the provided integer
  return result; // 120
}
factorialize(5);
function factorialize(num) {
  var result = num;
  if (num === 0 || num === 1) 
    return 1; 
  while (num > 1) { 
    num--;
    result *= num;
  }
  return result;
}
factorialize(5);

3. Factorial một số với một vòng lặp cho

function reverseFactorial(num) {
   let product = 1;
   let n = 1;
       
   while (product <= num) {
       if (product === num) {
          return `${n}!`
       } product *= ++n;
   }
  
}
0
function reverseFactorial(num) {
   let product = 1;
   let n = 1;
       
   while (product <= num) {
       if (product === num) {
          return `${n}!`
       } product *= ++n;
   }
  
}
1

Một giai thừa trong mã hóa là gì?

Một giai thừa và ví dụ là gì?In this article, I’ll explain how to solve freeCodeCamp’s “Repeat a string repeat a string” challenge. This involves…
In this article, I’ll explain how to solve freeCodeCamp’s “Repeat a string repeat a string” challenge. This involves…

Factorial của một mảng là gì?In this article, I’ll explain how to solve freeCodeCamp’s “Confirm the Ending” challenge.
In this article, I’ll explain how to solve freeCodeCamp’s “Confirm the Ending” challenge.

Chức năng của một giai thừa là gì?This article is based on Free Code Camp Basic Algorithm Scripting “Reverse a String”
This article is based on Free Code Camp Basic Algorithm Scripting “Reverse a String”

Trong toán học, giai đoạn của một số nguyên không âm N có thể là một thuật toán khó khăn. Trong bài viết này, tôi sẽ giải thích ba cách tiếp cận, thứ nhất với hàm đệ quy, thứ hai sử dụng vòng lặp thời gian và thứ ba bằng cách sử dụng vòng lặp., the factorial of a non-negative integer n can be a tricky algorithm. In this article, I’m going to explain three approaches, first with the recursive function, second using a while loop and third using a for loop.This article is based on Free Code Camp Basic Algorithm Scripting “Check for Palindromes”.
This article is based on Free Code Camp Basic Algorithm Scripting “Check for Palindromes”.

Chúng ta đã thấy một cách tiếp cận đệ quy trên một chuỗi trong bài viết trước, làm thế nào để đảo ngược một chuỗi trong JavaScript theo 3 cách khác nhau? Lần này chúng tôi sẽ áp dụng cùng một khái niệm trên một số.How to Reverse a String in JavaScript in 3 Different Ways ? This time we will apply the same concept on a number.This article is based on Free Code Camp Basic Algorithm Scripting “Find the Longest Word in a String”.
This article is based on Free Code Camp Basic Algorithm Scripting “Find the Longest Word in a String”.

Thử thách thuật toánThis article is based on Free Code Camp Basic Algorithm Scripting “Title Case a Sentence”.
This article is based on Free Code Camp Basic Algorithm Scripting “Title Case a Sentence”.

Trả lại giai thừa của số nguyên được cung cấp.In this article, I’m going to explain how to solve Free Code Camp’s “Return Largest Numbers in Arrays” challenge. This…
In this article, I’m going to explain how to solve Free Code Camp’s “Return Largest Numbers in Arrays” challenge. This…

Nếu số nguyên được biểu diễn bằng chữ N, thì một giai thừa là sản phẩm của tất cả các số nguyên dương nhỏ hơn hoặc bằng n.

Các nhân viên thường được đại diện với ký hiệu tốc ký n!n!n!Medium, Twitter, Github and LinkedIn, right after you click the green heart below ;-)

Ví dụ: 5! = 1 * 2 * 3 * 4 * 5 = 1205! = 1 * 2 * 3 * 4 * 5 = 1205! = 1 * 2 * 3 * 4 * 5 = 120



Cung cấp các trường hợp thử nghiệm

Một giai thừa trong mã hóa là gì?

Một giai thừa và ví dụ là gì?In this article, I’ll explain how to solve freeCodeCamp’s “Repeat a string repeat a string” challenge. This involves…the sum of multiplication of all the integers smaller than that positive integer. For example, factorial of 5 is 5 * 4 * 3 * 2 * 1 which equals to 120. Factorial Program in C: All positive descending integers are added together to determine the factor of n.

Factorial của một mảng là gì?In this article, I’ll explain how to solve freeCodeCamp’s “Confirm the Ending” challenge.

Chức năng của một giai thừa là gì?This article is based on Free Code Camp Basic Algorithm Scripting “Reverse a String”products of every whole number from 1 to n. In other words, take the number and multiply through to 1. For example: If n is 3, then 3! is 3 x 2 x 1 = 6.

Factorial của một mảng là gì?

Các công thức đệ quy để tính toán giai thừa của một số là: thực tế (n) = n*thực tế (n-1).Do đó, chúng tôi sẽ xây dựng một mảng theo cách từ dưới lên bằng cách sử dụng đệ quy ở trên.Khi chúng tôi đã lưu trữ các giá trị trong mảng thì chúng tôi có thể trả lời các truy vấn trong thời gian O (1).fact(N) = N*fact(N-1). Hence, we will build an array in a bottom-up manner using the above recursion. Once we have stored the values in the array then we can answer the queries in O(1) time.fact(N) = N*fact(N-1). Hence, we will build an array in a bottom-up manner using the above recursion. Once we have stored the values in the array then we can answer the queries in O(1) time.fact(N) = N*fact(N-1). Hence, we will build an array in a bottom-up manner using the above recursion. Once we have stored the values in the array then we can answer the queries in O(1) time.

Chức năng của một giai thừa là gì?

Chức năng giai thừa là gì?Chức năng giai thừa là một công thức toán học được đại diện bởi một dấu chấm than "!".Trong công thức giai thừa, bạn phải nhân tất cả các số nguyên và tích cực tồn tại giữa số xuất hiện trong công thức và số 1.multiply all the integers and positives that exist between the number that appears in the formula and the number 1.multiply all the integers and positives that exist between the number that appears in the formula and the number 1.multiply all the integers and positives that exist between the number that appears in the formula and the number 1.