Spaces:
Sleeping
Sleeping
File size: 1,551 Bytes
b12e4cb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
{
"cells": [
{
"cell_type": "markdown",
"id": "c37acdaf",
"metadata": {},
"source": [
"# Assignment 1: Calculate the Factorial\n",
" \"Write a function to compute the factorial of a given number n.\n",
" The factorial of a number n is the product of all positive integers less than or equal to n.\n",
" For example, factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ac5b116f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Factorial of 5 is 120\n"
]
}
],
"source": [
"def factorial(n):\n",
" if n == 0 or n == 1:\n",
" return 1\n",
" else:\n",
" return n * factorial(n - 1)\n",
" \n",
" # Test the function\\,\n",
"number = 5\n",
"result = factorial(number)\n",
"print(f'Factorial of {number} is {result}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80df5cfc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "ml_env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|