If you like this site and you like coffee, why not buy me one. Because I love it

Click to buy coffe
avatar
Untitled

Guest 99 7th Nov, 2022

BASH 2.76 KB
                                           
                         #!/bin/bash

# Set variables
index_number=$1
input_file=$2

# Set BC_LINE_LENGTH=0 to dissable line breaks in BC
# https://www.gnu.org/software/bc/manual/html_mono/bc.html#SEC23
export BC_LINE_LENGTH=0

# Pre-calculate π as variable so we can get better accuracy
# While commonly used to represent Pi, 22/7 is only accurate to 0.04025% of Pi. 
# PI=$(echo "scale=100; 22/7" | bc -l)
# echo $PI

# A more accurate fraction used to approximate Pi is 355/113, which is accurate to 0.00000849%.
# PI=$(echo "scale=100; 355/113" | bc -l)
# echo $PI

# But best way seems to be 4*a(1): this is one of the mathematical formulas used to calculate the number Pi.
#This is based on the Arc Tangent a() function.
# http://www.tux-planet.fr/calculer-le-chiffre-pi-en-ligne-de-commande-sous-linux/
scale=100
PI=$(echo "scale=$scale; 4*a(1)" | bc -l)
# echo $PI

# Define e 
E="2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664"


echo "Irrational search script!"
echo ""
# Check if parameters were supplioed
if ! [ $# -eq 2 ]; then
    echo "Bad amount of arguments supplied!"    
    echo "Usage: $0 INDEX_NUMBER INPUT_FILE"
    exit 1
elif ! [[ $index_number =~ ^[0-9]+$ ]] ; then
   echo "INDEX_NUMBER must be acctual number!"
   exit 2
elif ! [ -f "$input_file" ]; then
    echo "INPUT_FILE do not exist!"
    exit 3
fi

echo "Index '$index_number' was found '$(grep -o -i $index_number $input_file | wc -l)' times in '$input_file' file"
echo ""

echo "Looking for longest π($scale) string in '$input_file' file"!
echo "π ≈ $PI"
echo ""

# set intial index lenght
pi_lengh=1
while true; do
    string_of_pi=$(echo ${PI/./} | head -c $pi_lengh)
    matches=$(grep -o -i $string_of_pi $input_file | wc -l)

    echo -e "pi_lenght: $pi_lengh,\tstring_of_pi: $string_of_pi,\tmatches: $matches"
    
    if [[ $matches -eq 0 ]]; then 
        echo "No more matches!"
        break
    else
        ((pi_lengh++))
    fi
done

echo "Looking for longest e($scale) string in '$input_file' file"!
echo "e ≈ $PI"
echo ""

# set intial index lenght
e_lengh=1
while true; do
    string_of_e=$(echo ${E/./} | head -c $e_lengh)
    matches=$(grep -o -i $string_of_e $input_file | wc -l)

    echo -e "e_lenght: $e_lengh,\tstring_of_e: $string_of_e,\tmatches: $matches"
    
    if [[ $matches -eq 0 ]]; then 
        echo "No more matches!"
        break
    else
        ((e_lengh++))
    fi
done
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes
Ta strona używa plików cookie w celu usprawnienia i ułatwienia dostępu do serwisu oraz prowadzenia danych statystycznych. Dalsze korzystanie z tej witryny oznacza akceptację tego stanu rzeczy.
Wykorzystywanie plików Cookie
Jak wyłączyć cookies?
ROZUMIEM