Factorial of a Given Number
Recursive Definition of Factorial
Now let us discuss the problem finding factorial of a given number. So for this also we
can define a recursive function and even using iteration that is using loopholes so we
can market. Let us first of all devise a recursive function for this fund.
So first I will define what does it mean by a factor of a phone number. C factorial is
representative of the symbol.
So this means factored in love and the means one in boo boo in boo 3 in boo goes on
to. And so it means factorial of five this one in boo boo boo. Three into four and
multiplied by five.
Example: Factorial of 5
So this will be wondering if I want to play all these things I'll get down to 120. So for N I
have to continue up to n so 45 a till five or 10 and I should continue until 10.
So just like some of foster national numbers this is my duplication of Foster natural
numbers that is called as factorial.
And even I'll tell you one more thing that zero factorial is also possible and its answer is
1 so even 1 factorial is also 1 and 0 factorial is also 1.
So both the results are well known let us define this recursively if I say fact of our
number n is one input to input three goes on up to one more time I will include that this
one will number and minus 1 in n.
So in this if I observe factor of a number from 1 to 10 this portion if I dig this factory
factorial off and the minus 1 yes this can be the mass factor often minus 1.
So factorial of and as a factorial of and the minus one multiplied by n.
If we multiply that with and then they get Denison that's factor often so this can be
defined recursively factorial of any number n as factorial of the minus one multiplied by
and then N as a greater than zero then anything close to zero means for zero also
answer is one. So return 1
so does the recursive definition.
Recursive Function in C/C++
And once you have a recursive relation or a mathematical formula then Derek Luke can
convert that recursive formula and do a C C++ function. So let me write on a function for
this one integer factorial of n
if and as equals to zero. Red Dawn when you do zero return one return one else on this
one so every dawn factorial of any minus 1 in 2.
And so the function of writing a recursive function is so easy you can directly convert a
forum online to recursive function not same function even we can write using iteration.
That is a loop. Right. And this is using recursion.
Comparison with Sum of Natural Numbers
Now it is more similar to some of us to natural number if you remember in the previous
example we saw a similar function. Only the difference was here it was plus and one
more difference. Right on zero was dead and stuff one because of the multiplication is
there. So it should be wrong. So the function is similar.
Analysis of Recursive Factorial
No. If we do analysis and analysis will be same the size of the memory required
depends on the value that you're passing if you find functional 4 5 that is factorial of 5
and then even making 6 calls. So the height of the stack will be 6. So the amount of
memory consume as order of n and the time number of calls it depends on the value
that you're passing like 4 5 which is making 6 calls. So again it is order of atoms just in
the previous video for finding some of foster national and why we have done similar
analysis. So I want to avoid analysis here because already we have done analysis for
this fund.
So an answer the same writing a dirty function for this one is a student exercise so you
can write on the function by yourself.
So that's all about factorial of a given number.
Factorial Function – Code Demonstration
Recursive Function Implementation
In this video we will see a function for finding factor of a phone number. I will show you
recursive function as well as iterative function. So first let's write recursive function for
finding factorial function takes parameter n if n is equals to zero then return 1
ls return factorial of and minus 1. But and
inside main function. I will take a variable R to get the result of that factorial function. Let
us find the factorial of number 5. The answer is 120 we already know the answer. We
should get the same result.
Display. OK I will run the program. We should get it out put 120
Testing the Recursive Function
is the output is 120 is a factorial of 5
let us check factor in a 4. It should be 20 for
yes it is correct. Let us stick factory love 0 or the result will be it is 1.
Handling Negative Input
Let us use a negative number here and see what the function will do. Factory left minus
1
see when the value of an S minus 1. Then it is not zero. So it will be calling itself again
and again minus 1 minus 1 so it's minus 2 then minus 3 minus 4. Goes on.
So actually it has gone into infinite calling and terminated. So you can see total how
many calls it has done.
2 6 2 0 5 4 calls it has done. Then why did it stop. Because of stock or slow.
So if a recursive function is going and in finite recursive call so at one point it will
terminate because of stack overflow system stack has overflow and so it has made so
many calls factor it is not defined for negative number. So if you want to avoid this
recursive call so then here you can write a condition to stop the function. The number is
negative.
Iterative Version of Factorial
Now I will write a dirty version of factorial. I will call you guys a fact. It takes a parameter
and for finding factorial I will take if that is initialized at 1 and also we need a variable I
for iteration using for loop. I assign one on words and I less than an equal to N I plus
plus then factor it is multiplied by I.
So I will be taking different values from one to N and it will be getting multiplied by F
every time so this will be multiplying just like wanting to go in Q3 and so on. And then
finally it on F F is having represented and here inside the main function I will call this a
factorial function and I will pass the parameter as 5.
Let us run the program and see what the output will be it should give output 120
yes output is 120.
Debugging the Iterative Version
Let us debug the program quickly and see how the values of f and I are changing in this
iterative version
so can observe the swatch here. Was this portion where the values of f I will be
changing. I will continue execution it will enter into factorial function. Now the variables I
and F both are dead.
I use one so one multiplied by F F is also 1 then I becomes 2 multiplied by f f becomes
2 then I becomes 3 multiplied by f f become 6 then I use the 4 4D multiplied 24 I use 5 5
is multiplied 120 I become 6 then it will stop
and come back to main function and print the result. So off of program.
So that's all in this video we have seen recursive as well as iterative virginal factorial
function.
PHP Arrays
Introduction to PHP Arrays
So now let's talk about one of the most popular features of PHP, arrays. PHP arrays are
just plain awesome. They are inspired by Perl. They're called associative arrays, Perl
had associative arrays.
And what happens is, part of what programmers have to do is, you have to write
algorithms, which is code and steps that you write through and then the data. And so,
you solve problems with a combination of the logic that you use and the data that you
create.
Data Structures and Associative Arrays
Data structures are how you shape the data. And in a language like C, they had this
thing called structs, which are shaped data items. In C++, you have objects, which are
shaped data items. Java has an object which is a shaped data item, and a data item
has a shape. Those are all really complex ways, lots of syntax to say, this person has a
first name, and a last name, and a phone number.
And arrays, associative arrays, are key-value pairs. So you say, this person has a first
name, and a last name, and a phone number, and you're done. And so they're just a
way for programmers, without learning a bunch of extra syntax, to create data structures
when they hardly even know it.
And so that's what Python dictionaries are very popular, one of the most popular
features in Python. Java has things like HashMaps, Windows has things called Property
Bags. They are all a version of an associative array or a key-value pair.
PHP arrays are probably my absolute favorite. When I move from PHP to Python I'm
like, dang it, I want these things to stay in order. So PHP arrays are just my favorite
associated arrays.
Types of PHP Arrays
In PHP they can either be a linear list indexed by numbers. Or they can be key-value
pairs like first name Chuck, last name Severance, phone number blah. And they have
two-dimensional arrays but they're really just arrays within arrays of arrays basically.
We'll talk a little bit about that but not too much.
Linear Arrays
So you can make an array that is a linear list of things. So here we're going to make an
array, so this is sort of a constructor. It says, make me an array that has two things in it.
And like most civilized languages, the first element is in [0]. So we use the sub operator
or index operator or item operator. So we say, give me the [1], which is actually the
second one, so we print out There. So we make an array, and then we reference an
item within that array. Unless we tell it otherwise, it just puts these things in its 0 and 1.
Key-Value Arrays
The thing that people really love though, is when you do key-value, and so this is the
syntax. Let's make an array, and I read this as maps to.
So this key of name maps to the value of Chuck, so this is the key and the value. The
key, of course, maps to Web Applications for Everybody. And then you can use the
index operator, again, to go look up the piece under the key course, and out comes
Web Applications for Everybody.
I love this, I love it, I love it. Sorry, I'm a fan of key-value arrays, because, like I said, it
allows us to build data structures without thinking too much about it. Although later
when you do object-oriented in C, you don't want to get so complex with your data
structures using arrays, but that's okay.
Printing Arrays: print_r and var_dump
So there is a couple of ways to print these things out, because we start building shapes,
so I mean shapes of data and conventions. You are the one as a programmer, you
decide you call it name or course or whatever. But after a while you make these shapes
and this is a super simple one.
print_r
So you want a way to print this stuff out, print_r. And I'm going to put a pre tag out so
that the new lines don't get all messed up, because that's HTML for pre tag. So that I
see actually the way it prints out. And so it doesn't do line wrapping on all this stuff.
So print_r goes through this array and prints the key and the value, key value, key value
and goes through it. You'll notice that the order is the same, both when I put it in and
when I take it out. That's one thing that I like.
The r in print_r I think stands for recursive which means you have array within an array
within an array. It'll kind of go through that and it'll indent even further and further and
further, this is very simple. But you can print out much more complex structures, and
you do end up with very complex structures.
Sometimes you have a linear array with integer keys inside of a key-value array.
Especially when you start talking APIs and JSON and stuff like that, so you want a
print_r.
var_dump
Another thing that is even more verbose and detailed because it talks more about the
type is a thing called var_dump. var_dump, I think of it as lower level. It's less pretty but
it's more explicit. And what this says is this $stuff is a two item array. And the first item is
name, maps to a 5 character string of the value of Chuck. course is the first key that
maps to a 5 character string, SI664. So it's just a little more verbose.
And I tend to use var_dump when I'm really digging through something. because print_r
makes it prettier, but sometimes you lose some detail.
The one thing I love about var_dump is that you can actually print false. You can
actually print false, because var_dump is printing both the type and the value.
Printing FALSE – A Common Pitfall
So here I have FALSE is in $thing. I print out One, I use print_r and it's absolutely
nothing. And this is like aah.
I'm so mad because I look at this and I say the code didn't run. But it did, this code did
run. So when you print FALSE, it doesn't show up. Have I told you that before? Again,
that's cause I am really usually angry after 20 minutes of wasted time because I keep
printing stuff out. Now I can't see it. But then I realized I should have watched my own
lecture and use var_dump, because var_dump says it's a false and it's boolean.
There's a reason it does it this way, I'm sure, but it's not very convenient for debug
printing.
Constructing Arrays
Now you could construct an array a couple different ways. You can start with an empty
array and append stuff to the end. So this is make me an array. And then this is go to
the end and add one, an item. And then it just adds these in integer positions, so 0 and
1, so it's a linear list.
You can do the same thing with key values. You just put the index operator on the left
hand side of the assignment statement, make an array. Stick Chuck under the key
name, stick WA4E under the course Order. Smiley face. Stays in order.
Looping Through Arrays
foreach Loop
Okay, looping, so this is looping through a key-value array. So it's a looping construct
that has two iteration variables for each, syntactically different. The array is the first
parameter, and then as is a keyword. And then you can have two iteration variables,
one for the key and one for the value. And so I read this as foreach $stuff as key maps
to value. And that's what you put right there, it's not a less than or a greater than or
equal It's a map to I think of that as an arrow, right? And I think that's exactly what the
design of the language was.
So this loop is going to run and $k is going to go through the keys, $v is going to go
through the value. And then the next time through the loop they're going to both
simultaneously advance. And so I think this is really pretty syntax.
I think that's really nice syntax, and I like it. I like being able to loop through arrays. If
you have a linear array, you can get it to key in a value. In this case the Key is 0 and 1,
because that's how this works. This is the 0 position, that's the 1 position.
Counted Loop
You can also, on a linear array. You gotta be careful that it's a well formed linear array.
You can use a counted loop, right? So count is a function that tells you how many things
there are. So in this case that's going to be 2, because there's 2 things in there. So I'm
going to have a iteration variable for $i = 0, $i < count, so that means it's going to be 0
and 1. [2] wouldn't work because this is an array [0] and an array [1] there. So then I'm
going to add 1, so this is a counted loop that's going to run twice.
It's a little uglier syntax wise, for this particular thing I would have used the foreach
instead, but you can do counted loops. Sometimes you actually need a counted loop
because you need the variable $i for some reason or another. And so just then you're
just going to dereference $stuff[$i] which is the 0 and the 1, and then the loop prints all
the stuff out. So that's a counted loop that's going to iterate through an array.
Two Dimensional (Nested) Arrays
Just talk briefly about two dimensional arrays. They're really not two dimensional arrays,
they are nested arrays. They are recursively nested arrays so you can put an array
within an array, right?
And so, this is outer array, so this is the outer array and this outer array has three
things. The first one, the second one and the third one. And this paper maps to an
array. See the comma? The comma's part of the outer array. So there are three things
in the outer array.
Example: Accessing Nested Arrays
So you can think of this as $products["pens"]. $products["pens"] is going into this outer
array and grabbing this thing. And then, ["marker"] is within this array grabbing
["marker"] and looking up Markers, so that pulls out Markers.
So it's not really two dimensional, arrays, it's array within arrays. And you won't build the
structure often. And if you're going to get data from a database maybe, or you're going
to get data off the web or something, read some JSON and parse it, or something you'll
get this. And then you'll just have to figure out how to dig into it, how to go in and dig it,
dig through this whole thing.
So that's arrays within arrays, that is kind of like two dimensional arrays.
So up next, we're going to talk about how there's a set of functions that help us build
arrays and search them and do various things.