• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Example Bash Script For Arithmetic Math Calculation with Input Value

By Abhishek Ghosh January 25, 2015 6:58 pm Updated on January 25, 2015

Example Bash Script For Arithmetic Math Calculation with Input Value

Advertisement

Here is an Example Bash Script For Arithmetic Math Calculation with Input Value. With Simple Logical Sets, in Bash, we do run complex stuffs. This was made for answering a Quora’s question. May be, someone looking for learning bash scripting. We wrote about writing bash script before and about unix shell.

 

Example Bash Script For Arithmetic Math Calculation with Input Value : The Problem

 

Typically on various social networks, you’ll see that, people posts diagram of Pyramids made of balls. Usually they have four layers. Balls can be arranged in two ways – hollow inside and full solid. For per layer count of balls except the top one (base layer’s number will be 04 if it is a 4 layered pyramid) as for solid, will be :

Vim
1
2
3
4
5
6
Base = (4 * 2 ) + ( 2 * 2 ) = 12
Layer 1 = (3 * 2) + ( 1 * 2 ) = 8
Layer 2 = ( 2 * 2 ) + (0 * 2 ) = 4
Layer 3 = constant = 1
------------------------------------
Total  = 12 + 8 + 4 +1 = 25

Bash Script For Arithmetic Math Calculation with Input Value

So the Formula is :

Advertisement

---

Vim
1
[ {number of the layer from top x 2} + {(number of the layer from top - 2) x 2} ]

Total = Sum of Formula’s output for all layers except the top + 1 for the top

If it is not hollow :

Vim
1
[ { number of the layer from top x 2} + {(number of the layer from top - 2) x 2} + {(number of the layer from top -2) x 2} ]

Calculation is same for both the ways.

Balls required for constructing the Layer 4 (base) for a solid one = [ {4 x 2} + {(4 – 2) x 2} + {(4 -2) x 2} ] = 8 + 4 + 4 = 16. Balls required for constructing the Layer 3 for a solid one = [ {3 x 2} + {(3 – 2) x 2} + {(3 -2) x 2} ] = 6 + 2 + 2 = 10. Balls required for constructing the Layer 2 for a solid one = [ {2 x 2} + {(2 – 2) x 2} + {(2 -2) x 2} ] = 4 + 0 + 0 = 4. Total = 16+10+4 + 1 = 31

Sir Newton wrote the thing. I have not discovered. It is related to structural stability. You can not keep it, if there is no gravity, you can not keep with magnet bars inside with opposite poles facing in the same place.

This is an example where an input value is needed and it should be passed and the calculation will be done. With non-math things, we can use [Y], [N] and catch both Y, y and N, n set to pass via if do it, else exit logic.

 

Example Bash Script For Arithmetic Math Calculation with Input Value

 

For the solid’s layer 4, we can write in this way :

Vim
1
$(((4 * 2)+((4 - 2)*2)+((4 - 2)*2)))

If you want to directly execute it on command line, you should copy paste this :

Vim
1
echo "$(((4 * 2)+((4 - 2)*2)+((4 - 2)*2)))"

In this case, we have two equations, so we can write in this way :

Vim
1
2
3
4
5
6
#!/bin/bash
echo "What is the number of Layer counted from the Top?:"
read input
echo "Then the $input th layer will have ::"
echo "$(((input * 2)+((input - 2)*2)+((input - 2)*2))) balls, if it is solid,"
echo "and $(((input*2)+((input - 2)*2))) balls, if it is hollow!"

$input th layer means, it will read like “7 th layer”. But the digits need a bit color :

Vim
1
2
3
4
5
6
#!/bin/bash
echo "What is the number of Layer counted from the Top?:"
read input
echo "Then the $input th layer will have ::"
echo "\x1B[36m$(((input * 2)+((input - 2)*2)+((input - 2)*2)))\x1B[0m balls, if it is solid,"
echo "and \x1B[36m$(((input*2)+((input - 2)*2)))\x1B[0m balls, if it is hollow!"

You need to save it as .sh file and make it executable and then run it. I have ready to use solution :

Vim
1
2
3
wget https://gist.githubusercontent.com/AbhishekGhosh/847323ec7d35c678fa26/raw/de99d4ea7837df65703b6777d2a59981140509aa/pyramid-basic.sh
chmod +x pyramid-basic.sh
sh pyramid-basic.sh

The output has been shown in the screenshot.

Tagged With bash math , calculation in bash , bash calculation , math in bash script , bash math examples , doing math in bash , python math calculations in a bash script , bash echo calculation , bash arithmetic , bash math elevation
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Example Bash Script For Arithmetic Math Calculation with Input Value

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • Adding Help & Option in Bash Script

    Many New Hobbyist Programmers and Students Write Bash Scripts. Adding Help & Option in Bash Script is Easy. Here is Basic Guide With Example.

  • WordPress & PHP : Different AdSense Units on Mobile Devices

    Here is How To Serve Different AdSense Units on Mobile Devices on WordPress With PHP. WordPress Has Function Which Can Be Used In Free Way.

  • Changing Data With cURL for OpenStack Swift (HP Cloud CDN)

    Changing Data With cURL For Object is Quite Easy in OpenStack Swift. Here Are Examples With HP Cloud CDN To Make it Clear. Official Examples Are Bad.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Market Segmentation in BriefSeptember 20, 2023
  • What is Booting?September 18, 2023
  • What is ncurses?September 16, 2023
  • What is JTAG in Electronics?September 15, 2023
  • iPhone 15 Pro Max Vs Samsung Galaxy S22/S23 UltraSeptember 14, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy