Recursive Factorial Function in BASH Programming by justyy

View this thread on steempeak.com
· @justyy ·
$21.78
Recursive Factorial Function in BASH Programming
As you may know, the famous <a href="https://helloacm.com/teaching-kids-programming-sum-of-distinct-positive-factorial-numbers-via-depth-first-search-algorithm/" title="Teaching Kids Programming – Sum of Distinct Positive Factorial Numbers via Depth First Search Algorithm">factorial</a> can be done in Recursion via:

```
f(n) = n * f(n-1) where  f(0) = f(1) = 1
```

<a href="https://helloacm.com/bash-function-to-install-docker/" title="BASH Function to Install Docker">BASH</a> supports function declaration and recursion - just like other modern programming language. See below the Recursive function to compute the factorial values.

```
#!/bin/bash

function f() {
    local n=$1
    if [[ $n -eq 0 ]]; then
        echo 1
    else
        echo $((n*$(f $n-1)))
    fi
}

for i in {1..10}; do
    echo "$i!=$(f $i)"
done
```

This BASH script prints the first 10 factorial numbers:

```
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
10!=3628800
```

We use "local" to keep the variables at scope of function, and we can use $() to call a function recursively.

See also: <a href="https://helloacm.com/teaching-kids-programming-recursion-in-five-minutes/" title="Teaching Kids Programming – Recursion in Five Minutes">Teaching Kids Programming – Recursion in Five Minutes</a>

Reposted to [Blog](https://helloacm.com/recursive-factorial-function-in-bash-programming/)



^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
---------------
## NEW! [Following my Trail (Upvote or/and Downvote)](https://steemit.com/witness-category/@justyy/following-my-trail-upvote-or-and-downvote-by-authorizing-posting-key-to-me)


Follow me for topics of *Algorithms, Blockchain and Cloud.*
I am @justyy - a Steem Witness
[https://steemyy.com](https://steemyy.com)

#### My contributions
- [Video Downloader](https://weibomiaopai.com/download-video-parser.php)
- [Steem Blockchain Tools](https://steemyy.com)
- [Free Cryptos API](https://steemit.com/witness-category/@justyy/serverless-api-to-query-the-cryptos-and-fiat)
- [VPS Database](https://anothervps.com/vps-database/)
- [Computing Technology Blog](https://helloacm.com)
- [A few useless tools](https://helloacm.com/tools/)
- [And some other online software/tools](https://steakovercooked.com/Software.Home)
- [Merge Files/Videos](https://slowapi.com/merge-videos/)
- [LOGO Turtle Programming Chrome Extension](https://chrome.google.com/webstore/detail/logo-turtle-graphics/dcoeaobaokbccdcnadncifmconllpihp)
- [Teaching Kids Programming - Youtube Channel](https://www.youtube.com/channel/UCK2ugmUOoYne1qQfSAph4nQ?view_as=subscriber) and [All Contents](https://github.com/DoctorLai/Teaching-Kids-Programming/blob/main/README.md)

#### Delegation Service
[Important Update of Delegation Service!](https://steemit.com/witness-category/@justyy/important-update-of-delegation-service)

- Delegate 1000 to justyy: [Link](https://steemyy.com/sp-delegate-form/?delegatee=justyy&amp;amount=1000)
- Delegate 5000 to justyy: [Link](https://steemyy.com/sp-delegate-form/?delegatee=justyy&amp;amount=5000)
- Delegate 10000 to justyy: [Link](https://steemyy.com/sp-delegate-form/?delegatee=justyy&amp;amount=10000)

#### Support me
If you like my work, please: 
- [Buy Me a Coffee](https://justyy.com/out/buymeacoffee), Thanks!
- [Become my Sponsor](https://github.com/sponsors/DoctorLai), Thanks!
- Voting for me:
https://steemit.com/~witnesses type in **justyy** and click ***VOTE***
https://steemyy.com/images/vote-for-justyy.jpg


1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
2. Vote @justyy as Witness:  https://steemyy.com/witness-voting/?witness=justyy&amp;action=approve
3. Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&amp;action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses


-------------
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 127 others
👎  , , ,
properties (23)
post_id92,158,692
authorjustyy
permlinkrecursive-factorial-function-in-bash-programming
categoryprogramming
json_metadata{"tags":["programming","bash","sct","whalepower","blog","upmefund","palnet","zzan"],"users":["justyy"],"image":["https:\/\/steemyy.com\/images\/vote-for-justyy.jpg"],"links":["https:\/\/helloacm.com\/teaching-kids-programming-sum-of-distinct-positive-factorial-numbers-via-depth-first-search-algorithm\/","https:\/\/helloacm.com\/bash-function-to-install-docker\/","https:\/\/helloacm.com\/teaching-kids-programming-recursion-in-five-minutes\/","https:\/\/helloacm.com\/recursive-factorial-function-in-bash-programming\/","https:\/\/steemit.com\/witness-category\/@justyy\/following-my-trail-upvote-or-and-downvote-by-authorizing-posting-key-to-me","https:\/\/steemyy.com","https:\/\/weibomiaopai.com\/download-video-parser.php","https:\/\/steemit.com\/witness-category\/@justyy\/serverless-api-to-query-the-cryptos-and-fiat","https:\/\/anothervps.com\/vps-database\/","https:\/\/helloacm.com","https:\/\/helloacm.com\/tools\/","https:\/\/steakovercooked.com\/Software.Home","https:\/\/slowapi.com\/merge-videos\/","https:\/\/chrome.google.com\/webstore\/detail\/logo-turtle-graphics\/dcoeaobaokbccdcnadncifmconllpihp","https:\/\/www.youtube.com\/channel\/UCK2ugmUOoYne1qQfSAph4nQ?view_as=subscriber","https:\/\/github.com\/DoctorLai\/Teaching-Kids-Programming\/blob\/main\/README.md","https:\/\/steemit.com\/witness-category\/@justyy\/important-update-of-delegation-service","https:\/\/steemyy.com\/sp-delegate-form\/?delegatee=justyy&amp;amount=1000","https:\/\/steemyy.com\/sp-delegate-form\/?delegatee=justyy&amp;amount=5000","https:\/\/steemyy.com\/sp-delegate-form\/?delegatee=justyy&amp;amount=10000","https:\/\/justyy.com\/out\/buymeacoffee","https:\/\/github.com\/sponsors\/DoctorLai","https:\/\/steemit.com\/~witnesses","https:\/\/steemyy.com\/sp-delegate-form\/?delegatee=justyy","https:\/\/steemyy.com\/witness-voting\/?witness=justyy&amp;action=approve","https:\/\/steemyy.com\/witness-voting\/?witness=justyy&amp;action=proxy"],"app":"steemit\/0.2","format":"markdown"}
created2021-06-23 08:31:57
last_update2021-06-23 08:31:57
depth0
children3
net_rshares43,826,838,003,364
last_payout2021-06-30 08:31:57
cashout_time1969-12-31 23:59:59
total_payout_value10.977 SBD
curator_payout_value10.799 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length3,883
author_reputation2,057,469,156,047,835
root_title"Recursive Factorial Function in BASH Programming"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (195)
@rme ·
hello, sir @justyy plz fix the voting bot. Every day I need to contact you to get upvote. It's already 40 minutes but my today's post gets no upvote.

my post: https://steemit.com/hive-129948/@rme/php-half-dozones-string-operations-in-php
properties (22)
post_id92,170,422
authorrme
permlinkqv6ats
categoryprogramming
json_metadata{"users":["justyy"],"links":["https:\/\/steemit.com\/hive-129948\/@rme\/php-half-dozones-string-operations-in-php"],"app":"steemit\/0.2"}
created2021-06-23 21:10:39
last_update2021-06-23 21:10:39
depth1
children2
net_rshares0
last_payout2021-06-30 21:10:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length238
author_reputation14,491,425,587,942,198
root_title"Recursive Factorial Function in BASH Programming"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@justyy ·
done.
properties (22)
post_id92,170,578
authorjustyy
permlinkqv6bbj
categoryprogramming
json_metadata{"app":"steemit\/0.2"}
created2021-06-23 21:21:21
last_update2021-06-23 21:21:21
depth2
children1
net_rshares0
last_payout2021-06-30 21:21:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length5
author_reputation2,057,469,156,047,835
root_title"Recursive Factorial Function in BASH Programming"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@rme ·
thank you :D
properties (22)
post_id92,170,715
authorrme
permlinkqv6bwo
categoryprogramming
json_metadata{"app":"steemit\/0.2"}
created2021-06-23 21:33:57
last_update2021-06-23 21:33:57
depth3
children0
net_rshares0
last_payout2021-06-30 21:33:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length12
author_reputation14,491,425,587,942,198
root_title"Recursive Factorial Function in BASH Programming"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000