.

Labels

.
Showing posts with label math tools. Show all posts
Showing posts with label math tools. Show all posts

Trignometry calculator

Calculate values of Sine(sin), Cosine(cos), Tangent(tan), Cotangent(cot), Secant(sec), Cosecant(csc), Arc Sine(asin), Arc Cosine(acos), Arc Tangent(atan), Arc Cotangent(acot), Arc Secant(asec), Arc Cosecant(acsc).

To put everything in separate script will need creation of too many files .Hence best thing to do is make them functions and place it in your '.bashrc' file in your home folder.Copy the entire codes below  (either radian or degree according to your need) and paste it in your .bashrc file.The codes below uses command line tool bc. Hence make sure that you have installed bc.


RADIANS

-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------


#trignometry
sin ()
{
    echo "scale=5;s($1)" | bc -l
}

cos ()
{
    echo "scale=5;c($1)" | bc -l
}

tan ()
{
    echo "scale=5;s($1)/c($1)" | bc -l
}

csc ()
{
    echo "scale=5;1/s($1)" | bc -l
}

sec ()
{
    echo "scale=5;1/c($1)" | bc -l
}

ctn ()
{
    echo "scale=5;c($1)/s($1)" | bc -l
}

asin ()
{
    if (( $(echo "$1 == 1" | bc -l) ));then
       echo "90"   
    elif (( $(echo "$1 < 1" | bc -l) ));then
       echo "scale=3;a(sqrt((1/(1-($1^2)))-1))" | bc -l
    elif (( $(echo "$1 > 1" | bc -l) ));then
       echo "error"
    fi
}

acos ()
{
    if (( $(echo "$1 == 0" | bc -l) ));then
       echo "90"
    elif (( $(echo "$1 <= 1" | bc -l) ));then
       echo "scale=3;a(sqrt((1/($1^2))-1))" | bc -l
    elif (( $(echo "$1 > 1" | bc -l) ));then
       echo "error"
    fi
}

atan ()
{
    echo "scale=3;a($1)" | bc -l
}

acot ()
{
    echo "scale=5;a(1/$1)" | bc -l
}

asec ()
{
    echo "scale=5;a(sqrt(($1^2)-1))" | bc -l
}

acsc ()
{
    echo "scale=5;a(1/(sqrt($1^2)-1))" | bc -l
}




-------------------------------------------------------------------------------------------------------------------------------------------------



DEGREES

-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#trignometry
sin ()
{
    echo "scale=5;s($1*0.017453293)" | bc -l
}

cos ()
{
    echo "scale=5;c($1*0.017453293)" | bc -l
}

tan ()
{
    echo "scale=5;s($1*0.017453293)/c($1*0.017453293)" | bc -l
}

csc ()
{
    echo "scale=5;1/s($1*0.017453293)" | bc -l
}

sec ()
{
    echo "scale=5;1/c($1*0.017453293)" | bc -l
}

ctn ()
{
    echo "scale=5;c($1*0.017453293)/s($1*0.017453293)" | bc -l
}

asin ()
{
    if (( $(echo "$1 == 1" | bc -l) ));then
       echo "90"   
    elif (( $(echo "$1 < 1" | bc -l) ));then
       echo "scale=3;a(sqrt((1/(1-($1^2)))-1))/0.017453293" | bc -l
    elif (( $(echo "$1 > 1" | bc -l) ));then
       echo "error"
    fi
}

acos ()
{
    if (( $(echo "$1 == 0" | bc -l) ));then
       echo "90"
    elif (( $(echo "$1 <= 1" | bc -l) ));then
       echo "scale=3;a(sqrt((1/($1^2))-1))/0.017453293" | bc -l
    elif (( $(echo "$1 > 1" | bc -l) ));then
       echo "error"
    fi
}

atan ()
{
    echo "scale=3;a($1)/0.017453293" | bc -l
}

acot ()
{
    echo "scale=5;a(1/$1)/0.017453293" | bc -l
}

asec ()
{
    echo "scale=5;a(sqrt(($1^2)-1))/0.017453293" | bc -l
}

acsc ()
{
    echo "scale=5;a(1/(sqrt($1^2)-1))/0.017453293" | bc -l
}



-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

After saving it. Run ". .bashrc" (without quotes) in your terminal.

To find value of Sine of 45 , run sin 45 in terminal. Similarly for
Cosine ..              cos 45
Tangent  ..           tan 45
Cotangent ..         cot 45  
Secant ..               sec 45
Cosecant ..           csc 45
Arc Sine ..           asin  .73
Arc Cosine ..       acos  .73
Arc Tanget ..       atan  .75
Arc Cotangent ..  acot  .73
Arc Secant ..       asec 45
Arc Cosecant ..   acsc 45


Enjoy Linux !!

convert Centigrade ,Fahrenheit and Kelvin

Very useful script to convert Centigrade, Fahrenheit and Kelvin ;all three scales for measuring temperature.

Centigrade scale has water freezing at 0 c and boil at 100 c
Fahrenheit scale has water freezing at  32 f and boil at 212 f
Kelvin scale has absolute absence of heat  at 0 k , freezing of water at 273.15 k and boiling at 373.15 k

-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
This script uses following formulas
Converting Centigrade(C) to Fahrenheit(F) :
     F=(C * ( 9/5) )+ 32 
Converting Fahrenheit(F) to Centigrade(C) :
    C=(F-32)*(5/9)
Converting Centigrade(C) to Kelvin(K) :
    K=C+275.15
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#to convert centigrade, fahrenheit and kelvin

#checking if any temperature scale is specified
c=$(echo $1 | grep -c "c")
f=$(echo $1 | grep -c "f")
k=$(echo $1 | grep -c "k")


#doing calculations
if (( $c == "1" ));then
   c=$(echo $1 | sed 's/c//g');
   f=$(echo "scale=1;($c*(9/5))+32" | bc -l)
   k=$(echo "scale=2;$c+273.15"|bc -l)
   echo -e "\n\e[1;30m=$f f & $k k\e[0m\n";
elif (( $f == "1" ));then
   f=$(echo $1 | sed 's/f//g');
   c=$(echo "scale=1;(($f-32)*5)/9" | bc -l)
   k=$(echo "scale=2;$c+273.15"|bc -l)
   echo -e "\n\e[1;30m=$c c & $k k\e[0m\n"
elif (( $k == "1" ));then
   k=$(echo $1 | sed 's/k//g');
   c=$(echo "scale=1;$k-273.15" | bc -l)
   f=$(echo "scale=1;($c*(9/5))+32" | bc -l)
   echo -e "\n\e[1;30m=$c c & $f f\e[0m\n";
else
   c=$(echo "scale=1;($1-32)*(5/9)" | bc -l)
   f=$(echo "scale=1;($1*(9/5))+32" | bc -l)
   echo -e "\n\e[1;30m=$c c  & $f f\e[0m\n"
fi





-------------------------------------------------------------------------------------------

Script is named temp in my computer.If you want to convert 40 degree centigrade to other scale type temp 40c in terminal and press enter ..





For converting Fahrenheit or kelvin scale type temp 45f and temp 45k respectively. If no scale is specified script will show the given value converted  from centigrade to Fahrenheit and form Fahrenheit to centigrade.


Enjoy Linux !!

Don't know what to do with these codes ?? click here


Test your mental math skill

How good is your mental maths? Well whether it is good or bad , this script will test your ability and train you in mental calcualtion.

-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
In this script randomly selected numbers and mathematical operators create a problem which is piped through bc and the solution of the problem is compared with input answer from the keyboard.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
#!/bin/bash

clear
questions=10       #the number of questions to be asked,change it to your desire.
X[1]="+"
X[2]="-"
X[3]="*"
X[4]="/"
timeout=0
wrong=0
mark=0
for (( i=1; i<=$questions; i++ ));do 
select=$[ ( $RANDOM % 2) + 1 ]

#selecting random numbers for calculation.Limit is set to numbers 25 and less than 25.Change 25 to any number of your choice if needed.
a=$[ ( $RANDOM % 25 ) + 1 ]
b=$[ ( $RANDOM % 25 ) + 1 ]
c=$[ ( $RANDOM % 25 ) + 1 ]
#random selection of mathematical operations.
w==$[ ( $RANDOM % 4 ) + 1 ]
x==$[ ( $RANDOM % 4 ) + 1 ]

     if [[ $select == 1 ]];then
           math="$a${X[$w]}$b"
           echo "$math"
           read -t 10 solution
           if [[ $solution == $null ]];then
               timeout=$((timeout + 1))
               echo -e "\e[1;31mtime out!!\e[0m    Ans: $(echo "scale=1; $math" | bc )\n------------"
               echo -e "\n"
           elif [[ $solution = $(echo "scale=1; $math" | bc | sed 's/\.0$//') ]];then
               mark=$((mark+1))
               echo -e "\e[1;32m  \e[0m\n------------"
           else
               echo -e "\e[1;34m  \e[0m    Ans: $(echo "scale=1; $math" | bc )\n------------"
           fi                  
     else 
           math="$a${X[$w]}$b${X[$x]}$c"
           echo "$math"
           read -t 10 solution
           if [[ $solution == $null ]];then
               timeout=$((timeout + 1))
               echo -e "\e[1;31mtime out!!\e[0m    Ans: $(echo "scale=1; $math" | bc )\n------------"
               echo -e "\n"
           elif [[ $solution = $(echo "scale=1; $math" | bc | sed 's/\.0$//') ]];then
               mark=$((mark+1))
               echo -e "\e[1;32m  \e[0m\n------------"
           else
               echo -e "\e[1;34m  \e[0m    Ans: $(echo "scale=1; $math" | bc )\n------------"
           fi               
      fi
done

#calculation of your score
wrong=$(($questions-($mark + $timeout)))

echo -e "\e[1;31mscore= \e[1;34m$mark/$questions\e[0m    wrong=$wrong    timeout=$timeout"
            


-------------------------------------------------------------------------------------------

Execute the script and it will ask 10 mathematical problems. You will get 10 seconds to answer each problem and after completing the tenth question, it will display your score.




Now, that score is not bad! 

Please note:
when doing calculation you must do division first,then multiplication , then addition and finally subtraction(  division  multiplication  addition  subtraction ) ; else your answer can get wrong.

Enjoy Linux!!

Dont know what to do with these codes ?? click here

Find factors of a number (factorization)

Linux has got a beautiful factor finding tool called factor. This script does the same job of factor tool.

In mathematics, factorization (also factorisation in British English) or factoring is the decomposition of an object (for example, a number, a polynomial, or a mtrix) into a product of other objects, or factors, which when multiplied together give the original. For example, the number 15 factors into primes as 3 × 5, and the polynomial x2 − 4 factors as (x − 2)(x+ 2). In all cases, a product of simpler objects is obtained.
                                                                   -Wikipedia ( http://en.wikipedia.org/wiki/Factorization )


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
The number provided is divided by natural numbers starting from 2. Each natural number recursively divides the number while reminder for each division operation is zero.If reminder is not zero , the script starts dividing with the next natural number.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#to find factors of a number
num=$1
for (( i=2; i<=$1; i++ ));do
    while [ $((num%$i)) == 0 ];do
        echo $i
        num=$((num/$i))
    done
done


-------------------------------------------------------------------------------------------


Script is called myfactor in my system. Figure shows finding factors with factor tool and then with myfactor script of 15, 40 and 13



.. are the results same ?? :)

Enjoy Linux !!

Don't know what to do with these codes ??  click here

Fibonacci numbers

Script to display Fibonacci series.

In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence:
0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\; \ldots\; (sequence A000045 in OEIS).
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
F_n = F_{n-1} + F_{n-2},\!\,
with seed values
F_0 = 0,\; F_1 = 1.

                                           -Wikipedia ( http://en.wikipedia.org/wiki/Fibonacci_number )



note: If you want only 5 rows of Fibonacci series , this for loop will do
--------------------------------------------------------
for (( i=0; i<5; i++ ));do
echo $((11**$i))
done
--------------------------------------------------------


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
Proper commends are provided in the script itself to make it understandable.
'tput' is command line tool to change the postion of the prompt.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
#!/bin/bash

#for displaying fibonacci numbers

num=1
zero[1]=1
zero[2]=1

read -p "Please enter number of rows:"
#for exiting script if 0 or no number is entered
if [[ $REPLY == 0 || $REPLY == $null ]];then
exit 0
fi

tput cuf $(($(tput cols)/2))
echo "1"
for (( i=1; i<$REPLY; i++ ));do
   
    for (( k=1; k<=$i; k++ ));do
#for getting each number for addition and assigning to array 'a'         
         a[$k]=$(echo "$num%(10^${zero[$k]})" | bc)
         num=$(echo "$num/(10^${zero[$k]})" | bc)
    done

#adding adjascent numbers assigned to array 'a'   
    for (( j=0; j<=$i; j++ ));do
        l=$((j+1))
        sum=$((a[j]+a[l]))
        wordcont=$(echo $sum | wc -c)
        zero[l]=$((wordcont-1))
        totalsum="$totalsum $sum"
    done
    num=$(echo $totalsum | sed 's/ //g')

#finding position of terminal prompt and printing value
    length=$(echo $totalsum | wc -c)
    postion=$((($(tput cols)-$length)/2))
    tput cuf $postion 2>/dev/null
    echo "$totalsum"
    totalsum=$null
done


-------------------------------------------------------------------------------------------


In my system the script is named fibonacci. If you want ,say 15 rows , of fibanocci series ; type fibonacci and press Enter . When script ask you for number of rows to be displayed , enter 15 and press Enter  and ...




.. Howzzat ?? 



Enjoy Linux !!

Dont know what to do with these codes ?? click here

Finding prime numbers

script to display prime numbers between zero and given number

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime, as only 1 and 5 divide it, whereas 6 is composite, since it has the divisors 2 and 3 in addition to 1 and 6. The fundamental theorem of arithmetic establishes the central role of primes in number theory : any integer greater than 1 can be expressed as a product of primes that is unique up to ordering. This theorem requires excluding 1 as a prime.
                                            Wikipedia ( http://en.wikipedia.org/wiki/Prime_number )




-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
'factor' is a linux command line tool which will display factors of any number.
A prime number will have only 1 and the number itself as it's factors.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#for displaying prime number between zero and given number

for (( i=2; i<=$1; i++ ));do
l=$(factor $i | cut -d : -f 1)
r=$(factor $i | cut -d : -f 2 | sed 's/ //g')
   if (( $l == $r ));then
       echo $i
   fi
done 

-------------------------------------------------------------------------------------------


In my computer the script is named prime. If you want to find prime numbers between 0 and 25, then type prime 25 in terminal and press enter ...







Scripts to check whether a given number is prime or not.
-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
 This script uses 'The Prime Number Trick' given here http://www.murderousmaths.co.uk/games/primcal.htm
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------
#!/bin/bash

#for checking whether given number is prime or not

check=$(((($1**2)+17)%12))
if [[ $check -eq 6 ]];then
   echo "Yes"
else
   echo "No"
fi

-------------------------------------------------------------------------------------------




-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
 This script uses 'The 24 Mystery Method' given here http://www.murderousmaths.co.uk/games/primcal.htm
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------
#!/bin/bash

#for checking whether given number is prime or not

check=$(((($1**2)-1)%24))
if [[ $check -eq 0 ]];then
   echo "Yes"
else
   echo "No"
fi


-------------------------------------------------------------------------------------------
Both scripts usage same as the first script


... So is 149 a prime number ??   ; )

Enjoy Linux !!


Don't know what to do with the codes ?? click here




Convert decimal to fraction


Script to convert a decimal number to its fractional form

1.
-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
In this script the decimal number is converted to fractional form by multiplyng and dividing it with necessary multipe of 10, for example 23.48 is converted to 2348/100 . Then HCF of 2348 and 100 is calculatted and both numerator and denominator are divided by HCF to get the result.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#to convert the given decimal number to fraction
#for converting decimal to fraction

#converting to fractional form

num=$(echo "$1" | cut -d . -f 1)
den=$(echo "$1" | cut -d . -f 2)
num="$num$den"
zero=$(echo $den | wc -c)
den=$((10**(zero-1)))
#checking if denominator or numerator is greater
if [[ $num -gt $den ]];then
   greater=$num
   lower=$den
else
   greater=$den
   lower=$num
fi
#finding hcf
while [ $lower -ne 0 ];do
    hcf=$lower   
    lower=$((greater%lower))
    greater=$hcf
done
#dividing numerator and denominator by hcf
num=$((num/hcf))
den=$((den/hcf))
#answer
echo "= $num/$den"


-------------------------------------------------------------------------------------------
In my case the script is named fraction. To find fractional form of any decimal number , say 6.24 , type fraction 6.24 and just press enter ...

..abra-ca-dabra..










   things I noticed about this script : 
* even with too many digits after decimal place , instant result is obtained.




2.
-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
In this script the decimal number is multiplied by natural number from 2 onwards and checked whether an integer is obtained.The fractional form of the decimal number will be integer divided by natural number (which when multiplied with the decimal number made it an integer).
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#to convert the given decimal number to fraction

for (( i=1; i>0; i++ ));do
     check=$(echo $(echo "$1*$i" | bc | cut -d . -f 2)*1 | bc)
    if [[ $check == 0 ]];then
        echo -e "=\e[1;34m$(echo "$1*$i" | bc | cut -d . -f 1)\e[0m/\e[1;34m$i\e[0m"
        exit 0
    fi
done


-------------------------------------------------------------------------------------------


   things I noticed about this script : 
* with too many digit after decimal place , result takes very long time.

Enjoy Linux !!


Don't know what to do with these codes ??  click here