Skip to content

Instantly share code, notes, and snippets.

@iblue
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iblue/11393688 to your computer and use it in GitHub Desktop.
Save iblue/11393688 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
/*
* Compile and run:
*
* gcc -lm -march=native -O3 -o madelung madelung.c -lm && ./madelung
*/
#define STEPS 100000
int main(void) {
int i,j;
long double result=0.0d;
for(i=1;i<STEPS;i++) {
for(j=1;j<STEPS;j++) {
long double temp = ((long double)1.0d)/sqrt(pow((long double)i,2.0d) + pow((long double)j,2.0d));
if((i+j)%2 == 0) {
result += temp;
} else {
result -= temp;
}
}
printf("%d/%d done\n", i, STEPS);
}
printf("%0.100f\n", (double)result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment