Last active
August 29, 2024 22:01
-
-
Save yogeshlonkar/af72bbd4c32cffa69aa6d238e03aa586 to your computer and use it in GitHub Desktop.
Repeating sequence in a range in bash and terraform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -eou pipefail | |
| cat <<'EOF' | |
| This script demonstrates how repeating sequence in a range works | |
| Formula: | |
| (((N + 1) % n) ^ (((N + 1) / n) % 2)) * (N + 1) | |
| Bash: | |
| $(((((N + 1) % n) ** (((N + 1) / n) % 2)) * (N + 1))) | |
| Terraform: | |
| pow((($N + 1) % n), (floor(($N + 1) / n) % 2)) * ($N + 1) | |
| Where N is the current number and n max value in the range with zero as the minimum value. | |
| N can be initialized with -1 to start the sequence from 0. | |
| Special operators: | |
| ^ - power operator | |
| ** - power operator (for bash) | |
| % - modulo operator | |
| EOF | |
| n=${1:-11} | |
| N=${1:--1} | |
| _N=$N | |
| echo -e "\nSequence using bash: n=$n, N=$N" | |
| for _ in $(seq 0 $((n + 2))); do | |
| new_N=$(((((N + 1) % n) ** (((N + 1) / n) % 2)) * (N + 1))) | |
| if [ $new_N -eq 0 ]; then | |
| echo "N=$new_N # Resetting the sequence" | |
| else | |
| echo "N=$new_N" | |
| fi | |
| N=$new_N | |
| done | |
| N=$_N | |
| echo -e "\nSequence using terraform: n=$n, N=$N" | |
| for _ in $(seq 0 $((n + 2))); do | |
| new_N="$(echo "pow((($N + 1) % $n), (floor(($N + 1) / $n) % 2)) * ($N + 1)" | terraform console)" | |
| if [ "$new_N" -eq 0 ]; then | |
| echo "N=$new_N # Resetting the sequence" | |
| else | |
| echo "N=$new_N" | |
| fi | |
| N=$new_N | |
| done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This script demonstrates how repeating sequence in a range works | |
| Formula: | |
| (((N + 1) % n) ^ (((N + 1) / n) % 2)) * (N + 1) | |
| Bash: | |
| $(((((N + 1) % n) ** (((N + 1) / n) % 2)) * (N + 1))) | |
| Terraform: | |
| pow((($N + 1) % n), (floor(($N + 1) / n) % 2)) * ($N + 1) | |
| Where N is the current number and n max value in the range with zero as the minimum value. | |
| N can be initialized with -1 to start the sequence from 0. | |
| Special operators: | |
| ^ - power operator | |
| ** - power operator (for bash) | |
| % - modulo operator | |
| Sequence using bash: n=11, N=-1 | |
| N=0 # Resetting the sequence | |
| N=1 | |
| N=2 | |
| N=3 | |
| N=4 | |
| N=5 | |
| N=6 | |
| N=7 | |
| N=8 | |
| N=9 | |
| N=10 | |
| N=0 # Resetting the sequence | |
| N=1 | |
| N=2 | |
| Sequence using terraform: n=11, N=-1 | |
| N=0 # Resetting the sequence | |
| N=1 | |
| N=2 | |
| N=3 | |
| N=4 | |
| N=5 | |
| N=6 | |
| N=7 | |
| N=8 | |
| N=9 | |
| N=10 | |
| N=0 # Resetting the sequence | |
| N=1 | |
| N=2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment