All About Technology! "

All About Technology!

All About Technology! A complete guide to latest technology and science.

Saturday, June 10, 2017

Java Recursion with examples

June 10, 2017 0

Recursion(JAVA)
Simply put, recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function. At first this may seem like a never ending loop, or like a dog chasing its tail. It can never catch it. So too it seems our method will never finish. This might be true is some cases, but in practise we can check to see if a certain condition is true and in that case exit (return from) our method. The case in which we end our recursion is called a base case . Additionally, just as in a loop, we must change some value and incremently advance closer to our base case.
Consider this function.
void myMethod( int counter)
{
if(counter == 0)
     return;
else
       {
       System.out.println(""+counter);
       myMethod(--counter);
       return;
       }
}
This recursion is not infinite, assuming the method is passed a positive integer value. What will the output be?
Consider this method:
void myMethod( int counter)
{
if(counter == 0)
     return;
else
       {
       System.out.println("hello" + counter);
       myMethod(--counter);
       System.out.println(""+counter);
       return;
       }
}
If the method is called with the value 4, what will the output be? Explain.
The above recursion is essentially a loop like a for loop or a while loop. When do we prefer recursion to an iterative loop? We use recursion when we can see that our problem can be reduced to a simpler problem that can be solved after further reduction.
Every recursion should have the following characteristics.
1.   A simple base case which we have a solution for and a return value.
2.   A way of getting our problem closer to the base case. I.e. a way to chop out part of the problem to get a somewhat simpler problem.
3.   A recursive call which passes the simpler problem back into the method.
The key to thinking recursively is to see the solution to the problem as a smaller version of the same problem. The key to solving recursive programming requirements is to imagine that your method does what its name says it does even before you have actually finish writing it. You must pretend the method does its job and then use it to solve the more complex cases. Here is how.
Identify the base case(s) and what the base case(s) do. A base case is the simplest possible problem (or case) your method could be passed. Return the correct value for the base case. Your recursive method will then be comprised of an if-else statement where the base case returns one value and the non-base case(s) recursively call(s) the same method with a smaller parameter or set of data. Thus you decompose your problem into two parts: (1) The simplest possible case which you can answer (and return for), and (2) all other more complex cases which you will solve by returning the result of a second calling of your method. This second calling of your method ( recursion ) will pass on the complex problem but reduced by one increment. This decomposition of the problem will actually be a complete, accurate solution for the problem for all cases other than the base case. Thus, the code of the method actually has the solution on the first recursion. 

Let's consider writing a method to find the factorial of an integer. For example 7! equals 7*6*5*4*3*2*1 . 

But we are also correct if we say 7! equals 7*6!.
In seeing the factorial of 7 in this second way we have gained a valuable insight. We now can see our problem in terms of a simpler version of our problem and we even know how to make our problem progressively more simple. We have also defined our problem in terms of itself. I.e. we defined 7! in terms of 6!. This is the essence of recursive problem solving. Now all we have left to do is decide what the base case is. What is the simplest factorial? 1!. 1! equals 1.
Let's write the factorial function recursively.
int myFactorial( int integer)
{
if( integer == 1)
     return 1;
else
       {
       return(integer*(myFactorial(integer-1);
       }
}
Note that the base case ( the factorial of 1 ) is solved and the return value is given. Now let us imagine that our method actually works. If it works we can use it to give the result of more complex cases. If our number is 7 we will simply return 7 * the result of factorial of 6. So we actaully have the exact answer for all cases in the top level recursion. Our problem is getting smaller on each recursive call because each time we call the method we give it a smaller number. Try running this program in your head with the number 2. Does it give the right value? If it works for 1 then it must work for two since 2 merely returns 2 * factorial of 1. Now will it work for 3? Well, 3 must return 3 * factorial of 2. Now since we know that factorial of 2 works, factorial of 3 also works. We can prove that 4 works in the same way, and so on and so on.
Food for thought: ask yourself, could this be written iteratively?
Note: make it your habit of writing the base case in the method as the first statement.
Note: Forgetting the base case leads to infinite recursion.
However, in fact, your code won't run forever like an infinite loop, instead, you will eventually run out of stack space (memory) and get a run-time error or exception called a stack overflow. There are several significant problems with recursion. Mostly it is hard (especially for inexperienced programmers) to think recursively, though many AI specialists claim that in reality recursion is closer to basic human thought processes than other programming methods (such as iteration). There also exists the problem of stack overflow when using some forms of recursion (head recursion.) The other main problem with recursion is that it can be slower to run than simple iteration. Then why use it? It seems that there is always an iterative solution to any problem that can be solved recursively. Is there a difference in computational complexity? No.
Is there a difference in the efficiency of execution? Yes, in fact, the recursive version is usually less efficient because of having to push and and pop recursions on and off the run-time stack, so iteration is quicker. On the other hand, you might notice that the recursive versions use fewer or no local variables.
So why use recursion? The answer to our question is predominantly because it is easier to code a recursive solution once one is able to identify that solution. The recursive code is usually smaller, more concise, more elegant, possibly even easier to understand, though that depends on ones thinking style. But also, there are some problems that are very difficult to solve without recursion. Those problems that require backtracking such as searching a maze for a path to an exit or tree based operations (which we will see in semester 2) are best solved recursively. There are also some interesting sorting algorithms that use recursion.
This problem comes from history, monks in Vietnam were asked to carry 64 gold disks from one tower (stack) to another. Each disk is of a different size. There are 3 stacks, a source stack, a destination stack and an intermediate stack. A disk is placed on one of three stacks but no disk can be placed on top of a smaller disk. The source tower holds 64 disks. How will the monks solve this problem? How long will it take them?
The easiest solution is a recursive one. The key to the solution is to notice that to move any disk, we must first move the smaller disks off of it, thus a recursive definition. Another way to look at it is this, if we had a method to move the top three disks to the middle position, we could put the biggest disk in its place. All we need to do is assume we have this method and then call it.
Lets start with 1 disk (our base case): Move 1 disk from start tower to destination tower and we are done.
To move 2 disks:
Move smaller disk from start tower to intermediate tower, move larger disk from start tower to final tower, move smaller disk from intermediate tower to final tower and we are done.
To move n disks (or think of, say, 3 disks):
Solve the problem for n - 1 disks (i.e. 2 disks) using the intermediate tower instead of the final tower (i.e. get 2 disks onto the intermediate tower). Then , move the biggest disk from start tower to final tower. Then again solve the problem for n - 1 disks but use the intermediate tower instead of the start tower (i.e. get the 2 disks onto the final tower using the start tower as the intermediate tower). 
Tail Recursion
Tail recursion is defined as occuring when the recursive call is at the end of the recursive instruction. This is not the case with my factorial solution above. It is useful to notice when ones algorithm uses tail recursion because in such a case, the algorithm can usually be rewritten to use iteration instead. In fact, the compiler will (or at least should) convert the recursive program into an iterative one. This eliminates the potential problem of stack overflow.
This is not the case with head recursion, or when the function calls itself recursively in different places like in the Towers of Hanoi solution. Of course, even in these cases we could also remove recursion by using our own stack and essentially simulating how recursion would work.
In my example of factorial above the compiler will have to call the recursive function before doing the multiplication because it has to resolve the (return) value of the function before it can complete the multiplication. So the order of execution will be "head" recursion, i.e. recursion occurs before other operations.
To convert this to tail recursion we need to get all the multiplication finished and resolved before recursively calling the function. We need to force the order of operation so that we are not waiting on multiplication before returning. If we do this the stack frame can be freed up.
The proper way to do a tail-recursive factorial is this:
int factorial(int number) {
    if(number == 0) {
           return 1;
        }
        factorial_i(number, 1);
}

int factorial_i(int currentNumber, int sum) {
    if(currentNumber == 1) {
        return sum;
    } else {
        return factorial_i(currentNumber - 1, sum*currentNumber);
    }
}
Notice that in the call return factorial_i(currentNumber - 1, sum*currentNumber); both parameters are immediately resolvable. We can compute what each parameter is without waiting for a recursive function call to return. This is not the case with the previous version of factorial. This streamlining enables the compiler to minimize stack use as explained above

Read More

Wednesday, February 15, 2017

Worlds most weird Discoveries

February 15, 2017 0

Most Weird But Wonderful Inventions

Ssg Penis -Drawing Robot
We have no idea why anybody would even think of this as a concept, let alone actually waste time and money actually making it reality, but there it is. Yes, a robot which draws a phallic symbol (incidentally one of the easiest things to draw which even a seven year old child could manage) and then writes the word ‘penis’ underneath it. Then, its inventor, David Neevel (a man with far too much money and time on his hands) will send it to you through the post. Strange… but quite funny in a very immature way.






Ssg Hairy Stockings For Young Girls 
Incredible. Better than a rape alarm in my book. They invented stockings which resemble hairy legs, basically material with realistic-looking hair attached to them, in order to make some girls look so unattractive that nobody would want to harass them. I suppose it could work in theory, but I’m not sure how fair it is that girls have to make themselves look ugly in order to avoid being hassled by neanderthals. And why stop there? Buck teeth, false fat stomachs, crooked nose pieces…







 Ssg Device To Control A Cockroach With A Smartphone
Very strange, and with seemingly no point to it at all, as well as being a tad disgusting and definitely not for the squeamish. You cut up the cockroach’s antenna, attach an electronic type of backpack to its back, connect to it using your mobile phone new app, then control it like a radio-controlled  car. A little bit unethical, weird, but actually has the potential to be quite good fun, especially if you use it to scare people.




  Ssg Urine -Powered Mobile Phones
This actually makes sense, although it’s still in the ‘developing’ stages at the moment. Scientists have worked out how to break the chemicals in our urine using modified bacteria, and they’re trying to work out how to use that microbial fuel to power up our mobile phones. Now how useful would that be! How we would ‘transfer’ that fuel into our phones is not so clear at the moment, and it could be a messy job. Environmentally-friendly phones, nice.






Ssg Push -Up Bra For Men 
Women have fooled men for years with those push up bras, and it’s time for equality to stare them back in the face. These are bras which give the distinct look of a honed and sculpted body, turning ‘man boobs’ into Adonis-like chiseled muscles, and saving you hours of effort in the gym, whilst helping you to fool drunken female victims and lure them back to your lair. Just make sure the lights are off when you get back home, and make sure she keeps her hands to herself.
- See more at: https://www.stopsleepgo.com/travel-blog/weird-and-wonderful/top-5-weird-but-wonderful-inventions.aspx#sthash.ZQcWjD5V.dpuf
Read More

Sunday, January 15, 2017

How to use a computer part 5

January 15, 2017 0

How to Use a Computer


You can do a lot of things with a computer. Though it seems very tough to using computer as a first time user. Now a days computers are built very simpler that you can find a computer running within a moments! If you are a beginner to computer don’t be upset just follow the steps bellow:

If you missed part one then read it from here How to use a computer part 1
If you missed part two then read it from here How to use a computer part 2
if you missed part three then you can find it here HOw to use a computer part 3
If you missed part four then read it from here How to use a computer part 4



    1.     Basic troubleshooting:

A computer can encounter problems as like as any other electronic devices. It will be very helpful for you to manage some of these problems by yourself and save your time and effort if you know the basic about how to troubleshoot a computer. You need not to be an expert to do this.
     ·        You can try to reset your computer at first when you faced a problem. Resetting of the PC can resolve a huge range of potential issues.

   2.     Know how to recognize a virus:

Viruses can cause a huge threat to the computer’s files and information also it can slow down your computer. Sometimes viruses can erase all the data that stored on your computer. Smart browsing of internet is enough to prevent most of the online viruses.

     ·     Viruses, Trojans, Spyware, Malware, adware can be destructive to your security system. These are often contains on another programs and installed with those programs.

   3.     Uninstalling troubled programs:

You can uninstall unnecessary and affected programs from your computer. Just go to the search bar and type “Control Panel” and then go to programs and features and select which programs to uninstall.


4.     
Reinstalling the OS:

If nothing can solve your problems, reinstalling the operating system may solve these types of problems.

   5.     Frequently clean the dust and junk:


Always keep your PC clean by using disk cleaner and defragmenter. You also need to keep clean your computer’s hardware to keep it cool because heat is one of the most common threats for hardware devices.
Read More

Wednesday, December 21, 2016

December 21, 2016 0
কম্পিউটারের মাউসের কাজ শুধু ডান, বাঁ ক্লিকেই শেষ নয়। মাউস দিয়ে এমন অনেক মজার আর দরকারি কাজ করা যায় যেটা অনেকেরই অজানা এমন কিছু কাজের উদাহরণ দেওয়া হলো:- 

  • শিফট কি আর ক্লিক
সম্পাদনার সব প্রোগ্রামই লেখার নির্দিষ্ট অংশ নির্বাচনের সুবিধা দিয়ে থাকে। আপনি যদি চান একটা পাতার লেখার শুধু মাঝখানের কয়েক লাইন নির্বাচন করবেন তবে সেই লেখার লাইনের শুরুতে একটা মাউসের বাম বোতাম চেপে কি-বোডের্র শিফট বোতাম চেপে ধরুন, যেখানে শেষ করবেন সেখানে গিয়ে মাউসে আরেকবার ক্লিক করলেই নির্দিষ্ট লেখাগুলো নির্বাচিত হয়ে যাবে এবং প্রয়োজনে সেটিকে কাট, কপি করতে পারবেন।

  • চাকার ব্যবহার

মাউসের চাকা বা স্ক্রল সাধারণত কোনো পৃষ্ঠাকে ওঠানামা করার কাজে ব্যবহৃত হয়ে থাকে। কিন্তু জানেন কি এই চাকা মাউসের তৃতীয় বোতাম হিসেবেও কাজ করে থাকে। ইন্টারনেট ব্যবহারের সময় কোনো ওয়েব ঠিকানার ওপর মাউসের কারসর রেখে স্ক্রল বোতাম চাপলে সেই সংযোগ আলাদা একটি ট্যাবে খুলে যাবে। কাঙ্ক্ষিত সাইটের কোনো পৃষ্ঠাকে বড় বা ছোট করে করে দেখতে চাইলে Ctrl কি চেপে মাউসের স্ক্রল উপরে-নিচে ঘোরালেই চলে। পাতাটি পর্যায়ক্রমে জুম-ইন, জুম-আউট হতে থাকবে

  • ক্লিক করে নির্বাচন

কোনো লেখার একটি শব্দের ওপর দুই ক্লিক করলে সেটা নির্বাচন করা যায়। যদি পুরো প্যারা নির্বাচন করতে চান, তাহলে তিন ক্লিক করতে হবে। অনেক সময় কম্পিউটারে কোনো কাজ সম্পন্ন করতে সতর্কতামূলক বার্তা আসে এবং Yes বা No নির্বাচন করতে বলে। ডিফল্ট অ্যাকশন যদি Yes হয়, তাহলে মাউসের Snap To অপশন ব্যবহার করলে স্বয়ংক্রিয়ভাবে মাউসের কারসর Yes বোতামের কাছে চলে যাবে। এতে করে মাউস না ঘুরিয়ে কাজটি সম্পন্ন করতে এক ক্লিকের প্রয়োজন হবে। জন্য কম্পিউটারের কন্ট্রোল প্যানেল থেকে Mouse-এর Properties- যান। এখানে Pointer Options ট্যাবের Snap To-এর পাশে টিক চিহ্ন দিয়ে Apply বোতামে ক্লিক করলে কাজটি হবে। এখন যেকোনো ডিফল্ট অ্যাকশনের জন্য মাউসের কারসর সেখানে স্বয়ংক্রিয়ভাবে চলে যাবে
_______________________________________________________________________
মোঃ ইব্রাহীম খলিল।
Mail: mdibrahimk48@gmail.com

Read More

Monday, December 19, 2016

How to use a computer part 5

December 19, 2016 0

How to Use a Computer



You can do a lot of things with a computer. Though it seems very tough to using computer as a first time user. Now a days computers are built very simpler that you can find a computer running within a moments! If you are a beginner to computer don’t be upset just follow the steps bellow:

If you missed part one then read it from here How to use a computer part 1
If you missed part two then read it from here How to use a computer part 2
if you missed part three then you can find it here HOw to use a computer part 3


Increasing functionality:


      1.   Setting up a new Printer- 
      

     now a days it is more easier to install a new printer on modern computers. You need to just plug the printer with the computer via USB cable to the USB ports and Operating System will do the rest.  Setting up a printer is essential for home, office or using a computer for business purposes like for a school because they all need to print instant.

      2.    Setting up a Home Network: 
      

     networks allow multiple computers to interact with each other and share documents, files and even Internet in a limited range. Connecting all computers and devices allows you to quick access to your devices and also you can request any file from other device. Setting up a network requires a router or hub or switch. They are same kind of hardware to connect all the devices with Ethernet or Wireless. 

      3.   Installing a webcam or microphone- 
        

     you need a webcam if you wish to video chat with family and friends using Skype, google+, facebook etc. microphone requires for the audio input into computer. Webcams and microphones generally installed automatically after plugging them into computer.  If they don’t install automatically then you need to install them manually like other software installation.

      4.   Adding audio output device (Speakers)- 
        

     if you want a bit high sound then you can use external speaker instead of  your laptop’s built in speaker. If you are a desktop user then you must require an external audio output device. Generally computers have built in connectors for speaker or headphone.  Computer speakers are generally color coded just match colored connectors to correct port.



Read More

Post Top Ad