Programming , Tech, and stuff
Programming
Project Euler :: Problem 2 :: Haskell
Jan 2nd
This is the second tutorial on solving Project Euler problems using Haskell. Hope you will enjoy!
Problem Description:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
Again, create a new text file and call it euler2.hs
First, we are required to make a function that computes the numbers of the Fibonnacci sequence. From the definition fib(x) = fib(x-1) + fib(x-2), we can easily code this in Haskell.
Here is how it would look:
fib :: Int -> Int fib 1 = 1 fib 2 = 1 fib x = fib(x-1) + fib(x-2)
Now on to test this function. Open up ghci, load the file, and type in “fib 30″, which asks for the 30th Fibonacci numbers. Actually, this will be quite slow, here are my results:
*Euler1> fib 30 832040 (2.46 secs, 172153128 bytes)
2.46 seconds to get the 30th number ? Way too slow. What is happening here, is that the computer has to do a lot of counting.
fib(30) = fib(29) + fib(28)
= [fib(28) + fib(27)] + [fib(27) + fib(26) ]
and so on and on, expanding the fib, until the problem is reduced to adding up a whole bunch of 1’s together, as the first two terms are 1’s.
So we need a solution, where we don’t need to recompute fib as many times, so you could call it a form of “caching”.
Here is my proposed solution:
fib :: Int -> Int fib n = table !! n where table = 0 : 1 : zipWith (+) table (tail table)
Whoa, what’s happening here ? We are creating a “table” of the Fibonacci numbers, and then simply calling the “!!” (value at index) function to get our desired number.
We define a table as a list, with first element 0, and second element 1, and with the rest of the list being computed recursively using “zipWith (+)”.
“zipWith” is a function that takes another function, such as the plus function (+), and two lists, and it “zips” them together using that operator.
For example, zipWith (+) [1,2,3] [1,1,1] will return the list [2,3,4].
Understanding the above revised fib function is best done by simply expanding the recursive bit. Here it is from my prompt.
*Euler1> zipWith (+) [0,1] [1] [1] *Euler1> zipWith (+) [0,1,1] [1,1] [1,2] *Euler1> zipWith (+) [0,1,1,2] [1,1,2] [1,2,3] *Euler1> zipWith (+) [0,1,1,2,3] [1,1,2,3] [1,2,3,5] *Euler1> zipWith (+) [0,1,1,2,3,5] [1,1,2,3,5] [1,2,3,5,8]
So we are seeing how the second parameter of zipWith is our desired sequence. Since Haskell implement lazy evaluation, it will stop computing the list as soon as it reaches “n”.
This approach is also much faster than the previous one. See for yourself!
*Euler1> fib 30 832040 (0.00 secs, 528596 bytes)
0.00 seconds is quite damn fast, and the solution uses up a lot less memory than the previous one!
Now using our knowledge of list comprehensions from the previous tutorial, and the function described above, we can solve problem 2 on Project Euler.
fib n = table !! n
where
table = 0 : 1 : zipWith (+) table (tail table)
euler2 = sum[x | x<- takeWhile (<4000000) (map fib [1..]), even(x)]
Line 5 has a couple new function we aren’t familiar with, such as takeWhile, map, and even.
“even” returns true when its parameter is even, and else it returns false. Hence, “even(x)” is one of our conditions for the list comprehension
Now the main part of this is
x <- takeWhile (<4000000) (map fib [1..])
Let’s look at “map” first. “Map” is a function which takes another function, and a list, and it applies that function to every element of the list.
For example, “map (+1) [1,2,3]” will return the list [2,3,4]. Think of this as a kind of a “foreach” loop you might know from imperative programming languages such as C,C++, Java, etc.
Now the “takeWhile” function. This function takes a predicate p, and a list, and returns a list such that all elements in the list meet the predicate p up to a certain point in the list, when we encounter a member that does not follow the predicate, and hence the list is trimmed.
For example, “takeWhile <5 [1,4,2,6,3]” will return [1,4,2] since the number 6 does not follow the predicate, it is NOT less than 5.
So finally, what we are saying, is : “x comes from infinite list of Fibonacci numbers, but trim this list so that we only have elements less than 4 million”
At the very end, use the predefined “sum” function we know from the previous tutorial, to get the sum of the list.
Here is script executing on my machine, very rapidly thanks to that method of computing fibonacci numbers!
*Euler> euler2 4613732 (0.00 secs, 524700 bytes)
Thanks for reading! This is only my second tutorial, so could you please post some comments about improving my style, explanations, etc.
Please come back for more tutorials!
Project Euler :: Problem 1 :: Haskell
Jan 2nd
This is the first tutorial about solving the problems on Project Euler.
I will be solving problem number 1, using the language Haskell, which is a functional programming language. If you come from a imperative programming language background (C++, C, Java, PHP), you might find Haskell a little ‘weird’ at first, but it is actually quite good for quickly solving some of the Project Euler problems!
Problem Description:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Solving this using Haskell is really simple. Open up ghci (Glasgow Haskell Compiler Interpreter), and then create somewhere a new file called euler1.hs, or whatever else you please.
Here is the code:
euler1 :: Int euler1 = sum [x | x <- [1..999], x `mod` 5 == 0 || x `mod` 3 == 0]
Line 1 says that the function euler1 is of type Int (think of it as just an integer), meaning it will return an integer after calling it. Line 2 is the meat of the function. Firstly, we are using the “list comprehension” syntax. Ignore “sum” for now, and just look at what’s inbetween the square brackets. Read it as “Return me a list of numbers x, where x comes from the finite list 1 to 999, and such that x is evenly divisible by 5 OR x is evenly divisible by 3.
Finally, use the predefined “sum” function. This function takes a list of numeric values, and returns the sum of them, which is precisely what we want.
As this is my very first tutorial like this, I would appreciate any comments, such as should I write more in-depth, less in-depth, explain some concepts more, explain how to set up Haskell on your system ?
Thanks, and please come back for the second tutorial for Project Euler!
TI Basic Insulter Program
May 4th
Hmm, this is a weird program, but try it out
You input the name of the person, select the statement, and then this loops on the main screen. Press the “Enter” key to end.
DOWNLOAD:HERE
New Calculator Program
May 1st
Hey all. After I had to do about 50 different examples of these in my chemistry class, I decided to make small program in TI-Basic that solves for the pH of strong/weak acids and bases. To do the weak acids calculations, you need to provide the pKa or pKb, and the concentration of the acid/base. It uses the assumption that the concentration of H+, OH- in weak acids/bases tends to zero, and solves it without using the quadratic equation, therefore it’s not fully accurate, but its enough for the requirements of the IB. You need a calculator that supports TI-Basic, and means to transfer the program from the computer to the calculator.
Download: HERE
TUTORIAL – How to hack and cheat the Facebook game Tetris / Blockstar
Apr 5th
This is the second, and the better tutorial on how to hack and cheat in the game Facebook Tetris / Blockstar. Use this method instead of the other one posted some time ago.
EDIT: Facebook tetris has updated and this, as far as I know, does not work anymore. If you have any ideas, post in the comment section
Here is the video tutorial, and the shortened text version after the vid.
How to HACK / CHEAT the Facebook Game Tetris / Blockstar (v.2)
Step 1. Download Cheat Engine HERE.
Step 2. Go to Facebook Tetris.
Step 3. Install and run Cheat Engine. Click Attach Process in the top left, and select your browser (firefox.exe, iexplore.exe etc)
Step 4. Start playing tetris so that you get any score. Just get something like a 100 and click pause. Go to Cheat Engine. In the Value box, type in your score. For search type select Exact Value, and for Data Type select “Double”. Click First Scan. The program will now scan the memory, and it will find from 1-1000 addresses.
Step 5. What you want to do now is go back to Tetris, and unpause the game. Get another couple of points, and pause the game again. Go back to Cheat Engine, and change the value in the Value box to your current score. IMPORTANT: Click Next Scan, and not first scan. Cheat Engine should now 1 address in the address box. If you have more, perform this step again until you find only 1 address.
Step 6. When you do find the 1 address, double click on it. This will add it to the window in the bottom of Cheat Engine. Now Right Click on the address, and select “Set Hotkey” or something along the lines of hotkey. You will a window pop up. For the key combination, click anything, I chose the Del key but you can chose anything you want. For the dropdown menu, select “Increment”, and for the increment value, put 10000 (IMPORTANT, no more than 10000). Click OK
Step 7. Go back to Tetris and unpause the game. Start playing, and hit the Del key or whatever you set for your hotkey. Your score will now increase by 10000. Keep on clicking delete and playing at the same time. If you just mash the hotkey the game will find out you are cheating. So don’t overdo it, drop one block, hit the hotkey, and you should easily get >2 million points
If the above is not working for you then you did something wrong. Reread the tutorial and watch the video again.
I hope this was useful, and I’m still working on finding a method to allow you to input any score you want without consequences, so check back the site often.
No go on and get yourself a high Blockstar score!!!