1 / 18

OWASP Speed Talks – Math Kata Demo

OWASP Speed Talks – Math Kata Demo. Yang Li OWASP Assistant Organizer NJ Chapter yang.li@owasp.org (917) 667-1972. Jan 11, 2012. Math Kata Demo.

kalb
Download Presentation

OWASP Speed Talks – Math Kata Demo

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OWASP Speed Talks – Math Kata Demo Yang Li OWASP Assistant Organizer NJ Chapter yang.li@owasp.org (917) 667-1972 Jan 11, 2012

  2. Math Kata Demo • Math Kata - A term extended from “Code Kata”, which in turn borrowed from Japanese martial art kata concept. A math kata is an exercise which help security professional horn his/her math and code skill through practices.

  3. Math Kata Demo • Why? Keep up your mind power in excellent shape before the battlefield. • How? Internet, bookstore and OWASP are your best friends. • What? I’ll give you a demo in this talk.

  4. Math Kata Demo • Example: Starting in the top left corner of a 2x2 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 20x20 grid?

  5. Math Kata Demo • My Initial Comprehension: Use a matrix with array (x,y) to represent the route position in the 2-dimentional grid. For example, the starting point is (0,0), the end point is (2,2) in 2x2 grid: Route 1: [(0,0), (1,0), (2,0), (2,1), (2,2)] Route 2: [(0,0), (1,0), (1,1), (2,1), (2,2)] Route 3: [(0,0), (1,0), (1,1), (2,1), (2,2)] Route 4: [(0,0), (0,1), (1,1), (2,1), (2,2)] Route 5: [(0,0), (0,1), (1,1), (1,2), (2,2)] Route 6: [(0,0), (0,1), (0,2), (1,2), (2,2)]

  6. Math Kata Demo • Patterns anyone? 1. Number of steps from start (0,0) -> end (2,2) are always the same (4 moves). 2. The number of steps need from (0,0) -> (2,2) is calculated as Count(2,2) - Count(0,0) = (2-0) + (2-0) =4. 3. For a larger grid 20x20, it would take Count(20,20) - Count(0,0) = (20-0) + (20-0) = 40 steps from start to end.

  7. Math Kata Demo • Patterns anyone? 4. Except for the intersection on the edges (x=0, or y=0) of the grid, there are always two ways to move to next intersection in the route (no backtracking rule). 5. For the intersection on the edges (x=0, or y=0) of the grid, there are only one way to move to the next intersection in the route (no backtracking rule).

  8. Math Kata Demo • More sketching?

  9. Math Kata Demo • Code It Out (Ruby): def pascals_triangle(x,y) return 1 if (x==0 or y==0) result = pascals_triangle(x-1,y) + pascals_triangle(x,y-1) return result end puts pascals_triangle(20,20)

  10. Math Kata Demo • Code It Out (Ruby): def pascals_triangle(x,y) return 1 if (x==0 or y==0) result = pascals_triangle(x-1,y) + pascals_triangle(x,y-1) return result end puts pascals_triangle(20,20) Recursive Function: I use a generic recursive function "pascals_triangle" to solve the puzzle. A recursion function is a function that call itself up until certain border condition is met. It's a simple and elegant solution with less than 10 line of code.

  11. Math Kata Demo

  12. Math Kata Demo • Code It Again (in “C”) – “Ruby” is too slow? #include <stdio.h> long long pascals_triangle (int x, int y); int main (void) { long long result = pascals_triangle(20,20); printf ("My calculation result is: %lld \n", result); } long long pascals_triangle (int x, int y) { if (x<=0 || y<=0) { return 1; } else { long long result = pascals_triangle(x-1,y) + pascals_triangle(x,y-1); return result; } }

  13. Math Kata Demo • It’s Faster in “C” – 46 minutes later: $ gcc euler_15.c -o pas $ time ./pas My calculation result is: 137846528820 real 46m34.642s user 46m31.517s sys 0m1.203s $

  14. Math Kata Demo • Algorithm Optimization: Space-Time Trade-off: If we could store the answer of previous calculation into cache, then we could retrieve it without re-calculation again.

  15. Math Kata Demo • Cache Mechanism Implementation (Ruby): require "memoize.rb" include Memoize def pascals_triangle(x,y) return 1 if (x==0 or y==0) return ( pascals_triangle(x-1,y) + pascals_triangle(x,y-1) ) end memoize :pascals_triangle puts pascals_triangle(20,20)

  16. Math Kata Demo • Now this is really FAST: $ time ruby euler_15.rb 137846528820 real 0m0.020s user 0m0.015s sys 0m0.006s $ Conclusion: Language does not matter speed wide, as long as your algorithm is sound.

  17. Math Kata Demo • Can you show me your code?

  18. Math Kata Demo • Credits: • Project Euler Question 15: http://projecteuler.net/ • Ruby Memoize Module: http://rubyforge.org/projects/memoize/

More Related