Advertisement
Imgoodisher

Pi

Mar 14th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. local circle = [[
  2.                                
  3.             0000000            
  4.        00000000000000000      
  5.      000000000000000000000    
  6.    0000000000000000000000000  
  7.   000000000000000000000000000  
  8.   000000000000000000000000000  
  9.  00000000000000000000000000000
  10.  00000000000000000000000000000
  11.  00000000000000000000000000000
  12.   000000000000000000000000000  
  13.   000000000000000000000000000  
  14.    0000000000000000000000000  
  15.      000000000000000000000    
  16.        00000000000000000      
  17.             0000000            
  18.                                
  19. ]]
  20.  
  21. local diameter, circumfrence = 0, 0
  22. local circletbl = {}
  23.  
  24. local x, y = 1, 1
  25. for char in string.gmatch(circle, ".") do
  26.     if not circletbl[x] then circletbl[x] = {} end
  27.    
  28.     if char == "\n" then
  29.         x = 1
  30.         y = y + 2 -- Because each '0' is 2 units tall
  31.     else
  32.         circletbl[x][y] = (char == "0")
  33.         circletbl[x][y+1] = (char == "0") -- Because each '0' is 2 units tall
  34.         x = x + 1
  35.     end
  36. end
  37.  
  38. local x = #circletbl[1] / 2
  39. for y = 1, #circletbl do
  40.     if circletbl[x][y] then
  41.         diameter = diameter + 1
  42.     end
  43. end
  44.  
  45. print("Diameter of the circle is "..diameter)
  46.  
  47. for x, tbl in pairs(circletbl) do
  48.     for y, val in pairs(tbl) do
  49.         if val then
  50.             if circletbl[x][y+1] == false or circletbl[x][y-1] == false or circletbl[x+1][y] == false or circletbl[x-1][y] == false then
  51.                 circumfrence = circumfrence + 1
  52.             end
  53.         end
  54.     end
  55. end
  56.  
  57. print("Circumfrence of the circle is "..circumfrence)
  58.  
  59. print("Pi is about equal to ".. circumfrence / diameter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement