Lets see how to generate a series i.e 0,1,2,...100,99.....1,0,1,2...... with an additional condition, that is, using only one variable.
First of all we must check the possibility of such an algorithm. Lets make an analogy (refer to figure below). A person runs from a pillar marked 0 towards another pillar marked 100, the distance between them is 100 meters. We take a variable called currentPosition to denote its position at any time. The value of currentPosition will change like 0,1,2,3,4......99,100. Now the person returns back to pillar marked 0. We can use the same variable to denote current position.
Here the direction of movement is different. But, we need not add another variable to denote direction. If the value of currentPosition is positive the person is moving from 0 to 100, if its negative the person is moving from 100 to 0. By analogy we can assume that it is possible to generate a series i.e 0,1,2,...100,99.....1,0,1,2...... using a single variable.
To provide solutions is not my aim. I want to enhance readers problem solving skills. So, try this algorithm yourself.
Also, try to generate a series i.e 0,1,2,...100,99.....1,0,1,2......199,200,199,198............2,1,0 using only one variable? Is that possible?
Sunday, June 26, 2011
Simple Algorithms - Swapping Values
We have three integer variables a,b,c. What are the steps to be followed to move value of a to b, b to c and c to a without using any extra variable?
To solve this we will try to find out a sub-problem first. This will make the task easier. A very simple problem is to swap values of two integer variables without using a third variable. Let a and b are two variables. The solution is:
swap(a,b) {
a=a+b;
b=a-b;
a=a-b;
}
The proof:
a =a+b; ----(1)
b=a-b=A(a+b) using (1) -b= a;---(2)
a=a-b= A(a+b) using (1)-B(a) using (2)=b;
Note: A(a+b) represents current value in variable a and so on.
Now if we have three variables a,b,c to move value of a to b, b to c and c to a we can follow following steps:
swap(a,b);
swap(a ,c);
So, what could be the algorithm if we have four integer variables i.e a, b, c, d ?
To solve this we will try to find out a sub-problem first. This will make the task easier. A very simple problem is to swap values of two integer variables without using a third variable. Let a and b are two variables. The solution is:
swap(a,b) {
a=a+b;
b=a-b;
a=a-b;
}
The proof:
a =a+b; ----(1)
b=a-b=A(a+b) using (1) -b= a;---(2)
a=a-b= A(a+b) using (1)-B(a) using (2)=b;
Note: A(a+b) represents current value in variable a and so on.
Now if we have three variables a,b,c to move value of a to b, b to c and c to a we can follow following steps:
swap(a,b);
swap(a ,c);
So, what could be the algorithm if we have four integer variables i.e a, b, c, d ?
Friday, June 24, 2011
FRIENDS Episode 3. Walk
Ashwani: Thoda udhar ho ke chal na....
Jaimal: Yarr..sari jagah to Mayank ne gher rakhi hai..Kahan jaun...
Next day Mayank on fruit diet.
Jaimal: Yarr..sari jagah to Mayank ne gher rakhi hai..Kahan jaun...
Next day Mayank on fruit diet.
Monday, June 13, 2011
FRIENDS Episode 2. Weighing Machine
Mayank: Yarr ye weighing machine tera weight 50 kilo bata rahi hai..
Jaimal: Kharab machine hai..kam bata rahi hai..Chal tera weight dekhte hai..
Mayank: Hmm.....
Jaimal: 100 kilo!!
Mayank: Sahi nahi hai yarr...jyada bata rahi hai..
Jaimal: Kam bata rahi hai...
Mayank: Nahi yarr...jyada...
Jaimal: Kharab machine hai..kam bata rahi hai..Chal tera weight dekhte hai..
Mayank: Hmm.....
Jaimal: 100 kilo!!
Mayank: Sahi nahi hai yarr...jyada bata rahi hai..
Jaimal: Kam bata rahi hai...
Mayank: Nahi yarr...jyada...
FRIENDS Episode 1 . Rain
Jaimal: Kaise jaun barish ho rahi hai...
Ashwani: Kya problem hai??..tu to barish ki bundon k beech m se nikal jayega.
Ram: Badal nishana le kar paani barsaen...tabh bhi kuch na ho....
Jaimal walked into the rain without any fear...
{ FRIENDS are always there to boost your moral }
Ashwani: Kya problem hai??..tu to barish ki bundon k beech m se nikal jayega.
Ram: Badal nishana le kar paani barsaen...tabh bhi kuch na ho....
Jaimal walked into the rain without any fear...
{ FRIENDS are always there to boost your moral }
Subscribe to:
Posts (Atom)