Movatterモバイル変換


[0]ホーム

URL:


Add Neon Auth to your app without leaving Cursor or Claude

PostgreSQL FACTORIAL() Function

Summary: in this tutorial, you will learn how to use the PostgreSQLfactorial() function to calculate the factorial of a number.

Introduction to the PostgreSQL factorial() function

The factorial of a non-negative integer n is the product of all positive integers less than or equal ton:

n!=n×(n−1)×(n−2)×…×2×1

By convention, 0! = 1.

In PostgreSQL, you can use the built-infactorial() function to calculate the factorial of a number:

factorial(n)

In this syntax,n is the number that you want to calculate the factorial. Thefactorial() function returns null if n is null.

If n is negative, the factorial() function will issue an error:

ERROR:  factorial of a negativenumber is undefined

PostgreSQL factorial() function example

Let’s take some examples of using thefactorial() function.

The following example uses thefactorial() function to calculate the factorial of the number 10:

SELECT factorial(5);

Output:

factorial-----------       120(1 row)

Summary

  • Use thefactorial() function to calculate the factorial of a number.

Last updated on

Was this page helpful?
Thank you for your feedback!

[8]ページ先頭

©2009-2025 Movatter.jp