Guest User

1/6 = 1/2 * 1/3

a guest
Dec 27th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef unsigned int u32;
  5.  
  6. double canonical_div(u32 interations) {
  7.     double rv = 0;
  8.     for (u32 x=1; x<interations; x++) {
  9.         rv += 1.0/x;
  10.     }
  11.     return rv;
  12. }
  13.  
  14. #define M (2U * 1024*1024*1024)
  15.  
  16. double fast_div(u32 interations) {
  17.     double rv = 0;
  18.    
  19.     double *prev = malloc(M);
  20.    
  21.     for (u32 x=1; x<interations; x++) {
  22.         prev[x] = 1.0/x;
  23.         rv += prev[x];
  24.         x++;
  25.         prev[x] = 0.5 * prev[x>>1];
  26.         rv += prev[x];
  27.     }
  28.    
  29.     return rv;
  30. }
  31.  
  32. int main() {
  33.     printf("%.12f\n", canonical_div(M/8));
  34.     //~ printf("%.12f\n", fast_div(M/8));
  35.    
  36.     return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment