Advertisement
Guest User

Pi Fractions

a guest
Jul 24th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.18 KB | None | 0 0
  1. #inspired by: http://davidbau.com/archives/2010/03/14/the_mystery_of_355113.html
  2. #as read on: https://hackerne.ws/item?id=4285531
  3. #this is PERL for those that don't know.
  4. #Keith Helbley, 7/24/2012, public domain
  5.  
  6. use strict;     #force slightly above terrible coding habits
  7. use warnings;       #tell us what is wrong when there are errors
  8. use Math::Trig;
  9. $|=1;           #flush the output
  10.  
  11. my $lasterror = 3;
  12. for (1..10000){ my $d = $_;
  13.     for (3..40000){ my $n = $_;
  14.        if (abs(($n/$d) - pi) < $lasterror){
  15.        $lasterror = abs(($n/$d) - pi);
  16.        print "\n $n / $d: error $lasterror";
  17.        }
  18.      }
  19. }
  20.  
  21. #output - note abs(error), error is not signed.
  22. # 3 / 1: error 0.141592653589793
  23. # 13 / 4: error 0.108407346410207
  24. # 16 / 5: error 0.0584073464102071
  25. # 19 / 6: error 0.0250740130768734
  26. # 22 / 7: error 0.00126448926734968
  27. # 179 / 57: error 0.00124177639681067
  28. # 201 / 64: error 0.000967653589793116
  29. # 223 / 71: error 0.000747583167258092
  30. # 245 / 78: error 0.000567012564152147
  31. # 267 / 85: error 0.000416183001557879
  32. # 289 / 92: error 0.000288305763706198
  33. # 311 / 99: error 0.000178512175651679
  34. # 333 / 106: error 8.32196275291075e-005
  35. # 355 / 113: error 2.66764189404967e-007
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement