Vikrant0 - I have created new universe out of nothing, come have a look at it! blogFibonacci in O(logN)In my previous post about Fibonacci series, I discussed about some functions to compute fibonacci numbers. I started with a function of exponential complexity and ended it with a lazy O(N) complexity algorithm. is it possible to optimize it beyond O(N)? I found this beautiful algorithm while going through suggested exercise in SICP Lets look at iterative fibonacci first
This takes N operations for computing fib N. Look carefully at the transformation of a and b in fibiter function.
If we apply transformation T on (a,b) we get new (a',b'). To get N th Fibonacci number we will to apply this transformation N times in pair (1,0). We have to consider T as special case of transformation
with p = 0 & q = 1. (Hats off to the person who discovered this brilliant transformation!). Now Fibonacci computation will look something like this.
once more apply the transformation
this is equivalent to
this gives us p' and q' in terms of p & q
this gives us a handy way to square the transformation T! How does it help us in improving complexity of Fibonacci? That you will understand immediately if you know how to compute exponential of a number using multiplication and squaring. Lets look at how do we compute b raised to power n.
This is going to take n operations for computing nth power of b. But this can be improved further. For example b^4 can be computed by squaring b^2, b^8 can be computed by squaring b^4 etc.
This will compute b^N in O(logN)! This proves that if we know how to multiply and how to square, we can compute Nth power in O(logN) complexity. Use the same technique for applying the transformation we discussed for Fibonacci.
last updated 8 months ago # FractalsIt took few beautiful lines of haskell code to generate these beautiful shapes :)
Sierpinskie Triangle
Snowflake
Snowflake last updated 1 year ago # Kaprekar operationMathematician D. R. Kaprekar played with four digit numbers and found a magic number 6174. He did something now known as kaprekar operation. Kaprekar operation can be defined as follows. Choose any four digit number. Permute its digits to form maximum and minimum number using those digits. take difference of these two numbers. One gets new number. On this number again keep performing above operation. Surprisingly the operation tends to converge the result to a unique number, 6174! I was delighted to see this nice property of 6174. I wrote haskell programme for it to verify as well as experiment more. Once programme is ready it was easy for me to perform kaprekar operation for various numbers with different number of digits. 3 digit numbers converged to 495. Then I was expecting that even 6, 7 or 8 digit number should converge to some number! But it didn't. Still there was interesting property seen! Although these number do not converge to a single number, the process gets trapped between more than one number. It converges to some kind of sequence. You can see similar patterns for numbers with same number of digits. I generated following patterns for numbers with digits 5, 6 and 7.
haskell function for performing kaprekar operation...
last updated 1 year ago # Zen of Pythondo you know people write poems in some programming languages? Perl is famous for such things! see Perl Limerics , Perl poetry contest. But what about a language that writes poems? well, python can do that! And mind you it writes a good high level philosophic poem! on your command prompt type
and see what happens! it prints following poem
last updated 1 year ago # FibonacciFew days back I started playing with Haskell. It took a while to get into pure functional paradigm. Going in this new programing world was fascinating as well as frightening! Frightening!..yeah..because it was a shock for python/C/Java programmer to imagine programming without destructive updates, to see that once assigned you can’t reassign values to variables! And many more surprises, I could not find loops! Loops have to be programmed using recursion! But after getting in here one feels Aha…. to see that your 2-3 lines code can do wonders, while equivalent code in C++/Java code fails to give that kick. So my journey in this wonderland started with Fibonacci numbers. Recently learned functional paradigm instantly gave me easy answer
it was fine till I asked for fib 30, the programme took hours to compute fib 100! oooolaaa, I did the mistake. Memories went back to initial programming lessons. A student programmer is always advised by master programmer that although recursion is beautiful, it is not efficient, always try to put your recursion in the from of iterative loop. But then there are no loops in Haskell! Through some iterations I reached to following piece of code
and then soon I improved to following
Anand had given me idea about infinite lists in lisp long back. That time i was surprised to see something infinite on a computer. Today I felt enlightened after using it!
last updated 1 year ago # |
|