banovski

Pi

Sep 11th, 2020 (edited)
1,805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main()
  5. {
  6.     int number, lower_limit, upper_limit, divisor;
  7.     long double division_result, subtraction_result, absolute_value, lowest_value;
  8.    
  9.     lowest_value=1;
  10.  
  11.     for(number=100; number<=10000; number++)
  12.     {
  13.         lower_limit=(int)(number/3.2);
  14.         upper_limit=(int)(number/3.1);
  15.         for(divisor=lower_limit; divisor<=upper_limit; divisor++)
  16.         {
  17.             division_result=(long double)(number)/(long double)(divisor);
  18.             subtraction_result=division_result-M_PI;
  19.             absolute_value=fabsf(subtraction_result);
  20.             if(absolute_value<lowest_value)
  21.             {
  22.                 lowest_value=absolute_value;
  23.                 printf("%i / %i = %.9LF\n", number, divisor, division_result);
  24.             }
  25.         }
  26.     }
  27.     return(0);
  28. }
  29.  
  30. /*
  31. 100 / 31 = 3.225806452
  32. 100 / 32 = 3.125000000
  33. 101 / 32 = 3.156250000
  34. 104 / 33 = 3.151515152
  35. 107 / 34 = 3.147058824
  36. 110 / 35 = 3.142857143
  37. 179 / 57 = 3.140350877
  38. 201 / 64 = 3.140625000
  39. 223 / 71 = 3.140845070
  40. 245 / 78 = 3.141025641
  41. 267 / 85 = 3.141176471
  42. 289 / 92 = 3.141304348
  43. 311 / 99 = 3.141414141
  44. 333 / 106 = 3.141509434
  45. 355 / 113 = 3.141592920
  46. */
  47.  
Add Comment
Please, Sign In to add comment