bc - loop

We have been programming slowly and now we are more confident than before. We can now change the value of a variable, check for a particular condition, we now know how to manage a large formula in the form of a function and save it in the file to use it in the future. Now we will go a little further on our journey. Now we will learn how we can run the same calculations repeatedly. For example, if you are given a task to calculate the sum of all the numbers from 1 to 100, then you have to do the same thing 100 times, which is to add the next number to the previous sum. Which is very boring and error-prone. I assume that we can not use any formula for some unknown reason, so we have to add all the numbers one by one. 

Suppose you were told to get the sum of all the numbers from 1 to 1000, then? 

Or suppose you need the sum of all the numbers from 1 to 10000 !!! 
You think the calculator will help. Well, you might not have to add the numbers, but you have to input all the numbers. 
Impossible ??? 

Here is where programming helps you, you write a small program and run with bc to get the result within a few seconds. Let's see how -

1
2
3
4
5
6
7
8
/* This function will sum all numbers from 1 to n */
define sum_numbers(n) {
result = 0;
for(i = 1; i < n; i++) {
result = result + i;
}
return result;
}

We have saved this program to a file and will run it with bc. I will now explain each line one by one.
  • In line number 1, we wrote a comment, which implies what the function is doing. Comment does not run in bc, so you can run whatever you want in a comment. Comment is a good programming practice, so that another programmer or you yourself read the program later and understand why the program was written.
  • We are already familiar with line number 2, so nothing to say there
  • In line 3, we provide the value 0 to the variable named result. This is also part of a programming, where we assign values ​​to a variable, in the language of programming it is called assignment.
  • In the 4th line we have written a loop, we have used the for loop. There are 3 parts of a for loop. Assignment, condition, update, i = 1 assignment in our loop, i < n condition  implies that as long as the value of i is less than n, the loop will run and finally we increase the value of i to 1 after running the loop every time, which is the update .
  • At line number 5, we are adding the value of current i to the previous sum, as we run the loop every time. Think on what will be the final sum.
  • Ending the loop on line number 6
  • We also have a familiarity with the 7th line, so there is nothing to say about it. 
Now let's run the program and see what happens -

totan@Home-Computer ~/bc $ bc -l sum_numbers.bc 
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.

sum_numbers(1)
1

sum_numbers(2)
3

sum_numbers(3)
6

sum_numbers(4)
10

sum_numbers(5)
15

sum_numbers(6)
21

sum_numbers(7)
28

sum_numbers(9)
45

sum_numbers(10)
55

sum_numbers(100)
5050

sum_numbers(1000)
500500

sum_numbers(10000)
50005000

sum_numbers(100000)
5000050000

sum_numbers(1000000)
500000500000

Another way we can write a loop is by using the while keyword. Let's see how we do it -

 1
2
3
4
5
6
7
8
9
10
/* This function will sum all numbers from 1 to n */
define sum_numbers(n) {
result = 0;
i = 0;
while(i <= n) {
result = result + i;
i = i + 1;
}
return result;
}

for and while works the same way and there are similar sections of assignment (line 4), condition (line 5), update (line 7) but all are in different places, unlike for loop where all these three sections come in a single line. 

Now we will finish the mini-series related bc by writing a program with loop for factorial-
define factorial ( n )  { 
result = 1;
for ( i = 1; i < = n; i ++ ) {
result = result * i;
}
return result;
}

I will not give an analysis of this program, I leave it on you to understand this program. Let's now calculate factorial of some numbers with this-

totan@Home-Computer ~/bc $ bc factorial.bc 
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
factorial(5)
120

factorial(6)
720

factorial(4)
24

factorial()
Runtime error (func=(main), adr=3): Parameter number mismatch

factorial(3)
6

factorial(2)
2

factorial(1)
1

factorial(0)
1

factorial(-1)
1

factorial(10)
3628800

factorial(11)
39916800

factorial(12)
479001600

factorial(20)
2432902008176640000

factorial(25)
15511210043330985984000000

factorial(30)
265252859812191058636308480000000

factorial(40)
815915283247897734345611269596115894272000000000

factorial(50)
30414093201713378043612608166064768844377641568960512000000000000

factorial(55)
12696403353658275925965100847566516959580321051449436762275840000000\
000000

factorial(70)
11978571669969891796072783721689098736458938142546425857555362864628\
009582789845319680000000000000000

factorial(90)
14857159644817614973095227336208257378855699612846887669422168637049\
85393094065876545992131370884059645617234469978112000000000000000000\
000

factorial(100)
93326215443944152681699238856266700490715968264381621468592963895217\
59999322991560894146397615651828625369792082722375825118521091686400\
0000000000000000000000

factorial(120)
66895029134491270575881180540903725867527463331380298102956713523016\
33557244962989366874165271984981308157637893214090552534408589408121\
859898481114389650005964960521256960000000000000000000000000000

factorial(150)
57133839564458545904789328652610540031895535786011264182548375833179\
82912484539839312657448867531114537710787874685420416266625019868450\
44663559491959220665749425920957357789293253572904449624724054167907\
22118445437122269675520000000000000000000000000000000000000

factorial(180)
20089606249913429965695133689846683891754034079886777794043533516004\
48609533959809411801381120973097356315941010373996096710321321863314\
95273609598531966730972945653558819806475064353856858157445040809209\
56035846331964466489111425643001782414179675381819233864230269332781\
8731986039603200000000000000000000000000000000000000000000

factorial(200)
78865786736479050355236321393218506229513597768717326329474253324435\
94499634033429203042840119846239041772121389196388302576427902426371\
05061926624952829931113462857270763317237396988943922445621451664240\
25403329186413122742829485327752424240757390324032125740557956866022\
60319041703240623517008587961789222227896237038973747200000000000000\
00000000000000000000000000000000000

factorial(250)
32328562609091077323208145520243684709948437176737806667479424271128\
23747555111209488817915371028199450928507353189432926730931712808990\
82279103027907128192167652724018926473321804118626100683292536513367\
89390895699357135301750405131787600772479330654023390061648255522488\
19436572586057399222641254832982204849137721776650641276858807153128\
97877767295191399084437747870258917297325515028324178732065818848206\
24785826598088488255488000000000000000000000000000000000000000000000\
00000000000000000

factorial(300)
30605751221644063603537046129726862938858880417357699941677674125947\
65331767168674655152914224775733499391478887017263688642639077590031\
54226842927906974559841225476930271954604008012215776252176854255965\
35690350678872526432189626429936520457644883038890975394348962543605\
32259807765212708224376394491201286786753683057122936819436499564604\
98166450227716500185176546469340112226034729724066333258583506870150\
16979416885035375213755491028912640715715483028228493795263658014523\
52331569364822334367992545940952768206080622328123873838808170496000\
00000000000000000000000000000000000000000000000000000000000000000000\
000

factorial(400)
64034522846623895262347970319503005850702583026002959458684445942802\
39716918683143627847864746326467629435057503585681084829816288351743\
52289619886468029979373416541508381624264619423523070462443250151144\
48670890662773914918117331955996440709549671345290477020322434911210\
79759328079510154537266725162787789000934976376571032635033153396534\
98683868313393520243737881577867915063118587026182701698197400629830\
25308591298346162272304558339520759611505302236086810433297255194852\
67443223243866994842240423259980555161063594237696139923191713406385\
89965379701478272066063202173794720103213566246138090779423045973606\
99567595836096158715129913822286578579549361617654480453222007825818\
40084843641559122945427538480355837451802267590006139956014559520612\
72111929181050324910080000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000

We have seen some interesting things in this series that will help us in our mathematical work, and we also knew how to do some simple programming. I will leave it here, but you can do some more with bc. If you want to know more, you can run man bc to know more about it and continue with
bc.

No comments:

Post a Comment